Using C++ how would I do the following: Write a program that prompts the user fo
ID: 3790996 • Letter: U
Question
Using C++ how would I do the following:
Write a program that prompts the user for the prices of 10 automobiles (use looping to do this and store the prices in an array). The automobiles may be priced from less than $1 up to $99,999. No negative numbers are allowed. Perform error checking to ensure that only valid numbers are entered ($1 - $99,999). Using a function, calculate and display the number of cars in all 10 price categories, i.e., $1 - $9999, $10,000 - $19,999, $20-000 - $29,999, … $90,000 - $99,999. Call a function that loops through the array, and displays the highest and lowest priced cars only. Call a separate function which calculates the average of all of the cars, and passes the average amount back to the main() function, where it is printed out.
Sample Session:
Please enter the price of Automobile #1: 25999
Please enter the price of Automobile #2: 15000
Please enter the price of Automobile #3: 45000
Please enter the price of Automobile #4: 65000
Please enter the price of Automobile #5: 2999
Please enter the price of Automobile #6: 33000
Please enter the price of Automobile #7: 82000
Please enter the price of Automobile #8: 41000
Please enter the price of Automobile #9: 24000
Please enter the price of Automobile #10: 19999
The number of cars in the $1 - $9999 range is: 1
The number of cars in the $10000 - $19999 range is: 2
The number of cars in the $20000 - $29999 range is: 2
The number of cars in the $30000 - $39999 range is: 1
The number of cars in the $40000 - $49999 range is: 2
The number of cars in the $50000 - $59999 range is: 0
The number of cars in the $60000 - $69999 range is: 1
The number of cars in the $70000 - $79999 range is: 0
The number of cars in the $80000 - $89999 range is: 1
The number of cars in the $90000 - $99999 range is: 0
The highest priced car is: $82000
The lowest priced car is: $2999
The average price of all the cars is: $35400 (round to the nearest dollar)
Explanation / Answer
//
// main.cpp
// chegg1
//
//
// All rights reserved.
//
#include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int a[10];
int cnt1,cnt2,cnt3,cnt4,cnt5,cnt6,cnt7,cnt8,cnt9,cnt10,sum;
int highest(){
int k=a[9];
return k;
}
int lowest(){
int k1=a[0];
return k1;
}
float average(float sum){
return (ceil)(sum/10);
}
void count(){
cout<<"The number of cars in the $1 - $9999 range is:"<<cnt1<<endl;
cout<<"The number of cars in the $10000 - $19999 range is:"<<cnt2<<endl;
cout<<"The number of cars in the $20000 - $29999 range is:"<<cnt3<<endl;
cout<<"The number of cars in the $30000 - $39999 range is:"<<cnt4<<endl;
cout<<"The number of cars in the $40000 - $49999 range is:"<<cnt5<<endl;
cout<<"The number of cars in the $50000 - $59999 range is:"<<cnt6<<endl;
cout<<"The number of cars in the $60000 - $69999 range is:"<<cnt7<<endl;
cout<<"The number of cars in the $70000 - $79999 range is:"<<cnt8<<endl;
cout<<"The number of cars in the $80000 - $89999 range is:"<<cnt9<<endl;
cout<<"The number of cars in the $90000 - $99999 range is:"<<cnt10<<endl;
}
int main() {
for(int i=0;i<10;i++){
cout<<"Please enter the price of Automobile #"<<i+1<<":";
cin>>a[i];
if(a[i]<0){
cout<<"Invalid Input: Please enter Postive number"<<endl;
i=i-1;
}
else{
sum=sum+a[i];
}
}
for(int i=0;i<10;i++){
if(a[i]>=0 && a[i]<=9999)
cnt1=cnt1+1;
if(a[i]>=10000 && a[i]<=19999)
cnt2=cnt2+1;
if(a[i]>=20000 && a[i]<=29999)
cnt3=cnt3+1;
if(a[i]>=30000 && a[i]<=39999)
cnt4=cnt4+1;
if(a[i]>=40000 && a[i]<=49999)
cnt5=cnt5+1;
if(a[i]>=50000 && a[i]<=59999)
cnt6=cnt6+1;
if(a[i]>=60000 && a[i]<=69999)
cnt7=cnt7+1;
if(a[i]>=70000 && a[i]<=79999)
cnt8=cnt8+1;
if(a[i]>=80000 && a[i]<=89999)
cnt9=cnt9+1;
if(a[i]>=90000 && a[i]<=99999)
cnt10=cnt10+1;
}
count();
sort(a,a+10);
int z=highest();
cout<<"The Highest priced car is $"<<z<<endl;
int z1=lowest();
cout<<"The Lowest priced car is $"<<z1<<endl;
int avg=average(sum);
cout<<"The Average price of all cars is $"<<avg<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.