Code:
#include <stdio.h>
#include <conio.h>
/*[0] is always the serving weight
[1]-[7] nutritional value*/
typedef float arrFruit[8];
typedef float arrOne[8];
/*Function displays 10 fruits and/or vegetables + their nutritional information*/
void marketoptions(arrFruit fruit)
{
int ex, i;
float fhun = 100.0;
float hund;
arrOne ahundred;
/*Uses the ahundred array to get the nutritional info per 100g of the food*/
for(ex = 1 ; ex<=8 ; ex++)
ahundred[ex] = (100.0/fruit[0]);
for(i = 1 ; i<=7 ; i++)
printf("%9.2f", ahundred[i]*fruit[i]);
printf("\n");
}
/*computes servings per grams/other*/
void servingscompute(arrFruit fruit, float grams)
{
float result;
result = grams/fruit[0];
printf("%.2f grams is equivalent to %.2f servings\n", grams, result);
}
/*Function for nutrient information. One fruit/veg per run.*/
void nutrientinfo(arrFruit fruit, float grams)
{
int i, ex;
arrOne ahundred;
printf("%9s %8s %8s %8s %8s %8s %8s\n", "Fol(mg)", "Ir(mg)",
"Zi(mg)", "Ca(mg)", "A(IU)", "C(mg)", "E(mg)");
for(ex = 1 ; ex<=8 ; ex++)
ahundred[ex] = (grams/fruit[0]);
for(i = 1 ; i<=7 ; i++)
printf("%9.2f", ahundred[i]*fruit[i]);
printf("\n");
}
/*goes to market. other options may be available here.. */
void gomarket(arrFruit aPapaya, arrFruit aStraw, arrFruit aSpinach, arrFruit aGrapefruit,
arrFruit aMango, arrFruit aCherry, arrFruit aArtich, arrFruit aOkra, arrFruit aBroc, arrFruit aKiwi)
{
}
/*Program hub. starts and returns here when done*/
int main()
{
int number, choice, monthspreg, chs;
float serving, result;
/*arrays of fruits*/
arrFruit aPapaya = {78.5, 29, 0.20, 0.06, 16, 746, 47.8, 0.24};
arrFruit aStraw = {72, 17, 0.30, 0.10, 12, 9, 42.3, 0.21};
arrFruit aSpinach = {90, 131, 3.21, 0.68, 122, 9433, 8.8, 1.87};
arrFruit aGrapefruit = {123, 16, 0.10, 0.09, 27, 1414, 38.4, 0.16};
arrFruit aMango = {82.5, 35, 0.13, 0.07, 9, 893, 30, 0.74};
arrFruit aCherry = {244, 20, 3.34, 0.17, 27, 1840, 5.1, 0.56};
arrFruit aArtich = {120, 107, 0.73, 0.48, 25, 16, 8.9, 0.23};
arrFruit aOkra = {80, 37, 0.22, 0.34, 62, 226, 13, 0.22};
arrFruit aBroc = {78, 84, 0.52, 0.35, 31, 1207, 50.6, 1.13};
arrFruit aKiwi = {69, 17, 0.21, 0.10, 23, 60, 64, 1.01};
/*characters that dont really have a serious use other than placeholders etc*/
char dumper, end;
/*Gains important information from the user that may be needed later on*/
printf("Welcome!\n");
printf("First thing's first. How many months are you in pregnancy?: ");
scanf("%d%c", &monthspreg, &dumper);
/*main part of program. stops the loop when indicated by user*/
do{
if (monthspreg > 9)/*prevents invalid input on monthspreg*/
printf("Invalid input.\n");
else{
printf("\n[1] View available selections\n");
printf("[2] Compute Servings from food\n");
printf("[3] Compute Nutrient Info of Food\n");
printf("[4] Go to market!\n");
printf("What do you want to do? ");
scanf("%d%c", &choice, &dumper);
if(choice==1)/*View available selections*/
{
printf("\nHere are the fruits and vegetables you can choose from:\n");
/*Table*/
printf("%11s %9s %8s %8s %8s %8s %8s %8s\n", "Food", "Fol(mg)", "Ir(mg)",
"Zi(mg)", "Ca(mg)", "A(IU)", "C(mg)", "E(mg)");
printf("%11s", "1.Papaya");
marketoptions(aPapaya);
printf("%11s", "2.Strwberry");
marketoptions(aStraw);
printf("%11s", "3.Spinach");
marketoptions(aSpinach);
printf("%11s", "4.Grapefrt");
marketoptions(aGrapefruit);
printf("%11s", "5.Mango");
marketoptions(aMango);
printf("%11s", "6.Cherry");
marketoptions(aCherry);
printf("%11s", "7.Artichoke");
marketoptions(aArtich);
printf("%11s", "8.Okra");
marketoptions(aOkra);
printf("%11s", "9.Broccoli");
marketoptions(aBroc);
printf("%11s", "10.Kiwi");
marketoptions(aKiwi);
}
if(choice == 2)/*Compute servings*/
{
printf("Which fruit/vegetable do you want to calculate servings for? (1-10) ");
scanf("%d%c", &chs, &dumper);
if(chs > 10 || chs < 1)
{
printf("Invalid Option\n");
}
else
{
printf("Input amount of food in grams: ");
scanf("%f%c", &serving, &dumper);
/*sends the correct fruit/vegetable info to the servingscompute function*/
switch(chs)
{
case 1: servingscompute(aPapaya, serving);
break;
case 2: servingscompute(aStraw, serving);
break;
case 3: servingscompute(aSpinach, serving);
break;
case 4: servingscompute(aGrapefruit, serving);
break;
case 5: servingscompute(aMango, serving);
break;
case 6: servingscompute(aCherry, serving);
break;
case 7: servingscompute(aArtich, serving);
break;
case 8: servingscompute(aOkra, serving);
break;
case 9: servingscompute(aBroc, serving);
break;
case 10: servingscompute(aKiwi, serving);
}
}
}
if(choice == 3)/*Compute nutrition*/
{
printf("Which fruit/vegetable do you want to calculate nutrients for? (1-10) ");
scanf("%d%c", &chs, &dumper);
if(chs > 10 || chs < 1)
{
printf("Invalid Option\n");
}
else
{
printf("Input amount of food in grams: ");
scanf("%f%c", &serving, &dumper);
/*works the same as option 2*/
switch(chs)
{
case 1: nutrientinfo(aPapaya, serving);
break;
case 2: nutrientinfo(aStraw, serving);
break;
case 3: nutrientinfo(aSpinach, serving);
break;
case 4: nutrientinfo(aGrapefruit, serving);
break;
case 5: nutrientinfo(aMango, serving);
break;
case 6: nutrientinfo(aCherry, serving);
break;
case 7: nutrientinfo(aArtich, serving);
break;
case 8: nutrientinfo(aOkra, serving);
break;
case 9: nutrientinfo(aBroc, serving);
break;
case 10: nutrientinfo(aKiwi, serving);
}
}
}
if (choice == 4)/*Go to market*/
{
gomarket(aPapaya, aStraw, aSpinach, aGrapefruit, aMango, aCherry, aArtich, aOkra,
aBroc, aKiwi);
}
} /*end of else statement. keep intact.*/
printf("%.2f\n", aPapaya [0]);
printf("Anything else? (y/n) ");
scanf("%c%c", &end, &dumper);
printf("\n");
}while(end != 'n') ;
/*exits the program*/
printf("+++++++++HAVE A NICE DAY :D+++++++++\n");
printf("Press enter again to exit\n");
getch();
return 0;
}
I've been stuck at doing gomarket function which does what I described earlier... I just need some clues as to what I should do next.