Question

In: Computer Science

In C++: 5) Suppose you have the following declaration:    struct student { string name;   int exams...

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)

Solutions

Expert Solution

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


Related Solutions

Consider the following declaration: typedef struct{                                  &nbsp
Consider the following declaration: typedef struct{                                          int A;                                          char B[10];                                          float C;                                          char D;                                  }rectype;                                   typedef rectype matrix[121][4][5];                                   matrix A1; Compute the address of element A1[120][3][3] given the base address at 2000.
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int...
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int x) { return (x * 3); } 2) What is C syntax to declare two variables, one called num of integer type and another called farray which is an array of 10 floating point numbers? 3) Write a C function array_max(int a[], int len) that takes an integer array & its length as its parameters and returns the largest value in the array. 4)...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next;...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next; }; ListNode *head; // List head pointer Assume that a linked list has been created and head points to the first node. Write code that traverses the list displaying the contents of each node’s value member Now write the code that destroys the linked list
c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year,...
c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year, int day. Make another struct Product that would contain the following variables: 1- Product name. 2- An array for the product ingredients. Set its max size to 3. 3- Product date of expiration. Use the date struct you made. Use those structs in entering the data for two products of your choice. Make a function printProduct(Product product) that prints out the contents of the...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;   ...
int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;    int playerNum;    int numOfTouchDowns;    int numOfcatches;    int numOfPassingYards;    int numOfReceivingYards;    int numOfRushingYards;    int ret;    int num = 0;    ifstream inFile;    ofstream outFile;    ret = openFile(inFile);    if (ret)        getData(inFile, bigGiants, numberOfPlayers);    else        return 1;    /// replace with the proper call to getData    do    {       ...
Create a struct MenuItem containing fields for name (a string) and price (a float) of a...
Create a struct MenuItem containing fields for name (a string) and price (a float) of a menu item for a diner. Create a ReadItem() function that takes an istream and a MenuItem (both by reference) and prompts the user for the fields of the MenuItem, loading the values into the struct's fields. Create a PrintItem() function that takes an output stream (by reference) and a MenuItem (by value) and prints the MenuItem fields to the stream in a reasonable one-line...
Book SerialNum : String - Name: String – Author : String PublishYear: int - Edition:int Status...
Book SerialNum : String - Name: String – Author : String PublishYear: int - Edition:int Status : boolean + Book() Book(String, String, String, int , int)+ + setName(String) : void + setSerialNum(String) : void + setAuthor(String) : void + setEdition(int) : void + setYear(int) : void + getName():String + getAuthor():String + getYear(): int + getSerialNum(): String + getEdition():int + setAvailable() : void + setUnavailable(): void checkAvailability(): boolean ----------------------------------------------------------------------------------- Library Name : String Address: String Static NUMBEROFBOOKS: int BooksAtLibrray : ArrayList...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT