0% found this document useful (0 votes)
4 views2 pages

Calculate The Total of Ingredients

The document contains two versions of a C program that calculates the total price of ingredients based on their weights and prices. The first version reads the quantity of ingredients, their weights, and prices, then computes the total price. The second version simplifies the process by reading prices first and then quantities, ultimately achieving the same result with a different structure.

Uploaded by

Fredy Criollo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Calculate The Total of Ingredients

The document contains two versions of a C program that calculates the total price of ingredients based on their weights and prices. The first version reads the quantity of ingredients, their weights, and prices, then computes the total price. The second version simplifies the process by reading prices first and then quantities, ultimately achieving the same result with a different structure.

Uploaded by

Fredy Criollo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>

int main(){

int qty;
int i=0;
//int j=0;

scanf ("%d", &qty);


//printf(" the recipe has %d ingredients\n", qty);

double pounds[qty];
double price[qty];
double total=0;

for (i=0; i<qty; i++)


{
scanf("%lf", &pounds[i]);

//printf("%.1lf\n", pounds);
}
for (i=0; i<qty; i++)
{
scanf("%lf", &price[i]);

//printf("%.1lf\n", price);
total = total + (pounds[i]*price[i]);

printf("%.6lf\n", total);

return 0;
}

Note: use just i, if I use i and j to calculate separately the Price and weight it
won´t give the expexted Output.

-------------------------------

v2.0 solution from edx:


#include <stdio.h>

int main()
{
int nbIngredients=0;
int i, idIngredient;
double price[10];
double totalPrice = 0.0;
double readPrice=0.0;

scanf("%d", &nbIngredients);

for(i = 0; i < nbIngredients; i++){


scanf("%lf",&readPrice);
price[i] = readPrice;
}

for (idIngredient = 0; idIngredient < nbIngredients; idIngredient++)


{
double quantity;
scanf("%lf\n",&quantity);
totalPrice = totalPrice + price[idIngredient] * quantity;
}
printf("%lf\n",totalPrice);

return 0;
}

You might also like