2D Arrays

Nov 9, 2009 at 6:35pm
I am getting confused on 2d arrays while calling them on functions.

const int row =3;
const int cols =7

void showArray(int [] [cols], int) //prototype function

how can I define this function to print on screen the values of rows and cols.




Nov 9, 2009 at 7:49pm
If the second parameter holds the number of rows, you'll need two nested for loops
Nov 9, 2009 at 9:20pm
here is my code so far:

#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];
}
}




what is wrong in it?????
Nov 9, 2009 at 9:33pm
Please use [code][/code] tags
cin>>MealType[NumofMonkeys][NumofDays]; MealType is (of course) a type, not an array
Nov 9, 2009 at 10:24pm
actually it should be;

1
2
cin>>NumofMonkeys;
cin>>NumofDays;

but how can I print it on screen as a 2D array...I am confused about defining this function


void printMeal(MealType, int, int); //print data as a table

also this function is not working:
void getMeal(MealType table, int &NumofMonkeys, int &NumofDays)

what I am doing wrong????
Nov 9, 2009 at 10:26pm
basically, user enters food consumption for 3 monkeys for everyday of a week.

once I get this right then i want to calculate average food taken a day of the week.

Topic archived. No new replies allowed.