It's a simple if greater than, then make it the value of BigHigh/BigLow, however it does not return successfully to main for the printout of the respective data. Instead, the printout reads 0 for both functions. Any tips on where I go wrong?
#include<iostream>
using namespace std;
double getData(double tempmatrix[][2], double BigHigh, double BigLow) {
for(int x=0;x<12;++x) {
cout << "Enter the highest temperature for Month " << x+1 << ": ";
cin >> tempmatrix[x][0];
if(tempmatrix[x][0] > BigHigh) {
BigHigh = tempmatrix[x][0];
}
cout << "Enter the lowest temperature for Month " << x+1 << ": ";
cin >> tempmatrix[x][1];
if(tempmatrix[x][1] > BigLow) {
BigLow = tempmatrix[x][1];
}
}
return BigHigh, BigLow;
}
double indexHighTemp(double BigHigh) {
cout << "\nHighest recorded temperature (according to you) was: " << BigHigh << endl;
}
double indexLowTemp(double BigLow) {
cout << "\nLowest recorded temperature (according to you) was: " << BigLow << endl;
}
main () {
double tempmatrix[12][2];
double BigHigh = 0, BigLow = 0;
getData(tempmatrix, BigHigh, BigLow);
indexHighTemp(BigHigh);
indexLowTemp(BigLow);
return 0;
}
I tried float and double as data types to no avail, I'm completely stumped at this point.
It's a simple if greater than, then make it the value of BigHigh/BigLow, however it does not return successfully to main for the printout of the respective data. Instead, the printout reads 0 for both functions. Any tips on where I go wrong?
#include<iostream>
using namespace std;
double getData(double tempmatrix[][2], double BigHigh, double BigLow) {
for(int x=0;x<12;++x) {
cout << "Enter the highest temperature for Month " << x+1 << ": ";
cin >> tempmatrix[x][0];
if(tempmatrix[x][0] > BigHigh) {
BigHigh = tempmatrix[x][0];
}
cout << "Enter the lowest temperature for Month " << x+1 << ": ";
cin >> tempmatrix[x][1];
if(tempmatrix[x][1] > BigLow) {
BigLow = tempmatrix[x][1];
}
}
return BigHigh, BigLow;
}
double indexHighTemp(double BigHigh) {
cout << "\nHighest recorded temperature (according to you) was: " << BigHigh << endl;
}
double indexLowTemp(double BigLow) {
cout << "\nLowest recorded temperature (according to you) was: " << BigLow << endl;
}
main () {
double tempmatrix[12][2];
double BigHigh = 0, BigLow = 0;
getData(tempmatrix, BigHigh, BigLow);
indexHighTemp(BigHigh);
indexLowTemp(BigLow);
return 0;
}
I tried float and double as data types to no avail, I'm completely stumped at this point.
Share Improve this question edited Mar 4 at 11:35 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 4 at 11:17 Mathieu BayanaMathieu Bayana 11 silver badge 5 |1 Answer
Reset to default 1The return statement was irrelevant, an attempt to make things work.
What is needed: the parameters must be passed a variable reference (an expression that contains a double, an assignable double lvalue).
double&
(double reference).
I give this solution, and also give a second solution with double pointers, as the compiler implements double&
. That is how it must be done in C, as C does not have double&
.
double maxHigh = 0, maxLow = 0;
getData(tempmatrix, maxHigh, maxLow);
indexHighTemp(maxHigh);
indexLowTemp(maxLow);
void getData(double tempmatrix[][2], double& BigHigh, double& BigLow) {
BigHigh = -9999.0;
BigLow = 9999.0
for (int x = 0; x < 12; ++x) {
cout << "Enter the highest temperature for Month " << x+1 << ": ";
cin >> tempmatrix[x][0];
if (tempmatrix[x][0] > BigHigh) {
BigHigh = tempmatrix[x][0];
}
cout << "Enter the lowest temperature for Month " << x+1 << ": ";
cin >> tempmatrix[x][1];
if (tempmatrix[x][1] < BigLow) {
BigLow = tempmatrix[x][1];
}
}
}
This is the same as the explicit code:
double maxHigh = 0, maxLow = 0;
getDataCStyle(tempmatrix, &maxHigh, &maxLow);
indexHighTemp(maxHigh);
indexLowTemp(maxLow);
void getDataCStyle(double tempmatrix[][2], double* BigHigh, double* BigLow) {
*BigHigh = -9999.0;
*BigLow = 9999.0
for (int x = 0; x < 12; ++x) {
cout << "Enter the highest temperature for Month " << x+1 << ": ";
cin >> tempmatrix[x][0];
if (tempmatrix[x][0] > *BigHigh) {
*BigHigh = tempmatrix[x][0];
}
cout << "Enter the lowest temperature for Month " << x+1 << ": ";
cin >> tempmatrix[x][1];
if (tempmatrix[x][1] < *BigLow) {
*BigLow = tempmatrix[x][1];
}
}
}
And some remarks:
- It seems more logical swapping the parameters, low before high:
double& BigLow, double& BigHigh
- Giving BigHigh and BigLow a value inside the function instead of 0 outside, ensures that not coincidentally the outside value is taken should it be an extreme value.
- I gave the passed variables an other name, it could be any double storing expression.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745047472a4608183.html
return
a value. The it should be assigned to a variable (or used in another way) on the caller side. – wohlstad Commented Mar 4 at 11:19return BigHigh, BigLow;
doesn't do what you think it does. Or actually, you seem to have a lot more assumptions, given that the return value is not used inmain()
anywhere. I'd strongly recommend getting a good C++ book to learn from, guessing syntax and how the code should work will only bring you woe and misery. – Yksisarvinen Commented Mar 4 at 11:20double getData
- so this method is supposed to return a double.return BigHigh, BigLow;
- that's not a double. If you want to return multiple things, you need to wrap them into a pair or tuple or something. Returning multiple values from a C++ function – C3roe Commented Mar 4 at 11:21main
. For that your code should look like thisvoid getData(double tempmatrix[][2], double& BigHigh, double& BigLow) {
andreturn BigHigh, BigLow;
should be deleted. It's clear that you are guessing at syntax, that's rarely going to work. – john Commented Mar 4 at 11:37