(C++) Using Visual Studio Express 2017 1. Write a program that prompts the user
ID: 3876499 • Letter: #
Question
(C++) Using Visual Studio Express 2017
1. Write a program that prompts the user to input a decimal number and outputs the number rounded to the nearest integer. There is no one function that will do this automatically. You will have to resort to some mathematical trick (very simple one) or a combination of functions. Remember static_cast will only truncate and not round.
2. Write a program that prompts the user to enter five test scores and then prints the average test score. (Assume that the test scores are decimal numbers.)
3. Write a C++ program that prompts the user to input the elapsed time for an event in seconds. The program then outputs the elapsed time in hours, minutes, and seconds. (For example, if the elapsed time is 9630 seconds, then the output is 2:40:30.), and seconds. The program then outputs the elapsed time in seconds.)
Explanation / Answer
/* 1. CPP program that prompts the user to input a decimal number and outputs the number rounded to the nearest integer.*/
#include<iostream.h>
int main()
{
double number;
cout<<"Enter any decimal number ";
cin>>number;
cout<<static_cast<int>(number+0.5)<<endl;
return 0;
}
Output:
Enter any decimal number 10.5
11
Enter any decimal number 10.8
11
Enter any decimal number 10.4
10
/* 2. CPP program that prompts the user to enter five test scores and then prints the average test score*/
#include <iostream>
int main()
{
double testscores[5],total=0.0,average;
int i;
cout<<"enter your five test scores";
for(i=0;i<5;i++)
cin>>testscores[i];
for(i=0;i<5;i++)
{
total=total+testscores[i];
}
average=total/5;
cout<<"total test score is"<<total<<" ";
cout<<"average of five test scores is"<<average;
}
Output:
enter your five test scores
50 60 89 45 90
total test score is 334
average of five test scores is 66.8
/* 3. CPP program that prompts the user to input the elapsed time for an event in seconds. The program then outputs the elapsed time in hours, minutes, and seconds. */
#include <iostream>
int main()
{
int secondsElapsed,hours,minutes,seconds;
int secondsinminute=60;
int secondsinhour=60*secondsinminute;
cout<<"enter the number of seconds elapsed: ";
cin>>secondsElapsed;
hours = secondsElapsed / secondsinhour;
secondsElapsed = secondsElapsed % secondsinhour;
minutes = secondsElapsed / secondsinminute;
seconds = secondsElapsed % secondsinminute;
cout <<" "<< hours << ":" << minutes << ":" << seconds << endl;
return 0;
}
Output:
enter the number of seconds elapsed: 3672
1:1:12
enter the number of seconds elapsed: 3600
1:0:0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.