8 Replies - 798 Views - Last Post: 15 October 2009 - 09:44 PM Rate Topic: -----

#1 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

I'm tracing the following program but my output isnt making it

Posted 15 October 2009 - 07:41 PM

Program's output when run:
5
20
25
500
25
500
1000
0
500
0
25
25
25
25

My output when I trace the program by hand:
5
20
25
500
25
500
1000
50
500
50
25
50
50
25

Where am I going wrong?

#include "DarkGDK.h"

void func1 (int& x, int& y);
void func2 (int x, int& y); 
void func3 (int& x, int& y);
void printValues (int x, int y); 

void DarkGDK(){

	int x= 5, y=20, z=10;
	printValues (x,y); 
	func1(x,y);
	printValues (x,y); 
	func2(y,z); 
	printValues (y,z);
	func3 (z,x); 
	printValues (z,x);
	dbWaitKey(); 
}

void func1 (int& a, int& b){
	a+=b; 
	b*=a;
	printValues(a,b);
}
void func2 (int a, int& b){
	b/= a;
	a*= 2; 
	printValues(a,b); 
}
void func3 (int& a, int& b){
	a=b; 
	b=a;
	printValues(a,b);
}
void printValues (int a, int b){
	dbPrint (dbStr(a));
	dbPrint (dbStr(b)); 
}


Is This A Good Question/Topic? 0
  • +

Replies To: I'm tracing the following program but my output isnt making it

#2 EdwinNameless   User is offline

  • D.I.C Addict
  • member icon

Reputation: 128
  • View blog
  • Posts: 723
  • Joined: 15-October 09

Re: I'm tracing the following program but my output isnt making it

Posted 15 October 2009 - 09:04 PM

View Postizic, on 15 Oct, 2009 - 06:41 PM, said:

Where am I going wrong?



The call to func2 is "func2(y,z); ", which means you are calling "func2(500,10)".

Therefore, what is the value of "b/=a" in func2?
Was This Post Helpful? 0
  • +
  • -

#3 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: I'm tracing the following program but my output isnt making it

Posted 15 October 2009 - 09:10 PM

b=10, a=500

500/10 = 50; b=50;

so if this is true, why is the next number 0 in the line of output after 1000 instead of 50?
Was This Post Helpful? 0
  • +
  • -

#4 EdwinNameless   User is offline

  • D.I.C Addict
  • member icon

Reputation: 128
  • View blog
  • Posts: 723
  • Joined: 15-October 09

Re: I'm tracing the following program but my output isnt making it

Posted 15 October 2009 - 09:15 PM

View Postizic, on 15 Oct, 2009 - 08:10 PM, said:

b=10, a=500

500/10 = 50; b=50;

so if this is true, why is the next number 0 in the line of output after 1000 instead of 50?


Nope. func2 actually says:

 b/=a;

Was This Post Helpful? 0
  • +
  • -

#5 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: I'm tracing the following program but my output isnt making it

Posted 15 October 2009 - 09:18 PM

which means b/0?

so if it was b=/a, it would be 500/10?
Was This Post Helpful? 0
  • +
  • -

#6 EdwinNameless   User is offline

  • D.I.C Addict
  • member icon

Reputation: 128
  • View blog
  • Posts: 723
  • Joined: 15-October 09

Re: I'm tracing the following program but my output isnt making it

Posted 15 October 2009 - 09:23 PM

View Postizic, on 15 Oct, 2009 - 08:18 PM, said:

which means b/0?

so if it was b=/a, it would be 500/10?


"b/=a " means "b = b / a;"

So that would be 10/500. And anticipating the next question: you are working with ints.
Was This Post Helpful? 1
  • +
  • -

#7 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: I'm tracing the following program but my output isnt making it

Posted 15 October 2009 - 09:27 PM

View PostEdwinNameless, on 15 Oct, 2009 - 08:23 PM, said:

View Postizic, on 15 Oct, 2009 - 08:18 PM, said:

which means b/0?

so if it was b=/a, it would be 500/10?


"b/=a " means "b = b / a;"

So that would be 10/500. And anticipating the next question: you are working with ints.


So "0" takes the place of the decimal i got when divided both numbers because im using an int?
Was This Post Helpful? 0
  • +
  • -

#8 EdwinNameless   User is offline

  • D.I.C Addict
  • member icon

Reputation: 128
  • View blog
  • Posts: 723
  • Joined: 15-October 09

Re: I'm tracing the following program but my output isnt making it

Posted 15 October 2009 - 09:34 PM

View Postizic, on 15 Oct, 2009 - 08:27 PM, said:

View PostEdwinNameless, on 15 Oct, 2009 - 08:23 PM, said:

View Postizic, on 15 Oct, 2009 - 08:18 PM, said:

which means b/0?

so if it was b=/a, it would be 500/10?


"b/=a " means "b = b / a;"

So that would be 10/500. And anticipating the next question: you are working with ints.


So "0" takes the place of the decimal i got when divided both numbers because im using an int?


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

#9 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: I'm tracing the following program but my output isnt making it

Posted 15 October 2009 - 09:44 PM

View PostEdwinNameless, on 15 Oct, 2009 - 08:34 PM, said:

View Postizic, on 15 Oct, 2009 - 08:27 PM, said:

View PostEdwinNameless, on 15 Oct, 2009 - 08:23 PM, said:

View Postizic, on 15 Oct, 2009 - 08:18 PM, said:

which means b/0?

so if it was b=/a, it would be 500/10?


"b/=a " means "b = b / a;"

So that would be 10/500. And anticipating the next question: you are working with ints.


So "0" takes the place of the decimal i got when divided both numbers because im using an int?


Exactly.


Nice! I've now "leveled up" in the art of C++ and earned an extra +5 in intellect and will power. I'm gonna need every point anyway because its definitely gonna get harder but its nothing I can't handle.

Also, a couple more questions:

I'm still confused as to what a function arguments, parameters, and function calls are/look like. Can you give an example of what each look like along with the difference between arguments and parameters? Also, whats the difference between value and reference parameters.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1