201k views
4 votes
Create a class ""Student"" that will hold an array of grades and has a method to calculate the average. It would also have a method to return any particular grade number (1-14).

User Scout
by
6.2k points

1 Answer

3 votes

Answer:

#include <iostream>

#include<time.h>

using namespace std;

class Student

{

public :

int grades[20]; //array of grades

double average(int arr[],int n) //method to calculate average of array of grades passed

{

int sum=0;

for(int i=0;i<n;i++) //loop to calculate sum of all grades

sum=sum+arr[i];

return sum/n; //average is sum of items/number of items

}

int get_grade() //method to return grade number(1-14)

{

srand(time(NULL)); //method to generate different random number each time

int num = rand() % 14 + 1; //method to generate random number b/w 1-14

return num;

}

};

int main()

{

Student s;

int grade[]={5,3,9,8,10};

cout<<"Average is : "<<s.average(grade,5);

return 0;

}

OUTPUT :

Average is : 7

Step-by-step explanation:

A class Student is created in which a method named average is there to calculate average of the grades passed and a method to generate a grade number b/w 1-14.

User Kfrosty
by
6.7k points