Thread: Odd problem: returning array from function

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    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.
    Last edited by disorder; 04-19-2008 at 03:05 PM. Reason: typo

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    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].
    Last edited by disorder; 04-19-2008 at 03:12 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You access r[4], so you need to declare r as a five-element array. And a. And b.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    All your arrays which are [4] need to be at least [5]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    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. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by disorder View Post
    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. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    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. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by disorder View Post
    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. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    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. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did the little indentation lesson teach you nothing?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM