Write a complete C++ program , including comments , to do the following: Your pr
ID: 3580953 • Letter: W
Question
Write a complete C++ program , including comments , to do the following: Your program will compute values of a formula that expresses result in terms of val. The formula is the following: (read formula from your assignment sheet: this shows up as a mess):
-4val 3 -3val 2 - 15val + 22
result = ____________________________
__________
| 2 - val 4 | + / 2val 2 + 8
Note that | | means absolute value
Use the library functions for absolute value and square root; do not use the library function for exponentiation. Make sure to include cmath header file.
1. The program should start by printing a message saying "this is the output of my first program ".
2. Then it should evaluate the formula starting with val = 3, going up by 0.5 each time until it reaches 5; therefore, it will use these values for val: -3.0, -2.5,..., 0.5, 0, 0.5, 1,..., 4.5, 5.
For each val value , the program should compute the corresponding result value . It should print these values together with explanations of what the values represent. For example, it should print the string "val = ", then the value of val, the string "result = ", and the value of result (do NOT use column headings instead of these explanations, as this will not be marked correct in CodelabBsee below).
Following each result value , the program should print a message that says one of three things (note that you can't print in italics):
If the value of result is exactly 0, the message should say result is zero.
If the value of result is positive, the message should say result is positive.
If the value of result is negative, the message should say result is negative.
A typical line of output would look like this:
val = 2 result = 0 result is zero
3. Once you have finished processing val = 5, the program should print the message (underneath the last line of output ) saying "the program is finished". Then the program should stop.
Be sure your results are correct! The formula will yield result values that are positive, negative and zero. The value of the formula will be zero exactly one time.
Submit your program (source code: prog1.cpp) and the output . Do NOT sent the executable file (prog1.exe).
Test your answers by running the program in Codelab (Test Your Assignment 1). You can copy your program into the work area. For your program to be accepted by CodeLab, your output --including your messages --must match mine exactly (except for spaces and capitals). Do NOT put the optionals in the program you submit to Codelab.
OPTIONALS: (5 points apiece) Do the optionals only AFTER your program runs correctly. Hand in a copy of the program WITHOUT the optionals, and a copy with the optionals. Do not make your program late while you are trying to do the optionals.
1. Have your program count how many times the value of result is positive, how many times it is negative, and how many times it is zero. Print these three values together with messages .
2. Have your program find which of the result values is closest to 0 (either larger or smaller) without actually being equal to 0. Have the program print this result value AND the val value that gives this closest result value .
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float val = -3;
float result;
int numerator;
float dominator;
int countPositive = 0, countNegetive = 0, countZero = 0;
int countClose2Zero = 0;
cout << "This is the output of my first program " << endl;
while (val <= 5)
{
//calculate result using given formula
numerator = (-4 * val * val * val) - (3 * val *val) - (15 * val) + 22;
dominator = abs(2 - val*val*val*val) + sqrt(2*val*val + 8);
result = numerator / dominator;
cout << "val = " << val << " " << "result = " << result << " ";
if (result == 0)
{
cout << "result is zero" << endl;
++countZero;
}
if (result < 0)
{
cout << "result is negetive" << endl;
++countNegetive;
}
if (result > 0)
{
cout << "result is positive" << endl;
++countPositive;
}
//add 0.5 and value moves up
val += 0.5;
}
cout << "result is positive " << countPositive << " times" << endl;
cout << "result is negetive " << countNegetive << " times" << endl;
cout << "result is zero " << countZero << " times" << endl;
cout << "the program is finished" << endl;
}
-----------------------------------------------------------------------------------------------------
output
This is the output of my first program
val = -3 result = 1.75983 result is positive
val = -2.5 result = 2.47655 result is positive
val = -2 result = 4 result is positive
val = -1.5 result = 7.72958 result is positive
val = -1 result = 9.12962 result is positive
val = -0.5 result = 5.97571 result is positive
val = 0 result = 4.55635 result is positive
val = 0.5 result = 2.67877 result is positive
val = 1 result = 0 result is zero
val = 1.5 result = -3.03121 result is negetive
val = 2 result = -2.88889 result is negetive
val = 2.5 result = -2.30824 result is negetive
val = 3 result = -1.87874 result is negetive
val = 3.5 result = -1.54783 result is negetive
val = 4 result = -1.31374 result is negetive
val = 4.5 result = -1.13246 result is negetive
val = 5 result = -0.995852 result is negetive
result is positive 8 times
result is negetive 8 times
result is zero 1 times
the program is finished
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.