Trouble understanding global variables and local variable program.

This is a discussion on Trouble understanding global variables and local variable program. within the C Programming forums, part of the General Programming Boards category; I've recently recieved the task of making a program where it distinguishes the difference between global and local variables. Code: ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Keep the count as a static variable inside the function. Return the results of dividing the sum by the count.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    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. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #5
    Registered User
    Join Date
    May 2011
    Posts
    30
    @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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  2. Local vs Global Variables
    By BENCHMARKMAN in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2007, 05:17 AM
  3. local and global variables???
    By geo_c in forum C Programming
    Replies: 5
    Last Post: 08-23-2004, 03:02 PM
  4. Global Vs Local Variables
    By Waldo2k2 in forum C++ Programming
    Replies: 17
    Last Post: 11-11-2002, 06:51 PM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21