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() <<...
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 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...
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...
Forms often allow a user to enter an integer. Write a program that takes in a...
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or any string with a non-integer character, the output is: no PYTHON 3
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
1/Write a C program that asks the user to enter 5 nums and adds each number...
1/Write a C program that asks the user to enter 5 nums and adds each number to a total. The program should then display the total. To add to the total, the program must call the function void sum(int value, int *ptotal) passing the value the user entered and the address of the total (a local variable in main). The function should add the value to the total. Note that the function is void and does not return anything. Don't...
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If...
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If the age is greater than 21 print "welcome," and if the age is less than 21 print "sorry." Use input validation to make sure the birthdate was entered correctly.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT