Pass-by-reference code not giving the correct answers

Mar 10, 2009 at 7:27pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Write a subfucntion called "diff_poly" to differentiate a polynomial.
//Assume for simplicity, he polynomial (with integers conefficients) 
//is of degree no more than 5, you could use 
//"bool diff_poly(intt& a, int& b, int& c, int& d, int& e, int& f)" as the prototype.
//Ask the user for the coefficients (e.g. 0 5 -2 0 0 6 for 
//5x^4 -2x^3 + 6) in the main function.  Then pass them to "diff_poly"
//via pass-by-reference on the coefficents. 
//The subfunction differentiate the polynomial and return true if the result is
//a none-zero polynomial and pass the coefficents of the result out to the main.
//Then main takes the coefficients and print the result on screen.  It will also
//indicate if the derivative is zero function or not based on the return value from
//diff_poly

#include <iostream>
using namespace std;  

bool diff_poly (int& a, int& b, int& c, int& d, int& e, int& f){
        a=a*5;
	b=b*4;
	c=c*3;
	d=d*2;
	e=e*1;
	f=f*0;
        if(a+b+c+d+e+f>0)
	{
		return true;
	}
   return false;
 }
	

int main()
{
    bool diff;
    int a,b,c,d,e,f;
    char again;
    do
    {  // run program untill user wants to stop
      cout<<"\nEnter six integers positive or negative:";
      cin>>a,b,c,d,e,f;
      diff=diff_poly(a,b,c,d,e,f);
	if(diff=true){
		cout<<" "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<endl;
      }
       cout<<"\nDo you want to run progaram again? (y/n):"<<endl;
      cin>>again;
    }  
    while((again=='y') || (again=='Y'));
    cout<<"\nBye\n";
    return 0;
}


I am not sure but it seems that my sub function is not working. These are the results I get when running the program
5 538057296 1055088376 269033992 -1210598631 0
That is after entering the integers 1 2 3 4 5 6. It looks like it sees the first and last integer but not the middle ones. Can someone please help me out?
Mar 10, 2009 at 7:56pm
are you sure the problem is with your diff_poly and not with your cin line?

try:

cin >> a >> b >> c >> d >> e >> f;
Mar 10, 2009 at 8:10pm
I figured that out as soon as I posted but now I cant get it to return a true or false it just always returns the numbers. I think something is up with the "if" statement. Thanks for the help.
Mar 10, 2009 at 8:12pm
There's no point in having two threads about the exact same problem.

*stops replying to this one*
Mar 10, 2009 at 8:18pm
maybe try if( (a+b+c+d+e+f) >0 )
Topic archived. No new replies allowed.