Objective: Write a C++ program that interacts with the user and does some repeti
ID: 3887084 • Letter: O
Question
Objective: Write a C++ program that interacts with the user and does some repetition. Reference: Lectures and Chapter 4 Lab: A. Using any of the three loops, write a program that will evaluate the function y = 4x^2 - 16x + 15, with x going from 1 to 2 in steps of 0.1. For each x, give the value of y and the message POSTIVE or NOT POSITIVE. Format the output in this format: B. Once you have the above code working. In the SAME cpp file, use a second type of loop to evaluate y = 2x^3 - 5x + 7 going from 3 to 4 in steps of 15. Do the same as above, format and include the message POSTITIVE or NOT POSITIVE. C. Now use the third type of loop. Prompt for the start value, end value and increment value and evaluate y = 3x^4 + 8x^2 - 24. Once again, format as above and include the message POSTITIVE or NOT POSITIVE When you have successfully completed the entire lab: 1. Run the program and copy the output to the end of the cpp file as a comment. 2. Upload the completed cpp file to Canvas. You will be graded on: Good programming style (5) Comments(30) Neat and readable (10) Variables (5) Logic (10) (10) Prompting (10) Selection (5) Repetition Output (10) Other(5)Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
// Local variable declaration:
float x=1, i, j, k;
cout << "while loop execution" <<endl;
cout << "x value y value" << endl;
while( x <= 2.1 ) {
float y = 4*x*x - 16*x +15;
cout << fixed << setprecision(1) << x ;
cout <<" ";
cout << fixed << setprecision(2) << y ;
if (y <0)
cout << " NOT POSITIVE" << endl;
else
cout << " POSITIVE" << endl;
x=x+0.1;
}
cout << endl;
cout << endl;
cout << "for loop execution" <<endl;
cout << "x value y value" << endl;
for(x = 3; x < 4; x = x + 0.15 ) {
float y = 2*x*x*x - 5*x +7;
cout << fixed << setprecision(2) << x ;
cout <<" ";
cout << fixed << setprecision(2) << y ;
if (y <0)
cout << " NOT POSITIVE" << endl;
else
cout << " POSITIVE" << endl;
}
cout << endl;
cout << endl;
cout << "do while loop execution" <<endl;
cout << "Start Value: ";
cin >> x;
cout << "End Value: ";
cin >> j;
cout << "Increment: ";
cin >> k;
cout << endl;
cout << "x value y value" << endl;
do {
float y = 3*x*x*x*x + 8*x*x -24;
cout << fixed << setprecision(2) << x ;
cout <<" ";
cout << fixed << setprecision(2) << y ;
if (y <0)
cout << " NOT POSITIVE" << endl;
else
cout << " POSITIVE" << endl;
x=x+0.1;
} while( x <= j+k );
return 0;
}
Result:
while loop execution
x value y value
1.0 3.00 POSITIVE
1.1 2.24 POSITIVE
1.2 1.56 POSITIVE
1.3 0.96 POSITIVE
1.4 0.44 POSITIVE
1.5 0.00 POSITIVE
1.6 -0.36 NOT POSITIVE
1.7 -0.64 NOT POSITIVE
1.8 -0.84 NOT POSITIVE
1.9 -0.96 NOT POSITIVE
2.0 -1.00 NOT POSITIVE
for loop execution
x value y value
3.00 46.00 POSITIVE
3.15 53.76 POSITIVE
3.30 62.37 POSITIVE
3.45 71.88 POSITIVE
3.60 82.31 POSITIVE
3.75 93.72 POSITIVE
3.90 106.14 POSITIVE
do while loop execution
Start Value: End Value: Increment:
x value y value
1.00 -13.00 NOT POSITIVE
1.10 -9.93 NOT POSITIVE
1.20 -6.26 NOT POSITIVE
1.30 -1.91 NOT POSITIVE
1.40 3.20 POSITIVE
1.50 9.19 POSITIVE
1.60 16.14 POSITIVE
1.70 24.18 POSITIVE
1.80 33.41 POSITIVE
1.90 43.98 POSITIVE
2.00 56.00 POSITIVE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.