Thread: Loops and storing values in memory

  1. #1
    Registered User
    Join Date
    Dec 2021
    Posts
    5

    Loops and storing values in memory

    How can we keep accepting values in a loop and also calling them in a different function without resetting the function whenever the new iteration happens?

    for eg.
    Code:
    int main()
    
    {
    
    int somevalue,avg;
    
    for(i=1;i<=5;i++)
    
    {
         scanf("%d",&somevalue);
    }
    
    avg=somefunction(somevalue);
    
    return 0;
    
    }
    int somefunction(int x)
    {
    int t=0;
    t=t+x/5;
    return t;
    }
    In this function I wanted to calculate the average of 5 numbers, but I don't want to manually write `scanf` for 5 values, as if I wanted to make a program with many numbers, it would be tedious.

    But as far as I know, there is no way this is possible, as with every new value of `i`, the function `somefuncton()` gets "reset" and the value of t becomes 0 again. Tried the same with pointers too, no result.

    So like is there actually a way to make the program which "stores" the value somehow and uses the "stored" value in the next new iteration?

    Pls help
    (ಥ⌣ಥ)
    Last edited by ZeenX1; 12-11-2021 at 12:43 PM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by ZeenX1 View Post
    How can we keep accepting values in a loop and also calling them in a different function without resetting the function whenever the new iteration happens?

    In this function I wanted to calculate the average of 5 numbers, but I don't want to manually write `scanf` for 5 values, as if I wanted to make a program with many numbers, it would be tedious.

    But as far as I know, there is no way this is possible, as with every new value of `i`, the function `somefuncton()` gets "reset" and the value of t becomes 0 again. Tried the same with pointers too, no result.

    So like is there actually a way to make the program which "stores" the value somehow and uses the "stored" value in the next new iteration?

    Pls help
    (ಥ⌣ಥ)
    Try this based on your code:
    Code:
    #include <stdio.h>
    
    // Please don't use "Magic Numbers"!  Use a #define
    #define DIM 5
    
    // Make the average calculating function more generic
    int somefunction(int total, int count);
    
    int main(void)
    {
       int somevalue = 0;
       int avg = 0;
    
       int total = 0;
       int count = 0;
    
    // for(i=1;i<=5;i++)
       for(int i = 0; i < DIM; i++)  // Better especially with array subscripts!
       {
          printf("Please enter value #%d ", i + 1);
          scanf("%d",&somevalue);
          total += somevalue;
          count++;
       }
    
       avg=somefunction(total, count);
    
       printf("Total: %d, count: %d, average: %d\n", total, count, avg);
    
       return 0;
    }
    
    // somefunction() should calculate and return a float or double!
    // Not all averages are whole numbers!
    int somefunction(int total, int count)
    {
    //   int t=0;
    //   t=t+x/5;
       return total / count;
    }

  3. #3
    Registered User
    Join Date
    Dec 2021
    Posts
    5
    I know how to do it the way you have done it, but I want to know is there a way that a function, other than `main()`, can store the values of, say i=2, and then "recall" it when i=3.
    Like I am putting it in a very bad way and is most likely not possible,
    for instance what you did was, first calculating the total in the loop itself and then sending the total to `somefunction()`
    Code:
     for(i=1;i<=5;i++)
       for(int i = 0; i < DIM; i++)  // Better especially with array subscripts!
       {
          printf("Please enter value #%d ", i + 1);
          scanf("%d",&somevalue);
          total += somevalue;
          count++;
       }
    
       avg=somefunction(total, count);
    But what I wanted to do is that, somehow the function `somefunction()`should store all the incoming values. I haven't started arrays yet, but is there going to be a way to store the incoming values of the loop in some array of `somefunction()`?


    In short, is there a way to call `somefunction()` inside the loop and have it store the values even in the next values of i?
    It doesnt even need to be a function to calculate the average, just something that stores values.

    for eg.

    Code:
    intsum(int);
    intmain()
    {
    inti,a,x,num;
    
    scanf("%d",&num);
    
    for(i=0;i<num;i++)
        {
    scanf("%d",&x);
    sum(x);
        }
    
    a=sum(x);
    printf("%d",a);
    
    
    
    
    
    getch();
    return0;
    }
    
    intsum(intx)
    {
    intt=0;
    t+=x;
    returnt;
    }
    
    
    In this the function is called in the loop itself, so whenever new value of `i` starts, the value of`t` is set again to 0, which I want to avoid.

    This question 99% has no solution to it, but I donno why I feel like there is one.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    ZeenX1:

    Yes, this could be done with an array to hold the entries. One function to input the values into the array, then a second to calculate the average of all the values, both called from main() along with the printf() statement to display the results.

    I haven't started arrays yet, ...
    I would suggest to study arrays first before attempting this.

    Also, Copy and Paste your code into [CODE] blocks, from your IDE using plain text! Without the colorization.

    Also, indentation is important. Compare my indented code against your code without indentation! See the Wikipedia article on indentation styles! Choose one and use it consistently!
    Last edited by rstanley; 12-12-2021 at 07:45 AM.

  5. #5
    Registered User
    Join Date
    Dec 2021
    Posts
    5
    THANK YOUUUUUUUUUUUUUUUUU!!!!!
    ~
    Copy and Paste your code into [CODE] blocks, from your IDE using plain text! Without the colorization.
    yeah would^^

    ~
    indentation is important
    have been skipping to learn formatting, waiting till most of c is done
    ps copy pasting from ide added the additional lines

    >>Thanks again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values in an array
    By kpop in forum C Programming
    Replies: 3
    Last Post: 04-12-2011, 01:46 PM
  2. Storing values in memory
    By Opel_Corsa in forum C Programming
    Replies: 5
    Last Post: 09-29-2006, 03:10 AM
  3. Storing different values of time
    By stellastarr in forum C Programming
    Replies: 3
    Last Post: 02-08-2006, 10:38 AM
  4. storing values.....NEED HELP.
    By new2tehgame in forum C++ Programming
    Replies: 9
    Last Post: 09-14-2005, 08:17 PM
  5. storing values
    By hen in forum C Programming
    Replies: 4
    Last Post: 07-08-2002, 03:20 PM

Tags for this Thread