Dymanic Array Function

A program to calculate students grades.the user
enter number of subjects and number of students then create 2D array of type Double for them.the user enter the grade of each student for each subject then
create a function which takes the 2D array of student grades and return 2D array of type String which contains the GPA of each subject for each
student

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//what's is wrong with string function ?!
  #include <iostream>

using namespace std;



string** GPA(double**matrix,int studentsNum,int subjectsNum)
{
     string** gradeGPA ;
     gradeGPA = new string*[studentsNum];

    for(int i=1;i<=studentsNum;i++)
    {
        for(int j=1;j<=subjectsNum;j++)
        {
          if (matrix[i][j] >=90)
          {
              gradeGPA[i][j]="A+";

          }
         else  if (matrix[i][j] >=85 && matrix[i][j]<90)
          {
              gradeGPA[i][j]='A';
          }
          else if (matrix[i][j] >=80 && matrix[i][j]<85)
          {
              gradeGPA[i][j]="B+";
          }
           else if (matrix[i][j] >=75 && matrix[i][j]<80)
          {
              gradeGPA[i][j]='B';
          }
          else  if (matrix[i][j] >=70 && matrix[i][j]<75)
          {
              gradeGPA[i][j]="C+";
          }
          else  if (matrix[i][j] >=65 && matrix[i][j]<70)
          {
              gradeGPA[i][j]='C';
          }
           else if (matrix[i][j] >=60 && matrix[i][j]<65)
          {
              gradeGPA[i][j]="D+";
          }
           else if (matrix[i][j] >=50 && matrix[i][j]<60)
          {
              gradeGPA[i][j]='D';
          }

         else if (matrix[i][j] <50)
         {
            gradeGPA[i][j] ='F';

         }

        }
    }


    return gradeGPA;
}
int main()
{
    int subjectsNum,studentsNum;
    double grade;
    double **Matrix; //to dynamically allocate a 2D array

    cout<<"# of students: ";
    cin>>studentsNum;
    cout<<"# of subjects: ";
    cin>>subjectsNum;

    Matrix= new  double*[studentsNum]; //Allocate Memory
    for(int i=0;i<studentsNum;i++){

        Matrix[i]= new double [subjectsNum];
        }

    for(int i=1;i<=studentsNum;i++)
    {
        for(int j=1;j<=subjectsNum;j++)
        {
          cout<<"Enter grade of Subject#"<<j<<" for Student#"<<i<<": ";
          cin>>grade;
        }
    }
for(int i=1;i<=studentsNum;i++)
    {
        for(int j=1;j<=subjectsNum;j++)
        {
         string **GPAresult = GPA(Matrix,studentsNum,subjectsNum);
          cout<<GPAresult<<endl;
        }
    }

//    Deallocation of 2D Array
        for(int i=0; i<studentsNum;i++)
        {
            delete []Matrix[i];

        }
         delete []Matrix;


    return 0;
}
I don't know everything thats wrong here but looking over the code these are things that seemed wrong to me.
First your matrix is like a 2d array with size [studentNum][subjectNum]. Shouldnt your for loops in line 13/15 start with 0 as [0][0] would be the first element in this array. Also the last element in the array should be [studentNum-1][subjectNum-1]. So having something like i <= studentNum would make it go out of bound.

Secondly, in your main ,what is the point of grade variable.? Should it not be matrix[i][j].
Also why do you have
string **GPAresult = GPA(Matrix,studentsNum,subjectsNum);
inside the 2 for loops.
Topic archived. No new replies allowed.