-1

so here is my code which trying to find the highest avg using different methods I managed to get the first two but I am stuck on the third method which needs a pointer to pointer of type student(struct).

#include <iostream>
using namespace std;
struct student
{
char Name[100];
float avg;

};

student highest(student A[], int size) {
 student max = A[0];
  for (int i = 1; i < size; i++)
    if (max.avg < A[i].avg)
        max = A[i];

 return max;
}
student* highest1(student A[], int size) {
 int highest = 0;
  for (int i = 0; i < size; i++) {
    if (A[highest].avg < A[i].avg)
        highest = i;
}
student* max = &A[highest];

return max;

}
student highest2(student** A, int row, int col) { 
 int maxi=0, maxj=0;
 int i = 0, j = 0;
   for ( i = 0; i < row; i++) {
     for ( j = 0; j < col; j++)
        if (A[maxi][maxj].avg < A[i][j].avg)
            maxi = i; maxj = j;
}

return A[maxi][maxj];

}

int main() {
cout << "please enter teh number of students " << endl;
int x;
int row, col, test;
cin >> x;
student *A =  new student[x];
student B[3][2];
cout << "please enter the name of the "<<x<<" students " << endl;
for (int i = 0; i < x; i++) {
    gets_s(A[i].Name, 100);
}
cout << "please enter the average for" <<x<<" students " << endl;
for (int i = 0; i < x; i++)
    cin >> A[i].avg;
//cout << "the highest grade is " << highest(A,x).avg << endl;
//cout << highest1(A, x)->avg;
highest2(B, 3, 2);
}

so of course after inputting the information into the matrix of the 3x2 i have to check that and that is the issue I don't understand how I can manage to send it to the function, I tried some way but I would get a logical error in which the code would check only 1 row.

Osama
  • 21
  • 5
  • `student B` is a two dimensional, uninitialized struct. What are your intentions here: `highest2(B, 3,2);`? Maybe you need to figure out *why* `B` exists and what its purpose is, because it is hard to tell from the code you posted. – PaulMcKenzie Mar 26 '21 at 13:45
  • Also, a `student**` is **not** a 2 dimensional array of students. So right there, your function declaration is a non-starter. – PaulMcKenzie Mar 26 '21 at 13:52
  • `student**` is a pointer to a pointer – LombardiD Mar 26 '21 at 14:38
  • @Osama I was afraid you were going to say that. This style of C++ is far away from what the industry standard. I understand the didactical appealing of low level manipulation, I just wonder if it is better using C to teach them instead. – Alessandro Teruzzi Mar 26 '21 at 14:58
  • @AlessandroTeruzzi how can i get more of the industrial stuff I am applying for google internship next year as a start off but I don't know from where will I go after that – Osama Mar 26 '21 at 15:12
  • if you are interested in C++ you should get familiar with the C++ standard library and boost. In general, manual memory management is not recommended. – Alessandro Teruzzi Mar 26 '21 at 16:22

1 Answers1

-1

highest2(student** A - this doesn't need double indirection (two stars), just one. The references like A[maxi][maxj] means it will be accessed as a 2-dimensional array. E.g. highest2(student* A

Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24