Solution of Assignment# 2 (Quiz)

Question #1:Write a program with a loop that lets the user enter a series of integers, followed by -99 to signalthe end of the series. After all the numbers have been entered, the program should display thelargest and smallest numbers entered.

int main()

{int num, greatest, least;
cout << "Type an integer that isn't -99\n";
cin >> num;
greatest = num, least = num;
while (num != -99)
{
if (num > greatest)
greatest = num;
else if (num < least)
least = num;
cout << "Type an integer that isn't -99\n";
cin >> num;
}
cout << "Least: " << least << endl;
cout << "Greatest: " << greatest << endl;
return 0;}


Question #2:
Write a program that uses nested loops to collect data and calculate the average rainfall over aperiod of years. The program should first ask for the number of years. The outer loop will iterateonce for each year. The inner loop will iterate twelve times, once for each month. Each iterationof the inner loop will ask the user for the inches of rainfall for that month.After all iterations, the program should display the number of months, the total inches ofrainfall, and the average rainfall per month for the entire period.Input Validation: Do not accept a number less than 1 for the number of years. Do not acceptnegative numbers for the monthly rainfall.

Solution:

void main()
{
int year,totalmonth=0;
float avg=0,n=0,suminch=0,totalsuminch=0;
cout<<"Enter years: ";
cin>>year;
while (year<1)
{
cout<<"Enter years: ";
cin>>year;
}
for (int i=1 ; i<=year ; i++)
{
cout<<"\n\"Data collection for year no. "<<(i)<<"\n";
for (int x=1 ; x<=12 ; x++)
{
cout<<"\nEnter the total rainfall for the month "<<(x)<<": ";
cin>>n;
suminch=suminch+n;
}
totalsuminch=totalsuminch+suminch;
totalmonth=totalmonth+x-1;
}
avg=totalsuminch/totalmonth;
cout<<"\nThe total months are "<cout<<"The total rainfall in inches are "<cout<<"The average rainfall for per month is "<}


Question #3:
The distance a vehicle travels can be calculated as follows:distance = speed * timeFor example, if a train travels 40 miles per hour for three hours, the distance traveled is 120 miles.Write a program that asks the user for the speed of a vehicle (in miles per hour) and howmany hours it has traveled. It should then use a loop to display the total distance traveled at theend of each hour of that time period. Here is an example of the output:


What is the speed of the vehicle in mph? 40How many hours has it traveled? 3Hour Distance Traveled-------------------------------- 1 40 2 80 3 120


Solution:


void main()
{
int speed,time;
cout<<"Enter speed: ";
cin>>speed;
cout<<"Enter time: ";
cin>>time;
cout<<"Time(in hour) Speed(km/h)"<<"\n";
cout<<"**********************************"<<"\n";
int i=1,distance=1;
while (i<=time)
{
distance=i*speed;
cout<<" "<i++;
}}


To download all these solutions in a text file,Solution of Assignment # 2.txt

3 comments:

  1. Anonymous said...

    Amazing! Its in fact amazіng paragraρh, Ӏ haѵe got muсh сleaг iԁea аbout
    frοm this aгtісle.

    Feel frеe to ѕurf to my wеb-sіtе herbal incensе burner
    ()

    Anonymous said...

    I've been surfing online more than 4 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all website owners and bloggers made good content as you did, the web will be a lot more useful than ever before.

    Here is my homepage; herbal incense blends lbs (wikigpia.info)

    Anonymous said...

    Amazing blοg! Is your theme custom made or did yоu download it from ѕomewhere?
    A deѕign like yourѕ with а few simple adjustements would really makе my blog jump out.
    Pleаse let me know where you gοt your theme.
    Thank уοu

    Feel frеe to surf to my weblog: party pills cheap

Post a Comment

Your words are your own, so be nice and helpful if you can.