Question

(C++ Program) 1. Write a program to allow user enter an array of structures, with each...

(C++ Program)

1. Write a program to allow user enter an array of structures, with each structure containing the firstname, lastname and the written test score of a driver.

- Your program should use a DYNAMIC array to make sure user has enough storage to enter the data.

- Once all the data being entered, you need to design a function to sort the data in descending order based on the test score.

- Another function should be designed to display the hightest, lowest and average test scores in all the test takers along with their name.

2. Design a class called XY. Design the class to have two private interger members X and Y.

- Provide a default constructor. Set both X and Y to be default value 0

- Provide a constructor with parameter. Initialize X and Y to be parameter values.

- Provide a copy constructor.

- Provide set and get member functions.

- Overload the + operator to enable adding of two XY instances. You are supposed to add their X part together and their Y part together.

- Overload the == operators to allow comparisons of two XY instances.

- Enable input and output of XY instance through the overloaded >> and << operators.

- You need to design a class header file, an implementation file and a driver file . The driver file needs to test out each of the member functions completely

(show output and comments)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

As per HomeworkLib policy we are supposed to answer the first complete question. Request you to post the second question separately. Thanks

Code for Question 1:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct driver
{
string firstname;
string lastname;
int score;
};

void sortDescending(driver* drivers, int n);
void displayStats(driver* drivers, int n);
void display(driver* drivers, int n);

int main()
{
int n;
driver* drivers = NULL;

cout << "How many drivers? ";
cin >> n;

drivers = new driver[n];

for(int i = 0; i < n; i++)
{
cout << "Driver " << (i+1) << endl;
cout << "\tFirst name: ";
cin >> drivers[i].firstname;
cout << "\tLast name: ";
cin >> drivers[i].lastname;
cout << "\tScore: ";
cin >> drivers[i].score;
cout << endl;
}

cout << "Before sorting the list of drivers is " << endl;
display(drivers, n);


sortDescending(drivers, n);

cout << "After sorting the list of drivers is " << endl;
display(drivers, n);
displayStats(drivers, n);

delete[] drivers;

}

void sortDescending(driver* drivers, int n)
{
int maxIdx;
for(int i = 0; i < n; i++)
{
maxIdx = i;
for(int j = i+1; j < n; j++)
{
if(drivers[j].score > drivers[maxIdx].score)
maxIdx = j;
}

if(maxIdx != i) //swap
{
driver temp = drivers[i];
drivers[i] = drivers[maxIdx];
drivers[maxIdx] = temp;
}
}
}

void display(driver* drivers, int n)
{
cout << left << setw(15) << "First name" << setw(15) << "Last name" << setw(10) << "Score" << endl;
for(int i = 0; i < n; i++)
{
cout << left << setw(15) << drivers[i].firstname
<< setw(15) << drivers[i].lastname <<
setw(10) << drivers[i].score << endl;

}
cout << endl;
}

void displayStats(driver* drivers, int n)
{
int highIdx = 0, lowIdx = 0;
double avg = 0;

for(int i = 0; i < n; i++)
{
if(drivers[i].score > drivers[highIdx].score)
highIdx = i;
else if(drivers[i].score < drivers[lowIdx].score)
lowIdx = i;
avg += drivers[i].score;
}

avg /= n;

cout << drivers[highIdx].firstname << " " << drivers[highIdx].lastname << " has scored highest score of " << drivers[highIdx].score << endl;
cout << drivers[lowIdx].firstname << " " << drivers[lowIdx].lastname << " has scored lowest score of " << drivers[lowIdx].score << endl;


cout << "The average score of all drivers is " << fixed << setprecision(2) << avg << endl;
}

How many drivers? 3 Driver 1 First name: John Last name: Smith Score: 85 Driver 2 First name: Michael Last name: Jackson Scor

Add a comment
Know the answer?
Add Answer to:
(C++ Program) 1. Write a program to allow user enter an array of structures, with each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the...

    In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the term and creates the polynomial c x^n. (This constructor can be given default arguments easily to have a...

  • Design a primary hash table data structure. a. Each hash table has an array of node...

    Design a primary hash table data structure. a. Each hash table has an array of node pointers and can point a head of a linked list. Suppose the node class is given to you. The data type of each node is int. b. Override the default constructor with a constructor that has one default parameter called capacity. What happens to your default constructor? Create some instances of the sequence class using the new constructor in multiple ways. c. Design the...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • You are to write a program (BookExceptionsDemo.java) that will create and, using user input, populate an...

    You are to write a program (BookExceptionsDemo.java) that will create and, using user input, populate an array of instances of the class Book. The class Book is loaded in our Canvas files: Book.java The user will enter a number n (n must be > 0, trap the user until they input a valid value for n), Your program will declare and create an array of size n of instances of the class Book. The user will be asked to enter...

  • In C++, Step 1: Implement the Student Class and write a simple main() driver program to...

    In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...

  • C++ problem: Make a program that the user enter the degree and coefficients of a polynomial....

    C++ problem: Make a program that the user enter the degree and coefficients of a polynomial. Use the overload operators mentioned in the class below: #include <iostream> #include <stdlib.h> using namespace std; class Polynomial{ public:    Polynomial(); //Default constructor: it creates an Polynomial of degree 99 Polynomial(int dg); //Special constructor: it creates an Polynomial of degree dg Polynomial(const Polynomial &Original); //Copy Constructor ~Polynomial(); //Destructor: deallocate memory void readPolynomial(); //readPolynomial Method: Reads all positions of the Polynomial void printPolynomial(); //printPolynomial Method:...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer...

    write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer class (Computer) that describes a computer object. A computer has the following properties: Amount of Ram in GB (examples: 8, 16), defaults to 8. Hard drive size in GB (examples: 500, 1000), defaults to 500. Speed in GHz (examples: 1.6, 2.4), defaults to 1.6. Type (laptop, desktop), defaults to "desktop" Provide the following functions for the class: A default constructor (constructor with no parameters)...

  • Write a C program Design a program that uses an array to store 10 randomly generated...

    Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT