Dymanic Array Question

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
//What's wrong with this code ?!!
  #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
            gradeGPA[i][j] ='F';

        }
    }
for(int i=0; i<studentsNum;i++)
        {
            delete []gradeGPA[i];

        }
         delete []gradeGPA;

    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=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++)
        {
          cout<<GPA(Matrix,studentsNum,subjectsNum)<<endl;
        }
    }

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

        }
         delete []Matrix;


    return 0;
}
Starting at line 76, you start to allocate memory to Matrix but you don't continue to do so in a for-loop. Remember you have to allocate memory for the columns as well

1
2
3
4
5
6
7
8
9
10
Matrix= new  double*[studentsNum]; //Allocate Memory

    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;
        }
    }


It might not be the only problem, but that's a start.
Last edited on

As PrivateRyan has said, you havent allocated your 2D array correctly.

i.e.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
using namespace std;

int main()
{
	double **Matrix;

	// allocate a 2d array, below example
	// is like allocating Matrix[2][3]
	Matrix = new double*[3];
	for (int i = 0; i < 3; ++i)
		Matrix[i] = new double[2];

	// ..... do stuff

	// release memory
	for (int i = 0; i < 3; ++i)
		delete[] Matrix[i];
	delete[] Matrix;

	return 0;
}
Thank you all for this helpful information.
But what is wrong in the function ?!!

Read above, you haven't allocated your 2D array correctly - as PrivateRyan stated it would be a good start to get that corrected first and go from there.
yeah ,I did that and I've already corrected what you all said and allocate 2D array correctly but still the program stopped working unexpectedly
Thanks in advance for your reply...

You need to be more specific instead of just saying "program stopped working unexpectedly" then someone may be able to help you further.

Have you debug stepped your code to discover where or what is failing?
I don't know where or what failed this code so I didn't specify as I don't know where is the error
yes , I've debugged the code but I don't reach a useful result...
Thank You again for helping me and sorry if I waste your time...

Not wasting my time at all, so no worries there.

Well i'm off to sleep now but what I would suggest is for you to re-post your updated code where you fixed the allocation of the 2d arrays then if someone comes across your post they may be able to help you.

If not, I will be around the forums tomorrow :)

OK , I'll do that
Thank You and good night :)
Topic archived. No new replies allowed.