60 627
Assignments Done
97,8%
Successfully Done
In April 2018

Answer to Question #50473 in C++ for John

Question #50473
Write a program that can find the price of all cars in a gallery, find the price of all cars that is
made in the specific year and made. Use structure and assume the maximum number of cars is
100 but ask the user how many cars he has.

Use the following structure and prototypes use the string library when needed.

struct Car
{
int Year;
double Price;
char Color[10];
char Type[20];
int Id;
};

void InputCars(struct Car[]);
void OutputCars(struct Car[]);
double AllPrices(struct Car[]);// find the price of all cars
double CustomSum(struct Car[],int);// find the price of all cars that is made in the year 2009;
double CustomSum(struct Car[],char[]);// find the price of all cars that is Opel made;
Expert's answer
#include <iostream>
#include <iomanip>
#include <string.h>
#include <cstdlib>
using namespace std;
struct Car {
    int Year;
    double Price;
    char Color[10];
    char Model[20];
    int Id;
};
void InputCars(Car cars[], int numOfCars);
void OutputCars(Car cars[], int numOfCars);
double AllPrices(Car cars[], int numOfCars); // find the price of all cars 
double CustomSum(Car cars[], int numOfCars, int year); // find the price of all cars that is made in the year 2009; 
double CustomSum(Car cars[], int numOfCars, char model[]); // find the price of all cars that is Opel made; 
int main() {
    int numOfCars = 0;
    Car cars[100];
    int item = 0;
    cout.setf(ios::fixed); // set fixed point
    cout.precision(2); // set output precision
    while (item != 6) {// print menu until user choose item 6
        cout << "-------------------------" << endl;
        cout << " 1. Input cars." << endl;
        cout << " 2. Output cars." << endl;
        cout << " 3. Calculate total price." << endl;
        cout << " 4. -//- for specific year." << endl;
        cout << " 5. -//- for specific model." << endl;
        cout << "-------------------------" << endl;
        cout << " 6. Quit" << endl;
        cout << "-------------------------" << endl;
        cout << ">";
        cin>>item;
        cout << endl;
        switch (item) {
            case 1:
            {
                cout << "Enter number of cars: ";
                cin>>numOfCars; // read number of cars
                cout << "Enter data for " << numOfCars << " cars." << endl;
                InputCars(cars, numOfCars); // call funtion to inpu cars
                break;
            }
            case 2:
            {
                OutputCars(cars, numOfCars);
                break;
            }
            case 3:
            {
                double total = AllPrices(cars, numOfCars); // calculate total price for all cars
                cout << "\nTotal price for all cars is: $" << total << endl;
                break;
            }
            case 4:
            {
                int year;
                cout << "Enter specific year: ";
                cin>>year; // read year
                double total = CustomSum(cars, numOfCars, year); // calculate total price
                cout << "\nTotal price for all cars made in " << year << " is: $" << total << endl;
                break;
            }
            case 5:
            {
                char model[30];
                cout << "Enter specific model: ";
                cin>>model; // read model
                double total = CustomSum(cars, numOfCars, model); // calculate total price
                cout << "\nTotal price for all cars of " << model << " is: $" << total << endl;
                break;
            }
            case 6:
            {
                cout << " -- Good bye!" << endl;
                break;
            }
            default:
            {
                cout << " -- Invalid item!" << endl;
                break;
            }
        }
        cout << endl;
    }
    return 0;
}
void InputCars(Car cars[], int numOfCars) {
    for (int i = 0; i < numOfCars; i++) {// for each records
        cout << "Car " << i + 1 << ": " << endl;
        cout << "     Year: ";
        cin >> cars[i].Year;
        cout << "    Price: $";
        cin >> cars[i].Price;
        cout << "    Color: ";
        cin >> cars[i].Color;
        cout << "    Model: ";
        cin >> cars[i].Model;
        cout << "       ID: ";
        cin >> cars[i].Id;
    }
}
void OutputCars(Car cars[], int numOfCars) {
    /*Print header of the table*/
    cout << setw(3) << "#" << setw(6) << "Year" << setw(10) << "Price($)"
            << setw(10) << "Color" << setw(12) << "Model" << setw(14) << "ID" << endl;
    for (int i = 0; i < numOfCars; i++) {// for each records
        /*Print data for current car*/
        cout << setw(3) << i + 1 << setw(6) << cars[i].Year << setw(10) << cars[i].Price
                << setw(10) << cars[i].Color << setw(12) << cars[i].Model << setw(14) << cars[i].Id << endl;
    }
}
double AllPrices(Car cars[], int numOfCars) { // find the price of all cars 
    double total = 0;
    for (int i = 0; i < numOfCars; i++) {// for each records
        total += cars[i].Price;
    }
    return total;
}
double CustomSum(Car cars[], int numOfCars, int year) { // find the price of all cars that is made in the year 2009; 
    double total = 0;
    for (int i = 0; i < numOfCars; i++) {// for each records
        if (cars[i].Year == year)// compare the year
            total += cars[i].Price;
    }
    return total;
}
double CustomSum(Car cars[], int numOfCars, char model[]) { // find the price of all cars that is Opel made; 
    double total = 0;
    for (int i = 0; i < numOfCars; i++) {// for each records
        if (strcmpi(cars[i].Model, model) == 0)// compare the model
            total += cars[i].Price;
    }
    return total;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions