Write a function display that takes the array of cars and its size as parameters and display all records. using structure , c++ code.

20 0

Get full Expert solution in seconds

$1.97 ONLY

Unlock Answer
  1. Write a function display that takes the array of cars and its size as parameters and display all records. using structure , c++ code.

EXPERT ANSWER

#include <iostream>
using namespace std;
#include<vector>;
struct Car
{
    string name;
    int size;
    
};

void display(vector<string> cars,int size[]){
    
    Car car[cars.size()];
    cout<<"displaying Car details :"<<endl;
    for(int i=0;i<cars.size();i++){
        car[i].name=cars.at(i);
        car[i].size=size[i];
        cout<<"Car "<<i+1<<" is "<<car[i].name <<" and its size is "<<car[i].size<<endl;
    }
}
int main()
{
    int n;
    cout<<"Enter Length of the array";
    cin>>n;
    cout << "Enter information of Cars : " << endl;
    vector<string> cars;
    int size[n];
    for(int i = 0; i < n; ++i)
    {
        string st;
        cout << "Enter Car Name" <<i+1<< " ";
        cin>>st;
        cars.push_back(st);
        cout << "Enter Size of the Car: ";
        cin >>size[i];

        
        cout << endl;
    }
    display(cars,size);
    
    return 0;
}

Output: