Thread: function and array assistance please

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    function and array assistance please

    The function char findMonth(int) is supposed to return the month
    that corresponds to the number entered by the user.

    The int lowestRainfall () function is supposed to return the month with the lowest rainfall.

    These are the errors I get when i compile

    rainfalls.cpp: In function `char findMonth(int)':
    rainfalls.cpp:63: error: declaration of 'char months[12][10]' shadows a parameter
    rainfalls.cpp: In function `int lowestRainfall(int (*)[2])':
    rainfalls.cpp:78: error: `sum' undeclared (first use this function)
    rainfalls.cpp:78: error: (Each undeclared identifier is reported only once for each function it appears in.)
    rainfalls.cpp:79: error: `months' undeclared (first use this function)
    rainfalls.cpp:79: error: `monthNum' undeclared (first use this function)
    rainfalls.cpp:83: error: expected `}' at end of input

    Code:
    #include <iostream>
    
    using namespace std;
    
    void getData(int rainfall[12][2]);
    int averageHigh(int rainfall[12][2]);
    int averageLow(int rainfall[12][2]);
    char findMonth(int months);
    int main()
    {
        int rainfall[12][2];
        char months[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"};
        int monthNum;
    
        getData(rainfall);
        averageHigh(rainfall);
        averageLow(rainfall);
        findMonth(monthNum);
    
        return 0;
    
    }
    void getData(int rainfall[12][2])
    {
        for (int i = 0; i < 12; i++)
        {
                    cout << "Please enter the highest and lowest amount of rainfall for the month." << endl;
                    cin >> rainfall[i][0] >> rainfall[i][1];
        }
    }
    
    int averageHigh(int rainfall[12][2])
    {
                    double sum=0;
                    double average=0;
                     for(int a=0;a<12;a++)
    
            {
                    sum+=rainfall[a][0];
            }
                    average=sum/12.0;
                    cout<<"Average high is- "<<average<<endl;
    
    
    
    }
    int averageLow(int rainfall[12][2])
    {
                    double sum=0;
                    double average=0;
                    for(int a=0;a<12;a++)
    
            {
                    sum+=rainfall[a][1];
            }
                    average=sum/12.0;
                    cout<<"Average low is- "<<average<<endl;
    }
    char findMonth(int months)
    {
                    int monthNum;
    
                    char months[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"};
    
                    cout<<"Please enter the number of the month"<<endl;
                    cin>>monthNum;
    
                    cout<<months[monthNum][10];
    }
    
    int lowestRainfall(int rainfall[12][2])
    {
               int low;
    
                     for(int a=0;a<12;a++)
    
            {
                    sum+=rainfall[a][1];
                    months[monthNum][10];
    
    
            }
                    cout<<"The month with the lowest rainfall is "<<low<<endl;

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    well i see you have not declared function lowest rainfall

  3. #3
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    There are a number of problems with this code that can very easily be alleviated using std::string and std::vector. I'm curious as to why you chose a vanilla C/C++ approach instead of using standard containers that can handle all of what you're trying to do and make it very, very easy.

    As for the very first error, it's talking about this:

    Code:
    void getData(int rainfall[12][2])
    I don't know the exact reasoning behind the compiler error but basically you can't declare an array like that as a function parameter. You'd be better off typedefing it and passing a pointer to it although I can't say that's even correct because I don't use C.

    You'd be in much better shape defining a vector<vector<int> >. You can access it the sameway (using subscribt operators, [x][y]) but storage and handling of it is a lot easier.

    You have functions which are returning char's but it looks like you're expecting whole strings. This won't work. At best you'll get the first character from a month string.

    If you change your function parameter lists and make use of STL's containers you'll find that most, if not all, of those errors will disappear. Your code will be more maintainable, easier to read and easier to work with.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you can pass array like that as a parameter? this from a previous post

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void AlterArray(int myarray[5])
    {
        myarray[2] = 60;
    }
    
    
    int main(int argc, char *argv[])
    {
        int myarray[5] = { 10,20,30,40,50 };
    
        printf("my array 3rd element: before call > %d \n",myarray[2]);
    
        AlterArray(myarray);
    
        printf("my array 3rd element: After call > %d \n",myarray[2]);
    
        system("PAUSE");
        return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by leeor_net
    As for the very first error, it's talking about this:
    No, it isn't. It is complaining that the parameter name of findMonth is months, but a local variable was declared with the same name in the scope of the function.

    Quote Originally Posted by leeor_net
    Code:
    void getData(int rainfall[12][2])
    I don't know the exact reasoning behind the compiler error but basically you can't declare an array like that as a function parameter.
    It is true that "you can't declare an array like that as a function parameter", but that does not declare an array as the function parameter. Rather, that declares a pointer to an array of 2 ints as a parameter.

    Quote Originally Posted by leeor_net
    You'd be in much better shape defining a vector<vector<int> >. You can access it the sameway (using subscribt operators, [x][y]) but storage and handling of it is a lot easier.
    Actually, I would prefer to define a structure to store the highest/lowest rainfall for a month, or perhaps just use std::pair<int, int>. Then one can use an array of 12 of these structures. A vector might be suitable, but the number of months is fixed and quite limited.

    In the case of the months array, it would indeed be simpler to use std::string.
    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

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think a big problem is that none of these functions should be prompting for input and displaying output. That is the task of the main function. These should just compute a result based on the arguments and return it.

    Code:
    char findMonth(int months)
    {
                    int monthNum;
    
                    char months[12][10]={
                             "January","February","March","April","May","June",
                             "July","August","September","October","November","December"
                    };
    
                    cout<<"Please enter the number of the month"<<endl;
                    cin>>monthNum;
    
                    cout<<months[monthNum][10];
    }
    Although the signature appears to be incorrect, the intent of this function is to accept the month number from the caller (that's why the function has the argument) and return the name of the month to the caller.

    Code:
    #include <iostream>
    
    //NB month is zero-based (January = 0, etc)
    const char* findMonth(int month)
    {
        if (month < 0 || month > 11) month = 12;
        static const char* monthNames[13] = {
            "January","February","March","April","May","June",
            "July","August","September","October","November","December",
            "Invalid month number"
        };
        return monthNames[month];
    }
    
    //it's up to the caller to decide what to use the function for:
    int main()
    {
         int m;
         while (std::cout << "Enter month number: " && std::cin >> m) {
             std::cout << "Month #" << m << " is " << findMonth(m - 1) << '\n';
        }
    }
    Similarly for the other functions. I even think that the intended usage of the findMonth functions is:

    Code:
    std::cout << "The month with the lowest rainfall is: " << findMonth(findLowestRainfall(data)) << '\n';
    //etc.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM