Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #include<ctime>
- using namespace std;
- int ** build2DArray(const int students, const int grades);
- void getGrades(int ** gPtr, const int students, const int grades);
- void displayGrades(int ** gPtr, const int students, const int grades);
- void getAverage(int ** gPtr, const int students, const int grades);
- int main()
- {
- int students, grades;
- int ** gradePtr;
- cout << "How many students are there? ";
- cin >> students;
- cout << "Enter how many grades there are. (Between 1 and 15) ";
- cin >> grades;
- gradePtr = build2DArray(students, grades);
- getGrades(gradePtr, students, grades);
- displayGrades(gradePtr, students, grades);
- getAverage(gradePtr, students, grades);
- system("pause");
- return 0;
- }
- int ** build2DArray(const int students, const int grades)
- {
- int** ptr;
- if (grades <= 0)
- {
- ptr = NULL;
- return ptr;
- }
- ptr = new int*[students];
- for (int i = 0; i < students; i++)
- ptr[i] = new int[grades];
- return ptr;
- }
- void getGrades(int ** gPtr, const int students, const int grades)
- {
- srand(time(NULL));
- for (int i = 0; i < students; i++)
- for (int*p = gPtr[i]; p < &gPtr[i][grades];)
- *p++ = rand() % 100 + 1;
- }
- void displayGrades(int ** gPtr, const int students, const int grades)
- {
- cout << "STUDENT GRADES" << endl;
- for (int r = 0; r < students; r++)
- {
- cout << "Student " << r + 1 << " Grades: ";
- for (int c = 0; c < grades; c++)
- cout << setw(4) << gPtr[r][c];
- cout << endl;
- }
- cout << endl << endl;
- }
- void getAverage(int ** gPtr, const int students, const int grades)
- {
- float sum = 0.0;
- float average;
- cout << "AVERAGE OF GRADES FOR EACH STUDENT" << endl;
- cout << setprecision(1) << fixed;
- for (int r = 0; r < students; r++)
- {
- cout << "Student " << r + 1 << " Grades: ";
- sum = 0;
- for (int c = 0; c < grades; c++)
- sum += gPtr[r][c];
- average = (sum / grades);
- cout << setw(5) << average;
- cout << endl;
- }
- cout << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement