d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Very Entry Level Cs Problem C++
Add Reply New Topic New Poll
Member
Posts: 2,664
Joined: Apr 29 2009
Gold: 255.00
Oct 9 2013 11:02pm
Here is a function we are given:

int smallest(int x, int y, int z)
{
if (x > y && x > z)
return x;
else
if (y > x && y > z)
return y;
else
if (z > x && z > y)
return z;
else
return 0;
}

I am supposed to rewrite the function without using a nested if... I'm confused by this. I feel like there are no nested ifs, though it might seem that way due to the way he indented. This is syntactically identical to:

int smallest(int x, int y, int z)
{
if (x > y && x > z)
return x;
else if (y > x && y > z)
return y;
else if (z > x && z > y)
return z;
else
return 0;
}

is it not? I'm really not sure why, but when we have an "else if" statement, my prof wants us to put the if part on the next line.

Is there a way to rewrite this without using any kind of potentially nested if statements?

Thank you.
Member
Posts: 28,331
Joined: Jun 9 2007
Gold: 11,700.00
Oct 9 2013 11:26pm
Quote (smashT @ 10 Oct 2013 05:02)
...

int smallest(int x, int y, int z)
{
111  if (x > y && x > z)
111  return x;
111  else
222  if (y > x && y > z)
222  return y;
222  else
333  if (z > x && z > y)
333    return z;
333  else
333    return 0;
}

I am supposed to rewrite the function without using a nested if... I'm confused by this. I feel like there are no nested ifs, though it might seem that way due to the way he indented. ...
...


if i understand that right you have three levels of ifs here, that is the statements '333' are nested within the '222' and those nested within the '111'

not c++ but i hope you understand anyhow:

...
int returnvalue
returnvalue=0
if (x>y && x>x) returnvalue=x end
if (y>x && y>z) returnvalue=y end
if (z>x && z>y) returnvalue=z end
return returnvalue
..

this is without nested ifs, just ensure that you put it into proper c++ form
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 9 2013 11:32pm
Quote (smashT @ Oct 10 2013 01:02am)
I am supposed to rewrite the function without using a nested if... I'm confused by this. I feel like there are no nested ifs, though it might seem that way due to the way he indented. This is syntactically identical to:


s it not? I'm really not sure why, but when we have an "else if" statement, my prof wants us to put the if part on the next line.


in the C family there is no such thing as "else if". your prof is correct, they are nested if statements.

brmv's answer should be sufficient. alternatively, you can compare each value only once:

int smallest = x;
if (smallest > y) smallest = y;
if (smallest > z) smallest = z;


/edit1: why is your function called "smallest" if it returns the largest?

This post was edited by carteblanche on Oct 9 2013 11:36pm
Member
Posts: 2,664
Joined: Apr 29 2009
Gold: 255.00
Oct 9 2013 11:32pm
So does "else if" imply a nested if?

Your function does make sense though. Does the "end" just break the if statement?

I guess I could just write something like:

int smallest(int x, int y, int z)
{
if (x > y && x > z)
return x;
if (y > x && y > z)
return y;
if (z > x && z > y)
return z;
}


Quote (carteblanche @ Oct 9 2013 09:32pm)
in the C family there is no such thing as "else if". your prof is correct, they are nested if statements.

brmv's answer should be sufficient. alternatively, you can compare each value only once:

int smallest = x;
if (smallest > y) smallest = y;
if (smallest > z) smallest = z;



that's the max i think  :P


Ohh, that's a good way to do it too, thanks.

I also just realized my indentations in my first post didn't work :(

This post was edited by smashT on Oct 9 2013 11:35pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 9 2013 11:37pm
Quote (smashT @ Oct 10 2013 01:32am)
So does "else if" imply a nested if?

Your function does make sense though. Does the "end" just break the if statement?

I guess I could just write something like:

int smallest(int x, int y, int z)
{
if (x > y && x > z)
  return x;
if (y > x && y > z)
  return y;
if (z > x && z > y)
  return z;
}




Ohh, that's a good way to do it too, thanks.

I also just realized my indentations in my first post didn't work :(


use the [code] blocks for indentations

and make sure your function either uses >= or default method in case more than 1 value is the smallest
Member
Posts: 2,664
Joined: Apr 29 2009
Gold: 255.00
Oct 9 2013 11:39pm
Quote (carteblanche @ Oct 9 2013 09:37pm)
use the [code] blocks for indentations

and make sure your function either uses >= or default method in case more than 1 value is the smallest


Yeah thanks, it is supposed to return 0 if none of the other statements are true. I didn't add that in my first reply. And it definitely needs a new name...
Member
Posts: 28,331
Joined: Jun 9 2007
Gold: 11,700.00
Oct 9 2013 11:40pm
Quote (carteblanche @ 10 Oct 2013 05:32)
...
/edit1: and i think your code returns the max?
/edit2: dont forget to use >= in case you have more than 1 smallest


@ edit 1 - agreed, except for the case that there are two or three equally large
@ edit 2 - it seems that the code is to return 0 if there is no largest

and @ op: yes, the 'end' is there to indicate that the if statement is finished
as said, i never used c++ so you need to put it into the correct form

This post was edited by brmv on Oct 9 2013 11:45pm
Member
Posts: 2,664
Joined: Apr 29 2009
Gold: 255.00
Oct 9 2013 11:50pm
Quote (brmv @ Oct 9 2013 09:40pm)
@ edit 1 - agreed, except for the case that there are two or three equally large
@ edit 2 - it seems that the code is to return 0 if there is no largest

and @ op: yes, the 'end' is there to indicate that the if statement is finished
as said, i never used c++ so you need to put it into the correct form


Yeah thanks, I think your first solution will work fine.

I have several versions of the same code all compiling and performing properly, so I think I've got it figured out.

Thanks to both of you guys.
Go Back To Homework Help Topic List
Add Reply New Topic New Poll