c++ PROBLEM 2 Use the following program shell for PROBLEMS 2 through 5. Write a
ID: 3743130 • Letter: C
Question
c++
PROBLEM 2
Use the following program shell for PROBLEMS 2 through 5.
Write a program to print the following numbers right-justified in a column on the screen. Make the values named constants.
When you are finished, copy and paste your output to the end of your source code as a comment. Re-compile this commented code to make sure that your have not introduced any new errors.
Finally, copy your code/output and paste it into the box below.
PROBLEM 3
First, modify your program to prompt for the five (5) numbers.
Next, add two additional statements to your program. Calculate the floating-point result from dividing the sum of the first two values by the sum of the last three values and store it in answer. The second statement should write the contents of answer on the screen to four decimal places. (Do not forget to declare answer.)
When you are finished, copy and paste your output to the end of your source code as a comment. Re-compile this commented code to make sure that your have not introduced any new errors.
Finally, copy your code/output and paste it into the box below.
PROBLEM 4
Finally, modify your program use three (3) functions.
(1) add the heading function you wrote in PROBLEM 1 to your program. Of course, call the function from main.
(2) Write and call a value-returning function to calculate the floating-point result from dividing the sum of the first two values by the sum of the last three values and store it in answer.
The third void function should write the contents of answer on the screen to four decimal places. (Do not forget to declare answer in main.
Use the following prototypes in your program
When you are finished, copy and paste your output to the end of your source code as a comment. Re-compile this commented code to make sure that your have not introduced any new errors.
Finally, copy your code/output and paste it into the box below.
Explanation / Answer
//program2
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << fixed << showpoint;
//problem 2
//1066 1492 512 1 -23
//declare 5 constants of type int
const int int1 = 1066, int2 = 1492, int3 = 512, int4 = 1, int5 = -23;
cout << right << int1 << " "<<right << int2 <<" "<< right << int3 <<" "<< right << int4 <<" "<< right << int5 << endl;
return 0;
}
/*output:
1066 1492 512 1 -23
*/
---------------------------------------------
//program3
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << fixed << showpoint;
//declare 5 variable of type int
int num1, num2, num3, num4, num5,sum1,sum2;
//ask for user input
cout << "Enter 5 integers : ";
cin >> num1 >> num2 >> num3 >> num4 >> num5;
//add first two numbers
sum1 = num1 + num2;
//add last 3 numbers
sum2 = num3 + num4 + num5;
//output sum1/sum2 with 4 decimal point
//use setprecision funtion
cout << setprecision(4);
cout << (float)sum1 / sum2 << endl;
return 0;
}
/*
//output
Enter 5 integers : 40 50 15 5 12
2.8125
*/
-------------------------------------------
//program4
//heading function not provided , writing this function, you can replace with your code, if you want
#include <iostream>
#include <iomanip>
using namespace std;
void heading(); // function prototypes
float divide_sum(int, int, int, int, int);
void display_answer(float);
int main()
{
cout << fixed << showpoint;
//problem 2
//declare 5 variable of type int
int num1, num2, num3, num4, num5,sum1,sum2;
//ask for user input
cout << "Enter 5 integers : ";
cin >> num1 >> num2 >> num3 >> num4 >> num5;
//call heading function
heading();
//call divde_sum
double ans;
ans=divide_sum(num1, num2, num3, num4, num5);
//call dispay function
display_answer(ans);
return 0;
}
void heading() // function prototypes
{
cout << "#######Function heading write your code here from program1###################" << endl;
}
float divide_sum(int num1, int num2, int num3, int num4, int num5 )
{
float sum1, sum2;
sum1 = num1 + num2;
sum2 = num3 + num4 + num5;
return sum1/sum2;
}
void display_answer(float ans)
{
//set precision as 4 decimal point
cout << setprecision(4);
cout << ans << endl;
}
/*output
Enter 5 integers : 40 85 5 6 12
#######Function heading write your code here from program1###################
5.4348
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.