# Thread: Odd problem: returning array from function

1. ## Odd problem: returning array from function

Hey all, I'm new here.

My little brother came to me for help with some high school programming homework and I've only taken a few college courses on programming and it's kind of out of my reach (sad isn't it?).
He has to read data from a text file setup as such into arrays:

dc[1]
gt[1] snr[1]
gt[2] snr[2]
gt[3] snr[3]
gt[4] snr[4]
gt[5] snr[5]
dc[2]
gt[6] snr[6]
gt[7] snr[7]

Five values of 'gt' with (a corresponding 'snr' value) for each 'dc'. There are 5 dc's. Then he has to send the arrays to a void function to calculate the slope, intercept, and r^2 of the line snr vs. gt for each dc group. So values 1-5 get one slope, intercept, and r squared, then the values 5-10 get a slope...etc etc.

Now you'll see in the code I used nested for loops to get this done, however my slope for the first dc group is correct IN THE FUNCTION but after the loop finishes and the data is sent back to the main program it changes. You can see in the program itself it prints the original data, then a list of calculated data (slope, then intercept, then r^2) from the array, and then it prints the formated table that is in the main program. Can anyone see what is going on here? We really appreciate it.

2. Also to quickly point out the problem.
slope[0] = 5.3 in the function
but in the main program formatted table you see it as 0.99 which is a[0].

3. You access r[4], so you need to declare r as a five-element array. And a. And b.

4. All your arrays which are [4] need to be at least [5]

5. Since this is C++, it's better to use std::vector, and if not Visual Studio, then use the member function .at to find errors.
And you or your little brother should really learn to indent:

Code:
```using namespace std;
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>

void linereg(double[],double[], double[], double[], double[]);
int main()
{
double dc[4],gt[25],snr[25], b[4], a[4], r[4];
char skiplines[80];
int i,j,z=0;
ifstream indata("noisedata.txt");
for (i=0;i<=3;i++)
{
indata.getline(skiplines, 80);
}
for (i=0;i<=4;i++)
{
indata >> dc[i];
cout << dc[i] <<endl;
for (j=0;j<=4;j++)
{
indata>>gt[z]>>snr[z];
cout << gt [z] << "   " << snr[z] << endl;
z++;
}
}

cout<<endl;

//cout << setw(20) << "dc(atoms/cc*10e-17)" << setw(8) << "slope" << setw(12)
//<< "intercept" << setw(12) << "r-squared" << endl;
//cout << "-------------------------------------------------" << endl;

linereg(gt, snr, a, b, r);

for(i=0;i<=4;i++){
cout << fixed << setprecision(1) << setw(12) << dc[i] << setprecision(2) << setw(15) << a[i] << setw(10) << b[i] << setw(14) << setprecision(4) << r[i] << endl;
}

system("pause");
return 0;
}

void linereg(double gt[], double snr[], double slope[], double intercept[], double rsqrd[])
{
int i, j, k=0;
double xsum=0., ysum=0., xave, yave, x2sum=0., y2sum=0., xysum=0.;
double n = 5, r[4];
for(j=0;j<5;j++){
xsum=0., ysum=0., xave, yave, x2sum=0., y2sum=0., xysum=0.;
for(i=0;i<5;i++)
{
xsum+=gt[k];
ysum+=snr[k];
x2sum+=gt[k]*gt[k];
y2sum+=snr[k]*snr[k];
xysum+=gt[k]*snr[k];
k++;
}
xave=xsum/n;
yave=ysum/n;

slope[j]=(xysum-yave*xsum)/(x2sum-xave*xsum);
cout << slope[j] << endl;
intercept[j]=yave-slope[j]*xave;
cout << intercept[j] << endl;
r[j]=(xysum-n*xave*yave)/(sqrt(x2sum-n*xave*xave)*sqrt(y2sum-n*yave*yave));
rsqrd[j]=r[j]*r[j];
cout << rsqrd[j] << endl;}
cout<<endl;
}```

6. Right! the defining of an array's space doesn't start at zero like the actual array counting does. This entire time I was getting down on myself, being an elec engineer and not being able to do high school programming!!! Thanks!

I guess the second part of this assignment also needs correcting. After the formatted data is printed, the user has to input a value of snr which creates five gt values that correspond to this one constant snr value. That equation is: gt[i]=(snrInput - b[i])/a[i]
Where b[i] and a[i] are the intercept and slope from before.

These five new gt values are then put into the same function linereg. This time however it wants the new gt[i] values versus dc[i]. The original function calculates snr[i] vs. gt[i]. My brother just called the function again this time using the gt variable where snr was and dc where gt was. It seems like it would work, and it does for the new intercept which turns out to be correct. The slope for this new line is off, the new slope is a7[0] in the program.

Any insight on this one? thanks again

7. Originally Posted by disorder
It seems like it would work, and it does for the new intercept which turns out to be correct. The slope for this new line is off, the new slope is a7[0] in the program.

Any insight on this one? thanks again
You compute five of each kind of variable, but you only have room for one -- those extras have to go somewhere; and in this case, they go on top of a7. Try again, but without a loop maybe.

8. The second time the function is called, it is taking 5 gt and 5 dc values and finding a single slope for them slope[0] and single intercept intercept[0].

Yes, it does do this FIVE more times even though I only have five new gt values and five dc values. However, those four extra slopes and intercepts are assigned to slope[1-4] and intercept [1-4] thus slope[0] should be uneffected. The guidelines he was given are rather vague but they do point out that it has to use the original function 'linereg' for the second calculation.

9. Originally Posted by disorder
However, those four extra slopes and intercepts are assigned to slope[1-4] and intercept [1-4] thus slope[0] should be uneffected.
I think you missed the point: slope[1] through slope[4] and intercept[1] through intercept[4] do not exist, consequently writing something there can only spread despair and sorrow throughout the land. If you must reuse the function, you must send an array of five elements into slope and intercept and rsqrd.

10. C++ in a high school course? And using arrays? Ugh. If it's an introduction to programming, they'd be better off using something like Python. If they're teaching C++, they should take Dr. Stroustrup's advice.

http://www.research.att.com/~bs/new_learning.pdf

11. Did the little indentation lesson teach you nothing?