I have an expression in a while loop.
// Calculate the total cost
total_cost = X1 * X2;
How can I store the total cost into an array each time the loop passes?
I have an expression in a while loop.
// Calculate the total cost
total_cost = X1 * X2;
How can I store the total cost into an array each time the loop passes?
Thanks for your reply Hodor.
I am running a function where a user inputs 2 dimensions which provides a number of calculations based on those dimensions. This function keeps looping until the user decides to end the function.
Each time the function loops, there are some calculations which I would like to store in an array which will printf the sum of these values after the user has decided to end the function.
I have come across tutorials on how to store values in an array where the user has directly entered the values, however I am having difficulty storing values in an array where the values come from an expression or a statement.
I hope this is a bit clearer.
Thanks
Code:// Loop until the user decides to end user_continue = 'y'; while (user_continue == 'y' || user_continue == 'Y') { // Get dimensions // Get the width correct = 0; while (correct == 0) { printf("Enter the width(m) > "); fflush(stdin); scanf("%f", &width); if (width <= 0) printf("Error: Width must be greater than 0(m).\n"); else correct = 1; } // Get the height correct = 0; while (correct == 0) { printf("Enter the height(m) > "); fflush(stdin); scanf("%f", &height); if (height <= 0) printf("Error: Height must be greater than 0(m).\n"); else correct = 1; } // Calculations cost = width * height * price /* would like to store the value of cost into an array each time the function loops. */ printf("Would you like to process another dimension? (y/n) > "); scanf("%s", &user_continue); printf("The total cost is %f", total_cost); /* total cost to be the sum of values in array */
The code is quite long so have just quickly re-written a short version so you can understand the gist of it.
Thanks
How can I store the total cost into an array each time the loop passes?For a while loop define an int for count and increment inside the loop as x.Code:#include <stdio.h> float values[] = { }, total; int main() { for (int x=0; x<=10; x++) { values[x] = x; }; for (int x=0; x<=10; x++) { total += values[x]; }; printf( "%f", total ); return 0; };
Last edited by Structure; 09-19-2019 at 07:42 AM.
"without goto we would be wtf'd"
@Structure: Your code does not compile:
Please don't post code as a solution to an OP, unless your code DOES compile correctly.Code:foo.c:3:18: warning: ISO C forbids empty initializer braces [-Wpedantic] 3 | float values[] = { }, total; | ^ foo.c:3:7: error: zero or negative size array ‘values’ 3 | float values[] = { }, total; | ^~~~~~ foo.c:18:2: warning: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic] 18 | };
Turn ON and turn UP your warning level to the highest level!
This code really does not answer the OP's original question.How can I store the total cost into an array each time the loop passes?
Last edited by rstanley; 09-19-2019 at 08:07 AM.
@ryananananan:
Please present a working program that we can compile and test. Data types of variables, #includes, main() and other details are missing.
Also, please be careful coping code from an IDE as there is hidden formatting that you have copied over when presenting the code above.
interesting...Code:foo.c:3:18: warning: ISO C forbids empty initializer braces [-Wpedantic] 3 | float values[] = { }, total; | ^ foo.c:3:7: error: zero or negative size array ‘values’ 3 | float values[] = { }, total; | ^~~~~~ foo.c:18:2: warning: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic] 18 | };
Code:#include <stdio.h> float values[20] = { 0 }; float total; int main() { for (int x=0; x<=10; x++) { values[x] = x; }; for (int x=0; x<=10; x++) { total += values[x]; }; printf( "%f", total ); return 0; };
"without goto we would be wtf'd"
for what?I vote to cancel "Structure's" account.
"without goto we would be wtf'd"
Well, I guess that first you need an array and also a variable to hold the current index (the next space in the array to store the value). Each time the user enters a number and you do the calculation store the result in the array (using array[current_index] = value; for example) and increment current index so the next time through the loop you store the value in the next array "slot"
Well, I guess that first you need an array and also a variable to hold the current index (the next space in the array to store the value). Each time the user enters a number and you do the calculation store the result in the array (using array[current_index] = value; for example) and increment current index so the next time through the loop you store the value in the next array "slot"
Here is an example:
Code:#include <stdio.h> #include <conio.h> int main() { float values[20] = { 0 }; int count = 0; float total = 0; int addAmount = 0; char user_continue = 'y'; while ( user_continue == 'y' ) { printf( "\n enter an amount:" ); scanf( "%i", &addAmount ); values[count] = addAmount; count++; printf( "continue?" ); user_continue = getch(); }; for ( int i=0; i <= count; i++ ) { total += values[i]; }; printf( "total: %f", total ); return 0; };
Last edited by Structure; 09-20-2019 at 01:36 PM.
"without goto we would be wtf'd"