You are using a version without Ads of this website. Please, consider donating:

### Need help with using multidimensional arrays with functions

First off I would like to introduce myself! This is my first post on this website. My name is Drew, and I'm a computer science major and Morehead State University in Morehead Kentucky. AND I NEED HELP!

This is the program I have written so far.

 ``123456789101112131415161718192021222324252627282930`` ``````//Exam1HandsOn.cpp #include #include using namespace std; int displayStudentScoresandAvg(int* intPtr, int size1, int size2){ int sum; for(int i = 0; i < size1; i++) { cout << setw(10) << "Student " << (i + 1); sum = 0; for(int j = 0; j < size2; j++) { cout << setw(11) << intPtr[i][j]; sum = sum + intPtr[i][j]; } cout << setprecision(2) << fixed << setw(11) << static_cast(sum) / 2; cout << endl; } } int main() { const int SIZE1 = 3; const int SIZE2 = 2; int scores[SIZE1][SIZE2]= {{90, 89},{92, 98},{99, 88}}; displayStudentScoresandAvg(scores, SIZE1, SIZE2); return 0; } ``````

The full program will be longer, but I need to be able to figure out how to get my arrays and functions working together before I can really move on. I believe if I can figure out how to get this first part working I should be able to get the rest of the program written.

I sort of based how I wrote this program on an example my teacher provided. However in the example my teacher was only using single dimension array, and I believe that's why I'm having this problem. Here is the example code my teacher gave us.
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````// pointers14b.cpp: pointers, arrays, and functions #include using namespace std; void getNumbers(int* intPtr, int size) { for (int i = 0; i < size; i++) { cout << "Enter number for Cell " << (i + 1) << ": "; cin >> intPtr[i]; } } void displayNumbers(int* intPtr, int size) { for (int i = 0; i < size; i++) { cout << intPtr[i] << " "; } cout << endl; } int addNumbers(int* intPtr, int size) { int total = 0; for (int i = 0; i < size; i++) { total = total + *intPtr; intPtr++; } return total; cout << endl; } int main() { const int SIZE= 3; int numbers[SIZE]; getNumbers(numbers, SIZE); cout << "You have entered the following numbers:" << endl; displayNumbers(numbers, SIZE); cout << "Total: " << addNumbers(numbers, SIZE) << endl; return 0; }``````

You can see that our syntax is similar. I thought I could simply use another size decelerator for the second subscript element for a multidimensional array, but that seems to have done something to make it not work.

Here's the error I get when trying to compile Exam1HandsOn.cpp in Visual Studio 2012 command prompt.

 ```Exam1HandsOn.cpp Exam1HandsOn.cpp(11) : error C2109: subscript requires array or pointer type Exam1HandsOn.cpp(12) : error C2109: subscript requires array or pointer type Exam1HandsOn.cpp(27) : error C2664: 'displayStudentScoresandAvg' : cannot convert parameter 1 from 'int[3][2]' to 'int *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast```

Lines 11-12 in Exam1HandsOn.cpp are as folows (located in the double for loop for displayStudentScoresandAvg()) :
 ``12`` ``````cout << setw(11) << intPtr[i][j]; sum = sum + intPtr[i][j];``````

I believe this is the problem my multidimensional array is causing a problem. Because in my teachers example he uses a similar style but only has to use 1 for loop and has the single dimension array

Line 27 is the function call to displayStudentScoresandAvg() in int main(). I'm not really sure that that error is about. But I think it might have something to do with line 14 of the program in displayStudentScoresandAvg(), which is as follows:
`cout << setprecision(2) << fixed << setw(11) << static_cast<double>(sum) / 2;`

I'm really more concerned with the first 2 errors, but it would be nice to figure out what is up with this too.

I will greatly appreciate ANY help. I have to turn this in tomorrow(10/2/2013) at 3:00PM for a test grade. Asking for help on here is pretty much my only hope.
Hey, welcome to the forum.

The first thing you should note is that your function (correctly) takes a pointer to an integer, which can be treated like a 1D array. The way the compiler handles the 2D array is actually by using a 1D array and a formula. Just google search "C++ 1D to 2D array formula" and you'll find it.
Topic archived. No new replies allowed.