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.