30%) In the last Thanksgiving parade there was a convoy of 10 vehicles, travelin
ID: 3738849 • Letter: 3
Question
30%) In the last Thanksgiving parade there was a convoy of 10 vehicles, traveling one behind the other. The vehicles include cars, pickup trucks, vans and RVs spread randomly along the convoy. Write a program that will perform the following: a. (500) Accept the number n and define an appropriate array to describe the convoy b. (5%) Fill the array with a code corresponding to the type of vehicle in each position 1. of the convoy and print the array (15%) Determine how many of each kind of vehicle in the convoy and print out the results. (596) What were the first and the last vehicles in the convoy and print out the results. c. d.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int count(char convoy[], int n, char code) {
int res = 0;
for(int i = 0;i < n;i++) {
if(convoy[i] == code) res++;
}
return res;
}
int main() {
int n;
cout<<"Enter the number of vehicles in the convoy: ";
cin>>n;
char vehicle_code[4] = {'C', 'P','V','R'};
char convoy[n];
//Randomly fill the convoy
for(int i = 0;i < n;i++) {
convoy[i] = vehicle_code[rand()%4];
}
//print the convoy
for(int i= 0;i < n;i++) {
cout<<convoy[i]<< " ";
}
cout<<endl;
//print the number of each kind of vehicle
cout<<"Number of cars: "<<count(convoy,n,'C')<<endl;
cout<<"Number of pickup trucks: "<<count(convoy,n,'P')<<endl;
cout<<"Number of vans: "<<count(convoy,n,'V')<<endl;
cout<<"Number of RVs: "<<count(convoy,n,'R')<<endl;
cout<<"First vehicle in the convoy: ";
switch(convoy[0]) {
case 'C': cout<<"Car"<<endl;
break;
case 'P': cout<<"Pickup Truck"<<endl;
break;
case 'V': cout<<"Van"<<endl;
break;
case 'R': cout<<"RV"<<endl;
break;
}
cout<<"Last vehicle in the convoy: ";
switch(convoy[n-1]) {
case 'C': cout<<"Car"<<endl;
break;
case 'P': cout<<"Pickup Truck"<<endl;
break;
case 'V': cout<<"Van"<<endl;
break;
case 'R': cout<<"RV"<<endl;
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.