Write a C++ program that calculates and displays values for y when y = ((x)(z)^0
ID: 3870158 • Letter: W
Question
Write a C++ program that calculates and displays values for y when
y = ((x)(z)^0.5) / (z - x) using nested "for" loops.
Your program should calculate the values of y for values of x ranging between 1 and 5 (inclusive of 1 and 5) and values of z ranging between 2 and 6 (inclusive of 2 and 6). The x variable should control the outer loop and be incremented in steps of 1 and z should be incremented in steps of 1. You must display the message "Undefined" if and when applicable.
Your output should consist of three columns with three column headings. The first column must be the "x" values, the second must be the "z" values and the third must be "y" values. You must use manipulators to make the columns line up.
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int x,z;
float y;
cout << "x z y" << endl;
// x in range 1 to 5 inclusive
for(x=1;x<6;x++){
// z in range 2 to 6 inclusive
for(z=2;z<7;z++){
//calculate y for given formula
y =pow(x*z,0.5)/(z-x);
cout << x << " "<< z << " ";
//check if y is infinity
if(isinf(y))
cout << "undefined" << endl;
else
cout << y << endl;
}
}
return 0;
}
/*
sample output
x z y
1 2 1.41421
1 3 0.866025
1 4 0.666667
1 5 0.559017
1 6 0.489898
2 2 undefined
2 3 2.44949
2 4 1.41421
2 5 1.05409
2 6 0.866025
3 2 -2.44949
3 3 undefined
3 4 3.4641
3 5 1.93649
3 6 1.41421
4 2 -1.41421
4 3 -3.4641
4 4 undefined
4 5 4.47214
4 6 2.44949
5 2 -1.05409
5 3 -1.93649
5 4 -4.47214
5 5 undefined
5 6 5.47723
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.