Reputation: 17
I have a problem approaching this problem. It says that I have let the program find the min, max, and range when the User enter a min range and a max range.
Here it the code I have:
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#define pi 3.1416
#define POINTS 20
using namespace std;
int main()
{
int Xmin, Xmax;
double step;
cout << "Enter a value for xMin and xMax:\n";
cin >> Xmin >> Xmax;
step = (double)(Xmax - Xmin) / (double)POINTS;
cout << "X-VALUES " << "" << "| " << "" << "Y-VALUES" << endl;
cout << "_________" << "" << "|_" << "" << "_________" << endl;
for (int i = 0; i < POINTS; ++i)
{
double x = Xmin + (step * i);
double y = 0.0572 * cos(4.667 * x) + 0.0218 * pi * cos(12.22 * x);
cout << x << "\t " << setprecision(2) << y << endl;
}
cout << "____________________" << endl;
return 0;
}
I here is the output of my program:
X-Value | Y-Value
__________|__________
-2 -0.0043
-1.8 -0.0982
-1.6 0.0378
-1.4 0.0438
-1.2 0.0099
-1 0.0618
-0.8 -0.1118
-0.6 -0.0198
-0.4 -0.0047
-0.2 -0.0184
0 0.1257
0.2 -0.0184
0.4 -0.0047
0.6 -0.0198
0.8 -0.1118
1 0.0618
1.2 0.0099
1.4 0.0438
1.6 0.0738
1.8 -0.0982
2 -0.0043
─────────────────────
Basically, this program all ready has a equation that calculates the numbers and list them based on the users inputs for Xmin and Xmax. I'm suppose to let the program find the min, max, and calculate it's range from the Y-Values from the table above.
Here is my code for finding min and max.
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <limits>
#define pi 3.1416
#define POINTS 20
using namespace std;
int main()
{
int Xmin, Xmax;
double step;
int max = numeric_limits<int> :: min();
int min = numeric_limits<int> :: max();
int num = 0;
cout << "Enter a value for xMin and xMax:\n";
cin >> Xmin >> Xmax;
step = (double)(Xmax - Xmin) / (double)POINTS;
cout << "X-VALUES " << "" << "| " << "" << "Y-VALUES" << endl;
cout << "_________" << "" << "|_" << "" << "_________" << endl;
for (int i = 0; i < POINTS; ++i)
{
double x = Xmin + (step * i);
double y = 0.0572 * cos(4.667 * x) + 0.0218 * pi * cos(12.22 * x);
//printf(" %f\t%f\n ", x, y);
cout << x << "\t " << setprecision(2) << y << endl;
}
cout << "____________________" << endl;
while (cout << "Enter a value for xMin and xMax:\n" &&
cin >> Xmin >> Xmax)
{
if (num > max) max = num;
if (num < min) min = num;
}
cout << "max is: " << max << '\n'
<< "min is: " << min << '\n';
return 0;
}
It runs, but it doesn't print out the min or max. It just repeats the program at "enter Xmin and Xmax". But when I enter a any leter it prints out the min and max. Help me. I'm confused.
Upvotes: 0
Views: 3242
Reputation: 57678
The local variable num
is never assigned a value within the function. You use it in a while
loop, but you don't input into it.
When you declare a variable, you should always initialize it.
Edit 1: Min and Max
You can keep a running minimum and maximum for any variable.
Set the maximum variable to the lowest value, set the minimum to the largest value.
After you calculate the y
variable perform the following:
if ( y > max_y) max_y = y;
if ( y < min_y) min_y = y;
This will keep a running minimum and maximum.
I recommend performing this for the sum variable. You will need a sum variable.
Upvotes: 1