Question

In: Computer Science

In C++ Modify the program #1 to allow the user to enter name-score pairs. For each...

In C++

Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it a class.  You will need a destructor because you are using dynamic arrays.

I need your help! This is fundamental II. Thank you

Solutions

Expert Solution

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

//function declaration
void array_sort(double *, string *, int);
double calculate_avg(double *, int);

int main()
{
//variable to store number of student test
int no_of_test;

cout << "Enter Number of test : ";
cin >> no_of_test;

//dynamic array to store student name and their test score
double *test_score = new double[no_of_test];
string *student_name = new string[no_of_test];

//fetch values into array using loop
for(int counter = 0; counter < no_of_test; counter++)
{
cout << "Student " << counter + 1 << " name: ";
  
//use of pointer notation
cin >> *(student_name + counter);

cout << "Test score: ";
cin >> *(test_score + counter);

//check input is valid or not
while(*(test_score + counter) < 0)
{
  
cout << "ERROR! Negative values not allowed!";
cout << " Enter again!\n";
cin >> *(test_score + counter);
}
}

//call array_sort() for sorting array
array_sort(test_score, student_name, no_of_test);

//format output
cout << fixed << setprecision(1);

//display data of arrays
cout << "\n";
cout << "Sorted test scores are: \n";
cout << "Name\t\tScore\n";
for(int counter = 0; counter < no_of_test; counter++)
{
cout << *(student_name + counter) << "\t\t";
cout << *(test_score + counter) << endl;
}

//for average
cout << "\n\nAverage of " << no_of_test << " tests is: ";
//call calculate_avg() to calculate average
cout << calculate_avg(test_score, no_of_test);

//return 0 to mark successful termination
return 0;
}

//sorting array by array_sort()
void array_sort(double *ptr, string *name_ptr, int array_size)
{
//temporary variable to help in swapping
double temp;
string temp_name;

//boolean variable that will control when array is done sorting
bool swap = true;

while(swap)
{
swap = false;

//loop on all elements
for(int counter = 0; counter < array_size - 1; counter++)
{
//check if element is > current element or not and if yes then perform swap
if(*(ptr+counter) > *(ptr+counter+1))
{
  
temp = *(ptr+counter+1);
temp_name = *(name_ptr+counter+1);

*(ptr+counter+1) = *(ptr+counter);
*(name_ptr+counter+1) = *(name_ptr+counter);

*(ptr+counter) = temp;
*(name_ptr+counter) = temp_name;

//update boolean variable after done
swap = true;
}
}
}
}

//calulate average
double calculate_avg(double *ptr, int array_size)
{
double total = 0.0;

//loop on values of array
for(int counter = 0; counter < array_size; counter++)
{
total =total + *(ptr + counter);
}

//typecast
return (double)total/array_size;
}


Related Solutions

In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
Write a C++ program that : 1. Allow the user to enter the size of the...
Write a C++ program that : 1. Allow the user to enter the size of the matrix such as N. N must be an integer that is >= 2 and < 11. 2. Create an vector of size N x N. 3. Call a function to do the following : Populate the vector with N2 distinct random numbers. Display the created array. 4. Call a function to determines whether the numbers in n x n vector satisfy the perfect matrix...
Modify the Tip Calculator app to allow the user to enter the number of people in...
Modify the Tip Calculator app to allow the user to enter the number of people in the party. Calculate and display the amount owed by each person if the bill were to be split evenly among the party members. Code: ----------------TipCalculator.java----------------------- import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class TipCalculator extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("TipCalculator.fxml")); Scene scene = new Scene(root); // attach scene graph to scene...
create a program that will allow the user to enter a start value from 1 to...
create a program that will allow the user to enter a start value from 1 to 5, a stop value from 10 to 12 and a multiplier from 1 to 4. the program must display a multiplication table from the values entered. for example if the user enters: start 2, stop 10 and multiplier 3, the table should appear as follows: 3*2=6 3*3=9 3*4=12 . . . 3*10=30
Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Using C# Design and implement a program (name it CheckPoint) that prompts the user to enter...
Using C# Design and implement a program (name it CheckPoint) that prompts the user to enter the x-coordinate then y-coordinate of a point (in a Cartesian plane) as integer values. The program prints out the entered values followed by the location of the point on the plane. The possibilities for a point are: the origin point, on the x-axis, on the y-axis, in the first quadrant, in the second quadrant, in the third quadrant, or in the fourth quadrant. Format...
Using Perl; Part 1: Allow the user to enter a full name in the “first last”...
Using Perl; Part 1: Allow the user to enter a full name in the “first last” format Print just the first name Print just the last name Print the name in “last, first” format Print the entire name out in all capital letters Use a single print statement to print out the first name on one line and the last name on the next line. There should be 5 print statements generating 6 lines of output. Part 2: Enter a...
Write a C program that performs the following: Asks the user to enter his full name...
Write a C program that performs the following: Asks the user to enter his full name as one entry. Asks the user to enter his older brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have any older brother). Asks the user to enter his younger brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT