user1576776
user1576776

Reputation: 3

C++ two dimensional array

I am having some issues with this program for school. I am trying to utilize a two dimensional array and am getting some errors regarding "no conversion from int to int * and '>=' : 'int [5]' differs in levels of indirection from 'int'". I can write it for one dimensional array but am having difficulty with the syntax for the two dimensional. Can someone point me in the right direction in regards to what I may be missing? I have commented out after the btnShow_CLick and it works correctly, it is just the btnGroup_Click where I am obviously missing something.

Thanks to anyone that could possibly share some knowledge.

    static const int NUMROWS = 4;
    static const int NUMCOLS = 5;
    int row, col;
    Graphics^ g;
    Brush^ redBrush;
    Brush^ yellowBrush;
    Brush^ greenBrush;
    Pen^ blackPen;


private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             g = panel1->CreateGraphics();
             redBrush = gcnew SolidBrush(Color::Red);
             yellowBrush = gcnew SolidBrush(Color::Yellow);
             greenBrush = gcnew SolidBrush(Color::Green);
             blackPen = gcnew Pen(Color::Black);
         }

    private: System::Void btnShow_Click(System::Object^  sender, System::EventArgs^  e) {

         panel1->Refresh();

         for (int row = 0; row < NUMROWS; row++)
         {
             for (int col = 0; col < NUMCOLS; col++)
             {
                 Rectangle seat = Rectangle(75 + col * 75,40 + row *40,25,25);
                 g->DrawRectangle(blackPen, seat);
             }
         }
     }

private: System::Void btnGroup_Click(System::Object^  sender, System::EventArgs^  e) {
             int score[NUMROWS][NUMCOLS] = {{45,65,11,98,66},
                                        {56,77,78,56,56},
                                        {87,71,78,90,78},
                                        {76,75,72,79,83}};

         int mean;
         int student;
         mean = CalcMean(score[]);
         txtMean->Text = mean.ToString();

         for (int row = 0; row < NUMROWS; row++)
         {
             for (int col = 0; col < NUMCOLS; col++)
             {
                 student = (row*NUMCOLS) + (col);
                 Rectangle seat = Rectangle(75 + col * 75,40 + (row * 40),25,25);
                 if (score[student] >= 80
                     g->FillRectangle(greenBrush, seat);
                 else if (score[student] >= mean)
                     g->FillRectangle(yellowBrush, seat);
                 else 
                     g->FillRectangle(yellowBrush, seat);
                 g->DrawRectangle(blackPen, seat);
             }
         }
     }

     private: double CalcMean(int score[])
     {
         int sum = 0;
         int students = NUMROWS * NUMCOLS;
         for (int i=0; i< students; i++) sum += score[i];
         return sum / students;
     }

Upvotes: 0

Views: 660

Answers (1)

molyss
molyss

Reputation: 256

Score[student] is equivalent to *(score+student), which is a *int. Instead you should probably use score[row][col], or its equivalent **(score+student) (I strongly advise the array notation). It is also equivalent to *Score[student], but that's pretty ugly.

Plus when I say "it's equivalent", it's only because sizeof int ==sizeof (*int). If you use the pointer logic with another type inside your array you might have funky results.

Upvotes: 1

Related Questions