Using 2 D arrays

Discussion in 'C' started by rafta, Nov 11, 2009.

  1. rafta

    rafta New Member

    Joined:
    Nov 3, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I am trying to code a program that asks user to enter food consumption everyday for 3 monkeys for a week. Then calculate average food consumed.
    min and max during that week.

    here is my code so far:

    Code:
    #include <iostream>
    #include<iomanip>
    
    using namespace std;
    
    //declares a new data type
    //which is a 2 dimensional array of floats
    
    const int NumofMonkeys =3;
    const int NumofDays= 7;
    
    
    
    //creat a new data type 2D array of doubles
    
    typedef double MealType[NumofMonkeys][NumofDays];
    
    void getMeal(MealType, int &, int &); //get the meal into the array
    void printMeal(MealType, int, int); //print data as a table
    
    
    int main ()
    
    {
    int rows,
    cols;
    MealType mealTable;
    getMeal(mealTable, rows, cols);
    printMeal(mealTable,rows,cols);
    
    system("pause");
    return 0;
    
    
    }
    void getMeal(MealType table, int &NumofMonkeys, int &NumofDays)
    { for (int row =0; row<NumofMonkeys; row++)
    {for (int col =0; col<NumofDays; col++)
    cout<<"Please Enter Number of monkeys"<<endl;
    cout<<"Please Enter number of the Day from 1to7:"<<endl;
    cin>>MealType[NumofMonkeys][NumofDays];
    }
    }
    where are the problems???
     
  2. johny_1015_1981

    johny_1015_1981 New Member

    Joined:
    Oct 5, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    I am not a c++ programmer. even though i am trying to answer your question..

    1. you have declared a function but did define it..
    void printMeal(MealType, int, int);

    prinMeal required definition. otherwise when you will try to compile you will get
    Unresolve symbol message.

    2. Very important one..
    In the function getMeal check the last line before two (}})

    cin>>MealType[NumofMonkeys][NumofDays];

    you have defined MealType as a datatype but not a variable. but you are trying to use as a variable.
    that is why you get a message like this..
    "Illegal use"

    you are suppose to use table instead of MealType.

    That is all i have noticed.

    Regards,
    johny
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice