0% found this document useful (0 votes)
7 views2 pages

Lastname Lab4

The document is a C++ program that calculates and displays statistics about student grades, including average, highest, lowest, and the number of students who passed or failed. It includes input validation for the number of students and their grades, ensuring grades are between 0 and 100. The program utilizes functions to perform calculations on the grades array and dynamically allocates memory for storing grades.

Uploaded by

dhaved.escover
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Lastname Lab4

The document is a C++ program that calculates and displays statistics about student grades, including average, highest, lowest, and the number of students who passed or failed. It includes input validation for the number of students and their grades, ensuring grades are between 0 and 100. The program utilizes functions to perform calculations on the grades array and dynamically allocates memory for storing grades.

Uploaded by

dhaved.escover
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

using namespace std;

// Function prototypes
float getAverage(int grades[], int size);
int getHighest(int grades[], int size);
int getLowest(int grades[], int size);
int countPasses(int grades[], int size);

int main() {
int numStudents;

cout << "Enter the number of students: ";


cin >> numStudents;

// Input validation for number of students


while (numStudents <= 0) {
cout << "Please enter a positive number of students: ";
cin >> numStudents;
}

int* grades = new int[numStudents];

// Input grades with validation


for (int i = 0; i < numStudents; i++) {
cout << "Enter grade for student " << (i + 1) << ": ";
cin >> grades[i];
while (grades[i] < 0 || grades[i] > 100) {
cout << "Invalid grade. Enter a grade between 0 and 100: ";
cin >> grades[i];
}
}

// Display grades
cout << "\nGrades entered: ";
for (int i = 0; i < numStudents; i++) {
cout << grades[i] << " ";
}
cout << endl;

// Display average, highest, lowest, passes, and fails


cout << "Average grade: " << getAverage(grades, numStudents) << endl;
cout << "Highest grade: " << getHighest(grades, numStudents) << endl;
cout << "Lowest grade: " << getLowest(grades, numStudents) << endl;
cout << "Number of students who passed: " << countPasses(grades, numStudents) << endl;
cout << "Number of students who failed: " << (numStudents - countPasses(grades, numStudents

delete[] grades;
return 0;
}

// Function definitions
float getAverage(int grades[], int size) {
float sum = 0;
for (int i = 0; i < size; i++) {
sum += grades[i];
}
return sum / size;
}

int getHighest(int grades[], int size) {


int highest = grades[0];
for (int i = 1; i < size; i++) {
if (grades[i] > highest) highest = grades[i];
}
return highest;
}

int getLowest(int grades[], int size) {


int lowest = grades[0];
for (int i = 1; i < size; i++) {
if (grades[i] < lowest) lowest = grades[i];
}
return lowest;
}

int countPasses(int grades[], int size) {


int count = 0;
for (int i = 0; i < size; i++) {
if (grades[i] >= 75) count++;
}
return count;
}

You might also like