# Thread: Trouble understanding global variables and local variable program.

1. ## Trouble understanding global variables and local variable program.

I've recently recieved the task of making a program where it distinguishes the difference between global and local variables.

Code:
```#include <stdio.h>
#include <math.h>
# define PRINT(x,y,z) printf("running average is %d / %d = %.3f Floor is %.0f Ceiling is %.0f\n", #x, #y, #z, floor(z), ceil(z))

double runningAverage(int sum, int numInput)
{

static int numinput = 0;
static double avg = 0;
numinput++;
static int tempsum;

tempsum = tempsum + sum;
avg = (double)tempsum / numinput;
printf("running average is %d / %d = ", sum, numinput);
printf("%.3f Floor is %.0f Ceiling is %.0f\n", avg, floor(avg), ceil(avg));
//PRINT(tempsum, numinput, count);
return 0;
}

int main()
{
int count;
int num;
printf("enter number (-1 to quit): ");
scanf("%d", &num);
while (num != -1)
{
count++;
runningAverage(num, count);
printf("enter number (-1 to quit): ");
scanf("%d", &num);
}
return 0;
}```
The program is supposed to read a number until -1 is inputted. Until then, it averages the number through each iteration and floors and ceilings it.

macro doesnt work so it's commented out. but my main problem is figuring out a method to make the function double runningAverage(int input) work instead of the two argument function that I currently have without using global variables.

In what ways can I achieve this? I can do the same program with a void function but I don't know how to tackle this otherwise. Any help would be greatly appreciated.

2. Keep the count as a static variable inside the function. Return the results of dividing the sum by the count.

Quzah.

3. Just drop the numInput parameter from the function definition and don't pass in count from main:
Code:
```double runningAverage(int sum)  // no second param, the static variables will take care of it.
...
runningAverage(num);  // no need to pass in count```
Also, having two variables whose names differ only by case (numinput and numInput in runningAverage) is a very bad idea. Easy to make mistakes, hard to find them. As for your macro, you shouldn't be using the "stringify" operator (#) on x, y and z. But really there is no need for your PRINT macro, you would be better off returning avg from runningAverage and using that value in your printf in main.:
Code:
```while (num != -1)
{
...
avg = runningAverage(num);
printf("running average is....", avg, floor(avg), ciel(avg));```

4. That's not a very good case for a function... Quite frankly I do the whole thing in main() in a loop. The input, calculation and printout are too intertwined to separate conveniently.

5. @quzah - Thanks for pointing me in the right direction, it helped, got it done.

@anduril - Thanks, for the advice, I was kinda panicking on the fly because I had very little time to do it so I did some dumb things.

@CommonTater - I realized that even before I had to do the program but, when instructed, I can't take any other route.