NUTrition C++ Code

The NUTrition software was a tiny project to take in nutritional values for a meal or food item (Calories, Grams of Fat, Vitamin A, Sodium, Grams of Carb, Protein, Vitamin C, Calcium, Iron) and re-express it as an array of nut equivalents.


/*
W. Brenna
May 31, 2012


	This program receives an input array of nutritional
	information and converts it to the number of grams
	of an orthogonal array of tree nuts.

	The input is:
	Calories	Grams of Fat	Vitamin A
	Sodium		Grams of Carb	Protein
	Vitamin C	Calcium		Iron

	We need to form an array of input and output,
	inverting the nut matrix, ensuring we don't use
	collinear nuts.

	The nut array is
	[
		Walnut
		Almond
		Peanut
		Macademia
		Cashew
		Pecan
		Chestnut
		Hazelnut
		Pistachio
	]

*/

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc, char* argv[])
{

	if(argc != 10)
	{
		cout << "Wrong number of input parameters. Fix this." << endl;
		cout << "Enter in the form: \"Calories Total_Fat Vitamin_A Sodium_mg Total_Carbs Protein Vitamin_C Calcium Iron\""<< endl;
		return EXIT_FAILURE;
	}

	int input[9];
	for(int i=1; i<10; i++)
	{
		input[i-1] = atoi(argv[i]);
	}

	char nuttype[9][15] = {	"Walnut", "Almond", "Peanut",
				"Macadamia", "Cashew", "Pecan",
				"Chestnut", "Hazelnut", "Pistachio"};

	//set up the inverted nut matrix
	double nutval[9];
	double nutmat[9][9] = {  
				 9.3114e+01,  -1.2404e+01,  -2.2703e+01,   3.4382e+01,   4.6017e+00,  -8.9574e+01,   6.7290e-01,  -1.7185e+01,   6.3501e+00,
  				-7.7819e+02,   1.0325e+02,   1.8998e+02,  -2.8699e+02,  -3.8932e+01,   7.5041e+02,  -5.6071e+00,   1.4329e+02,  -5.3200e+01,
   				 3.4293e+01,  -7.4176e+00,  -9.1502e+00,   2.3748e+01,  -2.0533e+00,  -4.3549e+01,  -4.2582e-01,  -9.2037e+00,   1.0284e+01,
  				-4.4865e+00,   2.7954e-01,   4.4463e+00,   8.5843e+00,  -1.3353e+00,  -3.3239e+00,   7.9077e-01,  -5.0003e+00,   1.8093e-01,
  				-1.8488e+01,   4.4664e+00,   2.9216e+00,  -1.0156e+01,   2.5891e+00,   2.3803e+01,   6.9162e-01,  -1.9771e+00,  -1.7496e+00,
  				-2.1237e+01,   2.4779e+00,   9.5194e+00,  -2.1025e+01,  -1.9353e+00,   3.0058e+01,  -4.4360e-01,   7.0198e+00,  -2.1153e+00,
  				 8.0570e+00,  -2.3429e+00,  -9.6204e-01,   4.4725e+00,  -1.8031e+00,  -1.0717e+01,   1.0656e+00,   1.8081e+00,   6.8938e-01,
				-7.9276e+00,   5.4660e+00,   7.7388e-01,   5.7099e+00,  -2.7095e+00,  -1.3446e+00,   1.6970e-01,  -2.1592e-01,   8.3923e-02,
 				 3.0746e+01,  -5.5571e+00,  -9.4759e+00,   1.6022e+01,   2.7636e+00,  -3.8243e+01,  -7.1117e-01,   1.0099e-03,   2.7824e+00
			   };


	//perform the multiplication
	for(int i=0; i<9; i++)
	{
		nutval[i] = 0;
		for(int j=0; j<9; j++)
		{
			nutval[i] += (nutmat[i][j])*(input[j]);
		}
	}


	//and print out the nuts
	cout << "The food corresponds to:" << endl;
	for(int i=0; i<9; i++)
	{
		cout << "\t" << setiosflags(ios::fixed) << setprecision(2) << setw(14) << nutval[i] << "\tgrams of " << nuttype[i] << endl;
	}
	return EXIT_SUCCESS;
}