Thread: How to write a function and calling it in the main using a global variable array?

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    1

    How to write a function and calling it in the main using a global variable array?

    I'm a college student very new to programming. I'm having some troubles with this specific program.

    The program should be able to store 500 integer values (temperatures) in a global array. The program has to contain the specific functions:

    • A function that finds the average of all temperatures stored
    • A function that finds the average of the last 5 recorded temperatures. If there are less than 5 temperatures recorded, return the average of all recorded temperatures.
    • A function that records a new temperature. It must check if there is still space in the array.
    • A function that prints the number of temperatures recorded
    • A function that prints all the temperatures recorded

    Here is my current program. I only need help writing ONE of the functions. I want to learn how to write and call them myself with some guidance. Thanks all.

    Code:
    #include <stdio.h>
    #define maxsz 500
    
    
    int temp[maxsz];
    
    
    float avgtemp ()
    {
        
    }
    float avgtemp5()
    {
    }
    int addtemp ()
    {
    
    
    }
    int numtemp()
    {
    }
    int alltemp()
    {
    }
    int main(void)
    {
    
    
        char option;
    
    
        printf("Please select an option:\n");
        printf("(A) Computed average of all temperatures\n");
        printf("(B) Compute the average of the last five temperatures\n");
        printf("(C) Add new Temperatures\n");
        printf("(D) Print the number of temperatures recorded\n");
        printf("(E) Print the temperatures recorded\n");
        printf("(F) Exit the program\n");
        scanf(" %c", &option);
        
        switch (option)
        {
            case 'A': avgtemp();
            
            case 'B': avgtemp5();
                
            case 'C': addtemp();
            
            case 'D': numtemp();
                
            case 'E': alltemp();
                
            case 'F': return 0;
        }
    
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In the first place, you should not be using a global array. Rather, write your functions to operate on a pointer to the first element of some array, along with either the size of the array, or a pointer to one past the end of the same array. For the former:
    Code:
    float avgtemp(const int temperatures[], size_t num)
    {
        /* sum all num temperatures, e.g., using a loop, then divide by num cast to float and return */
    }
    Notice that I declared the pointer to be a pointer to const int: this is because avgtemp does not need to modify the values to compute the average. If you have not seen size_t, don't worry: that is just an unsigned integer type that is the type of the result of the sizeof operator. You could just use int for now.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    How are you supposed to populate the array? The questions imply that the array may hold any number of temperature values (up to the max).

    For development purposes, I'd suggest you print a string in each function saying which function you're in. This will help you uncover a problem in "main()". These strings can be removed as you move forward.

    Build your program gradually, testing along the way: A development process

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string declaration as global vs a main local variable...
    By Jim Iwannou in forum C Programming
    Replies: 4
    Last Post: 11-14-2013, 03:12 AM
  2. how to scanf a global variable outside the main ?
    By Islam Assi in forum C Programming
    Replies: 3
    Last Post: 09-07-2013, 04:45 AM
  3. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  4. Replies: 2
    Last Post: 02-09-2006, 06:56 AM
  5. How to declare a global variable in Main()
    By vnrabbit in forum C Programming
    Replies: 2
    Last Post: 06-20-2002, 12:59 PM