4 Replies - 228 Views - Last Post: 06 August 2012 - 09:37 AM Rate Topic: -----

#1 brittanymegan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-August 12

output of this c++ code

Posted 06 August 2012 - 08:06 AM

] Given the following function de nition, what output will result from the call
foo(5)?
void foo ( int b ) {
i f ( b % 3 > 1 ) {
for ( int i = b ; i > 0 ; i ) {
cout << i /b ;
}
cout << " ! \ n" ;
} e l s e {
while ( b > 0 ) {
cout << b ;
b --;
}
cout << " ?\n" ;
}
}


since 5%3 is greater than one the if statement is completed: I'm assuming that the output is 5/5 4/5 3/5 2/5 1/5

This post has been edited by jimblumberg: 06 August 2012 - 10:12 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: output of this c++ code

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5594
  • View blog
  • Posts: 9,029
  • Joined: 19-March 11

Re: output of this c++ code

Posted 06 August 2012 - 08:09 AM

for ( int i = b ; i > 0 ; i ) {
cout << i /b ;
}



I'm guessing a permaloop here.
Was This Post Helpful? 0
  • +
  • -

#3 brittanymegan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-August 12

Re: output of this c++ code

Posted 06 August 2012 - 08:17 AM

the line for ( int i = b ; i > 0 ; i ) { is meant to be for ( int i = b ; i > 0 ; i -- ) {
Was This Post Helpful? 0
  • +
  • -

#4 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: output of this c++ code

Posted 06 August 2012 - 09:32 AM

Please use the code tags on the forum.

void foo ( int b )
{
	if ( b % 3 > 1 )
	{
		for ( int i = b; i > 0; i-- )
		{
			cout << i / b;
		}
		cout << " !\n";
	} else {
		while ( b > 0 )
		{
			cout << b;
			b--;
		}
		cout << " ?\n" ;
	}
}


5 % 3 is the remainder of 5 / 3. Since things are automatically converted to integers, 5 % 3 = 2. Therefore, the first conditional is true and we continue within'.

First up, we have the loop for ( int i = b; i > 0; i-- ) which initially sets i to 5 and decrements i by 1 after each cycle. Therefore, it is essentially the same as:
cout << 5 / 5;
cout << 5 / 4;
cout << 5 / 3;
cout << 5 / 2;
cout << 5 / 1;


Because these are not literals, they will be calculated and again converted into integers. Therefore, they will produce the output of 10000

Afterwards, we have cout << " !\n"; before finally reaching the end of the function.

To sum everything up, the output of the function foo(5); would be:

Quote

10000 !

Note that there is also a line-break after this.
Was This Post Helpful? 1
  • +
  • -

#5 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: output of this c++ code

Posted 06 August 2012 - 09:37 AM

EDIT:

Sorry, I wrote the numbers backwards. The loop would essentially be identical to:
cout << 5 / 5;
cout << 4 / 5;
cout << 3 / 5;
cout << 2 / 5;
cout << 1 / 5;
Which would produce 10000, as I stated above.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1