Good evening;

I am having a final exam next week and I'm having an issue with this program.

the program should user receive from the user 6 marks for each one of four students and then compute the average and finally show the index of the student who has the highest average.(using a dynamic two Dimensional array).


Now, I have no errors at all but, the programme does not show the correct index .In fact, it shows a constant number no matter what numbers I enter.

I appreciate the help.

#include<iostream>
using namespace std;
int main(){
double **marks2d;
double stud_row=4,Quiz_Col=6;
double sumup=0;double *avg=new double[stud_row];
marks2d=new double *[stud_row];
for(int i=0;i<stud_row;i++)
marks2d[i]=new double[Quiz_Col];
for(int y=0;y<stud_row;y++)
{cout<<"Enter 6 marks for student number#"<<y+1<<endl;
for(int x=0;x<Quiz_Col;x++)
{cin>>marks2d[y][x];
sumup+=marks2d[y][x];}
avg[y]=sumup/Quiz_Col;}
int maxindex=0;
double max=avg[0];
for(int t=0;t<stud_row;t++)
{if (avg[t]>max){maxindex=t;max=avg[t];}}
cout<<"\n\n\n\n\n\nthe student with the highest average is stud number:"<<maxindex+1<<"\n\n\n\n";return 0;}

Recommended Answers

All 5 Replies

Your error is here

double max=avg[0];

for(int t=0;t<stud_row;t++)

if (avg[t]>max)
{
  maxindex=t;
  max=avg[t];
}

If you don't see it ask again.

Are you trying to win an obfuscated code contest or something? I'm usually not a friend of unnecessary whitespace, but this is overdoing it.

A readable version after sending it through a formatter:

#include <iostream>
using namespace std;

int main()
{
    double** marks2d;
    double stud_row=4,Quiz_Col=6;
    double sumup=0;
    double* avg=new double[stud_row];
    marks2d=new double*[stud_row];
    for (int i=0; i<stud_row; i++)
        marks2d[i]=new double[Quiz_Col];
    for (int y=0; y<stud_row; y++)
    {
        cout << "Enter 6 marks for student number#" << y+1 << endl;
        for(int x=0; x<Quiz_Col; x++)
        {
            cin >> marks2d[y][x];
            sumup+=marks2d[y][x];
        }
        avg[y]=sumup/Quiz_Col;
    }
    int maxindex=0;
    double max=avg[0];
    for(int t=0; t<stud_row; t++)
    {
        if (avg[t]>max)
        {
            maxindex=t;
            max=avg[t];
        }
    }
    cout << "\n\n\n\n\n\nthe student with the highest average is stud number:" << maxindex+1 << "\n\n\n\n";
    return 0;
}

Lol yeah, The code is impossible to read, as is posted.

I seem to have misread it. I can't understand what you are doing

Why is this a double array

for(int y=0; y<stud_row; y++)
{
   cout << "Enter 6 marks for student number#" << y+1 << endl;
   for(int x=0; x<Quiz_Col; x++)
   {
      cin>>marks2d[y][x];
      sumup+=marks2d[y][x];
   }
   avg[y]=sumup/Quiz_Col;
}

amazingly i do not see the error!!!!!!!!!!
what is the problem of that part of the code????

Please read second comment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.