pass a reference

my function does not return modified values

Page 1 of 1

7 Replies - 1188 Views - Last Post: 08 July 2008 - 04:40 PM Rate Topic: -----

#1 thompson03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 12-June 08

pass a reference

Posted 08 July 2008 - 02:36 PM

Hi, I'm having problems getting my function to return the "new" value of a variable.

The function works, I put a cout in the function and it is getting the correct value,
but the cout in main() is still the original value of the variables

I declared my function in a header file

long gcd(long a, long b, long&x, long&y) it does not seem to matter if i have long &x, long& x or long&x

in my function file i have
long gcd(long a, long b, long&x, long&y) again the spacing doesnt seem to matter
{procedure}

in my main file
i have gcd(a,b,x,y)

and cout gcd(ab,x,y) << x << y and the x and y are not changed
even though a cout in the function file shows the function is working.

Is there another location that the "&" is needed?

The book i'm reading gives one simple example of reference, based on that, and websearches , everything looks hunky dorry, but it isnt working :(

i've included the files

nope, i guess not, the website wont allow .cpp extension in attachement or something

i hesitate to copy all the code, because it is quite long and in three files. but i will if asked

Is This A Good Question/Topic? 0
  • +

Replies To: pass a reference

#2 Hyper_Eye   User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: pass a reference

Posted 08 July 2008 - 03:00 PM

You may have to paste the code because it doesn't seem like your doing anything wrong. Here is a simple example I just wrote that works:

#include <iostream>

using namespace std;

void increment(int &a)
{
  a++;
}

int main()
{
  int num = 0;

  increment(num);
  cout << num << endl;

  return 0;
}

Was This Post Helpful? 0
  • +
  • -

#3 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: pass a reference

Posted 08 July 2008 - 03:01 PM

If it's that big, you might not fit it all into one post...

Try adding them to a zip, and uploading the zip :)
Was This Post Helpful? 0
  • +
  • -

#4 thompson03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 12-June 08

Re: pass a reference

Posted 08 July 2008 - 03:18 PM

I am compiling using the cl program that runs in the command line for visual basic (got sick of creating a new project for every snippet of code) Running windows vista home basic.

The program is an extended exercise from C++ for Mathematicians by Scheinerman

the header and function code were given in the book, I wrote the main() and the extra cout in the function

Here is header file
#ifndef GCD_H
#define GCD_H

/**
*Calculate the greates common divisor of two integers.
*Note: gcd (0,0) will return 0 and print an error message.
*@param a the first integer
*@param b the second integer
*@return the greatest common divisor of a and b
*/

long gcd(long a, long b);
long gcd(long a, long b, long&x, long&y);

#endif



the function file

#include <iostream>
using namespace std;

long gcd(long a, long b, long&x, long&y)
{
	long d; // place to hold final gcd

	// in case b=0, we have d=|a|, x =1 or -1, y arbitrary (say, 0)
	if (b==0) 
	{
		if (a<0)
		{
			d = -a;
			x = -1;
			y = 0;
		}
		else
		{
			d = a;
			x = 1;
			y = 0;
		}
		return d;
	}

	// if b is negative, here is a workaround

	if (b<0) 
	{
		d = gcd (a,-b,x,y);
		y = -y;
		return d;
	}

	// if a is negative, here is a workaround

	if (a<0)
	{
		d = gcd (-a,b,x,y);
		x = -x;
		return d;
	}

	// set up recursion

	long aa = b;
	long bb = a%b;
	long qq = a/b;
	long xx,yy;

	d = gcd(aa,bb,xx,yy);
	x = yy;
	y = xx - qq*yy;
	cout << x <<","<<y<<","<<xx<<","<<yy<<"\n"; // this verifies x and y are correct
	return d;
}



and the main file

#include "gcd.h"
#include <iostream>
using namespace std;

/**
*A program to find d = ax + by given a, b
*Linear Diophantine Equations
*/


int main ()
{
	long a,b,x,y;

	cout << "Enter the first number --> ";
		cin >> a;
		cout <<  endl;

	cout  << "Enter the second number --> ";
		cin >> b;
		cout << endl;

		x=1;
		y=1;

	cout << "The gcd of " << a << " and " << b <<  " is "
		<< gcd(a,b,x,y) << "=" << a << "x" << x 
		<< "+" << b << "x" << y << endl;
	return 0;
}


This post has been edited by thompson03: 08 July 2008 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#5 Hyper_Eye   User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: pass a reference

Posted 08 July 2008 - 03:46 PM

I believe you are experiencing a scope problem. Try it this way:

	long gcd_val;

	gcd_val = gcd(a,b,x,y);

	cout << "The gcd of " << a << " and " << b <<  " is "
		<< gcd_val << "=" << a << "x" << x
		<< "+" << b << "x" << y << endl;

Was This Post Helpful? 0
  • +
  • -

#6 thompson03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 12-June 08

Re: pass a reference

Posted 08 July 2008 - 04:06 PM

Ok!
that fixed it :)


now can you tell my, " why did that work ???"

since the value was ok anyway, and x and y where the problem, how in the heck did that fix x and y!!
Was This Post Helpful? 0
  • +
  • -

#7 Hyper_Eye   User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: pass a reference

Posted 08 July 2008 - 04:25 PM

View Postthompson03, on 8 Jul, 2008 - 06:06 PM, said:

Ok!
that fixed it :)


now can you tell my, " why did that work ???"

since the value was ok anyway, and x and y where the problem, how in the heck did that fix x and y!!


Your values were getting set. They just weren't set when you printed them because of the way cout works. cout actually works from right to left as is evident by <<. Try this code and you will understand:

#include <iostream>

using namespace std;

int increment(int &num)
{
  num++;

  return 0;
}

int main()
{
  int a = 0;
  int b = 0;
  int c = 0;

  a++;
  cout << a << endl;

  cout << increment(b) << b << endl;

  cout << c << increment(c) << endl;

  return 0;
}


Keep in mind that the return from the function will be first on the second print and it will be second on the third one. You would expect to see:

1
01
00

You will actually see:

1
00
10

This post has been edited by Hyper_Eye: 08 July 2008 - 04:28 PM

Was This Post Helpful? 1
  • +
  • -

#8 thompson03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 12-June 08

Re: pass a reference

Posted 08 July 2008 - 04:40 PM

Ok, I get it.

Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1