As the title states.. is it possible to pass two variables back from a float function.. one through 'return variable' & the other through reference? If it is i will post the code.. but after hours of debugging & the program is still not recognizing the changed reference parameter (but the 'return variable' returns the correct value) this is the onlY conclusion i feel i can come too.
Appreciate a quick response! - Chris
GPXtC02
- 2 Contributors
- forum4 Replies
- 5 Views
- 10 Years Discussion Span
- comment Latest Post by GPXtC02
Ancient Dragon 5,243
>>is it possible to pass two variables back from a float function.. one through 'return variable' & the other through reference
Absolutely yes. If you can't get it to work right then post the code. Here is a common way to do it.
int foo( int& x)
{
x = 123;
return 1;
}
int main()
{
int n = 0;
int k = foo(n);
cout << "n = " << n << " k = " << k << "\n";
return 0;
}
GPXtC02
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
float GetHS(float[], int, int&); //Proto-type.
float GetLS(float[], int, int&); //Proto-type.
// /////// //
float GetHS(float(Scores)[], int nRecords, int& indexa) { //Function computes the h
float Highest=Scores[indexa];
for(int a1=0; a1!=(nRecords+1); a1++)
if(Highest<Scores[a1])
{
Highest=Scores[a1];
indexa=a1;
}
return Highest;
}
// /////// //
float GetLS(float(Scores)[], int nRecords, int& indexb) { //Function computes the l
float Lowest=Scores[indexb];
for(int a2=0; a2!=(nRecords+1); a2++)
if(Lowest>Scores[a2])
{
Lowest=Scores[a2];
indexb=indexb+a2;
}
return Lowest;
}
// //////////////////////////////// //
void main(void) {
int IDs[50];
float Scores[50];
int i=0, argOne=0, argTwo=0;
GetData(IDs, Scores, i);
int nRecords=i;
cout<<"The highest test score was "<<GetHS(Scores, nRecords, argOne)<<" by "<<IDs[argOne]<<"."<<endl;
cout<<"The lowest test score was "<<GetLS(Scores, nRecords, argTwo)<<" by "<<IDs[argTwo]<<"."<<endl;
cout<<"The average test score for the class is "<<GetAverage(Scores, nRecords)<<"."<<endl;
}
As the problem revolves the data i'm getting back from the GetLS & GetHS functions i took out the functions that are irrelevant to the problem i'm having but left enough to give a good idea of what i'm after. Problem is after going into GetLS or GetHS (both are similar.. one to find the highestscore & the other the lowest score) The score part comes out with the data declared in argOne&argTwo (0). i.e it never changed. when i put in a 'cout<<indexa' or '*b' too make sure it is finding the right data it tells me I am as it prints out the data i want.. problem is it never returns the index part (argOne & Two) which is what i want.
"<<GetLS(Scores, nRecords, argTwo)<<" the right data fine (using the 'return variable'
but "IDs[argTwo]" doesn't return the correct value.
Thanks
Ancient Dragon 5,243
in GetHS(), line 10, I assume nRecords is the number of valid entries in the array Scores. If that is true then line 12 is incorrect. Should be this: for(int a1=0; a1 < nRecords; a1++)
because arrays are numbered from 0 up to but not including nRecords. Same is true of line 24.
lint 28: indexb=indexb+a2; << wrong. should be indexb = a2;
lines 43, 44 and 45. Split those lines up something like below and see if that solves the problem, after correcting the above problems.
float x = GetHS(Scores, nRecords, argOne);
cout<<"The highest test score was "<<x<<" by "<<IDs[argOne]<<"."<<endl;
GPXtC02
appreciate the help. the above simple syntax errors were the problem