Thread: Array from value of expression in while loop

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    5

    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. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by ryananananan View Post
    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. #3
    Registered User
    Join Date
    Sep 2019
    Posts
    5
    Quote Originally Posted by Hodor View Post
    Can you provide a better explanation of what you're trying to achieve?
    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

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by ryananananan View Post
    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
    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. #5
    Registered User
    Join Date
    Sep 2019
    Posts
    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. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    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.
    Last edited by Structure; 09-19-2019 at 07:42 AM.
    "without goto we would be wtf'd"

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    @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.
    Last edited by rstanley; 09-19-2019 at 08:07 AM.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by rstanley View Post
    @Structure: Your code does not compile
    Of course it doesn't.

    I vote to cancel "Structure's" account.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    @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. #10
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    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[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"

  11. #11
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    I vote to cancel "Structure's" account.
    for what?
    "without goto we would be wtf'd"

  12. #12
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    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. #13
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Lightbulb

    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array can't be created (expression must have a constant value)
    By DecoratorFawn82 in forum C Programming
    Replies: 5
    Last Post: 01-03-2018, 03:00 PM
  2. initializer expression list treated as compound expression
    By karthikeyanvisu in forum C Programming
    Replies: 7
    Last Post: 02-26-2011, 05:19 PM
  3. Replies: 2
    Last Post: 11-25-2009, 07:38 AM
  4. Converting to expression to string of array elements
    By Sailors in forum C Programming
    Replies: 12
    Last Post: 07-26-2007, 03:01 PM
  5. Replies: 18
    Last Post: 07-25-2007, 07:55 AM

Tags for this Thread