# Thread: Array from value of expression in while loop

1. ## Array from value of expression in while loop

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? 2. Originally Posted by ryananananan 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?
Can you provide a better explanation of what you're trying to achieve? 3. Originally Posted by Hodor Can you provide a better explanation of what you're trying to achieve?

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 4. Originally Posted by ryananananan 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
yes, it's a little bit clearer. Can you show us the code you've written so far attempting to solve this problem?

Edit: btw, to store a value in a value in an array is the same no matter how the value is derived 5. 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 6. How can I store the total cost into an array each time the loop passes?
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;
};```
For a while loop define an int for count and increment inside the loop as x. 7. @Structure: Your code does not compile:

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 | };```
Please don't post code as a solution to an OP, unless your code DOES compile correctly.

Turn ON and turn UP your warning level to the highest level!

How can I store the total cost into an array each time the loop passes?
This code really does not answer the OP's original question. 8. Originally Posted by rstanley @Structure: Your code does not compile
Of course it doesn't.

I vote to cancel "Structure's" account. 9. @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. 10. 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 | };```
interesting...

Code:
```#include <stdio.h>

float values = { 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;
};``` 11. I vote to cancel "Structure's" account.
for what? 12. 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" 13. ## 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 = { 0 };

int count = 0;
float total = 0;

char user_continue = 'y';

while ( user_continue == 'y' ) {
printf( "\n enter an amount:" );
count++;
printf( "continue?" );
user_continue = getch();
};

for ( int i=0; i <= count; i++ ) {
total += values[i];
};

printf( "total: %f", total );
return 0;
};``` Popular pages Recent additions array, cost, expression, loop, total 