C++ Programming Array A car dealer has 10 salespersons. Each salesperson keeps t
ID: 3684301 • Letter: C
Question
C++ Programming Array
A car dealer has 10 salespersons. Each salesperson keeps track of the number of cars sold each month and reports it to the management at the end of the month. The management keeps the data in a file and assigns a number, 1 to 10 to each salesperson. The following statement declares an array, cars, of 10 components of type int to store the number of cars sold by each salesperson.
Int cars[10];
Write the C++ code to store the number of cars sold by each salesperson in the array cars, output the total number of cars sold at the end of each month, and output the salesperson number selling the maximum number of cars. (Assume the data is in the file cars.dat. and this file has been opened using ifstream variable inFile.)
Explanation / Answer
#include<iostream.h>
#include<fstream.h>
void main()
{
int ind,max,cars[10],i=0,sum=0;
ifstream inFile("cars.dat");
while(inFile)
{
inFile>>cars[i];
sum=sum+cars[i];
i++;
}
cout<<"Total cars sold="<<sum;
max=cars[0];
for(i=0;i<10;i++)
{
if(cars[i]>max)
{
max=cars[i];
ind=i;
}
}
cout<<"Sales person selling max cars is="<<i+1;
inFile.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.