In: Computer Science
In C++:
5) Suppose you have the following declaration:
struct student
{
string name;
int exams [5];
double average;
};
a) Declare a pointer to the structure student and use it to create a dynamic structure.
b) Ask the user to enter the name of a student and his 5 exam scores.
c) Calculate the average of the 5 exams and assign it to the average.
d) Delete the dynamic structure.
d) Create a dynamic array of 10 students.
e) Ask the user to enter the names of all student and their 5 exam scores.
f) For each student, calculate the average of the 5 exams and assign it to the average.
g) Delete the dynamic array.
6) Assume you have the following declaration (in main) :
int num[13] [7];
Assume that array num is filled completely. Write functions to perform each of the following:
a) A function that prints all elements in the array that are greater than 80 or less than 10.
b) A function that finds the largest number in the array and returns its subscript.
c) A function that finds and returns the average of all the numbers in the array.
d) A function that print the entire array in a neat table, but starting with the last row and proceeding in reverse order to the first.
7) Fill in the blanks to declare “Student’s” constructor taking two parameters and initializing its private members: names and dateOfBirth.
Student::Student( string x, Birthday bo)
___________: name (_______________) x,
dateOfBirth(bo) {
}
(answer is : for the first blank and x for the second blank)
5.
struct student
{
string name;
int exams [5];
double average;
};
// Declare a pointer to the structure student and
use it to create a dynamic structure.
student *stud = new student;
// Ask the user to enter the name of a student and his
5 exam scores.
cout<<"Enter name of the student : ";
cin>>stud->name;
for(int i=0;i<5;i++)
{
cout<<"Enter exam
score-"<<(i+1)<<" : ";
cin>>stud->exams[i];
}
// Calculate the average of the 5 exams and assign
it to the average
stud->average = stud->exams[0];
for(int i=1;i<5;i++)
stud->average +=
stud->exams[i];
stud->average = stud->average/5;
// Delete the dynamic structure.
delete stud;
// Create a dynamic array of 10 students.
student *students = new student[10];
// Ask the user to enter the names of all student
and their 5 exam scores.
for(int i=0;i<10;i++)
{
cout<<"Enter name of
student-"<<(i+1)<<" : ";
cin>>students[i].name;
for(int j=0;j<5;j++)
{
cout<<"Enter exam scores-"<<(j+1)<<" : ";
cin>>students[i].exams[j];
}
}
// For each student, calculate the average of the 5
exams and assign it to the average.
for(int i=0;i<10;i++)
{
students[i].average =
students[i].exams[0];
for(int j=1;j<5;j++)
{
students[i].average += students[i].exams[i];
}
students[i].average =
students[i].average/5;
}
// Delete the dynamic array.
delete [] students;
6.
Assume you have the following declaration (in main) :
int num[13] [7];
// function that prints all elements in the array that are
greater than 80 or less than 10.
// array is num and rows is the number of rows in num and cols is
the number of columns in num
void printSelectedElements(int num[][7], int rows, int cols)
{
// loop over rows
for(int i=0;i<rows;i++)
{
// loop over columns
for(int j=0;j<cols;j++)
{
// if element at i,j > 80 or < 10 display it
if(num[i][j]
> 80 || num[i][j] < 10)
cout<<num[i][j]<<" ";
}
}
}
// function that finds the largest number in the array and
returns its subscript.
// array is num , rows is the number of rows in num, cols is number
of columns in num
// the subscript of largest number is returned in maxRow and maxCol
which are passed by reference
void largestNumber(int num[][7], int rows, int cols, int
&maxRow, int &maxCol)
{
maxRow = -1, maxCol = -1;
// loop over the rows
for(int i=0;i<rows;i++)
{ // loop over the columns
for(int j=0;j<cols;j++)
{
// if maxRow and maxCol is not set or value at i, j is greater
than current max value, update maxRow and maxCol
if((maxRow == -1
&& maxCol == -1) || (num[i][j] >
num[maxRow][maxCol]))
{
maxRow = i;
maxCol = j;
}
}
}
}
// function that finds and returns the average of all the
numbers in the array.
// array is num, rows is the number of rows in num, cols is number
of columns in num
double array_average(int num[][7], int rows, int cols)
{
double total = 0;
// loop to add the elements of array num
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
total +=
num[i][j];
}
// if number of elements > 0
if((rows*cols) > 0)
return total/(rows*cols); //
calculate and return the average
return 0; // return 0
}
// function that print the entire array in a neat table, but
starting with the last row and proceeding in reverse order to the
first.
// array is num, rows is the number of rows in num, cols is number
of columns in num
void displayReverse(int num[][7], int rows, int cols)
{
// loop over the rows in reverse order
for(int i=rows-1;i>=0;i--)
{
// loop over the columns
for(int j=0;j<cols;j++)
{
cout<<num[i][j]<<" ";
}
cout<<endl;
}
}
7.
private members: names and dateOfBirth.
Student::Student( string x, Birthday bo) : name (x),
dateOfBirth(bo)
{}
// In the above statement the member variables are initialized
through initializer list where name is initialized with the string
x and dateOfBirth is initialized with bo using copy constructor of
string and Birthday class