Tuesday, July 28, 2009

Can someone help me with this C++ code. I tried to compile it but it does not work, any help would be great.?

This is what i am trying to do.


Write the following function to sort three numbers in increasing order





Void sort(int num1, int num2, int num3)





The main program should read the three numbers from key board and invoke the above function.








Here is the program.





void sort(int a, int b , int c)


{


int t;


if (a%26gt;b)


{


t=b;


b=a;


a=t;


}


if (a%26gt;c)


{


t=c;


c=a;


a=t;


}





if (b%26gt;c)


{


t=b;


b=c;


c=t;


}

Can someone help me with this C++ code. I tried to compile it but it does not work, any help would be great.?
I think you should use use pointers concept in order to change actual variables that you are entering as parameters.


do something like:


void sort(int %26amp;a, int %26amp;b , int %26amp;c)


{


int t;


if (a%26gt;b)


{


t=b;


b=a;


a=t;


}


if (a%26gt;c)


{


t=c;


c=a;


a=t;


}





if (b%26gt;c)


{


t=b;


b=c;


c=t;


}


}
Reply:Your second if doesn't work under all circumstances.


You're missing a closing brace as well.





You should use a nested if.


If (a%26gt;b)


If (a%26gt;c)





Try something like that.
Reply:hmmm i got no clue, i no C++ but this seems all correct, o havent done the sort function though, but everything else seems perfect....
Reply:you need to close out your function with a }
Reply:Ahh, Yes the old C++ code. Wait, Are we talking about cup sizes?
Reply:? WHAT ?
Reply:ok, there is a much simpler way of doing this. But to answer you question, right now you are missing the } at the end of the function.





Tip: You might want to name your variables so that they dont get mixed up.





you could try something like this:





void largest (int a, int b, int c)


{





if (a %26gt; b %26amp;%26amp; a %26gt; c)


cout %26lt;%26lt; "the largest number is " %26lt;%26lt; a %26lt;%26lt; "." %26lt;%26lt; endl;


else if (b %26gt; c %26amp;%26amp; b %26gt; a)


cout %26lt;%26lt; "The largest number is " %26lt;%26lt;b %26lt;%26lt; "." %26lt;%26lt; endl;


else if (c %26gt; a %26amp;%26amp; c %26gt; b)


cout %26lt;%26lt; "The largest number is " %26lt;%26lt; c %26lt;%26lt; "." %26lt;%26lt; endl;


else if ( a == b %26amp;%26amp; b == c)


cout %26lt;%26lt; "All numbers are equal" %26lt;%26lt; endl;


}





That is one of the simplest ways to do it. You can use that to apply to any three numbers.
Reply:I always say, if you dont specify the error how can anyone help. It seems all these answers are shots in the dark.


You can have more success if you provide the exact error message.


You could be missing a library (#include).


You could be missing a required prototype specifier.


It could be the missing scope specifier.


What does the compiler say when it errors?
Reply:First of all people shouldn't answer a question with "? WHAT ?" or whatever the first person said. If you dont know it then just dont answer.


Secondly, go to the public library and get a book on C++. I know mine has a few books about it. If you can't find a book there you can always try to find an eBook online somewhere.
Reply:I don't have a C++ compiler, but I believe you are missing a closing brace after your last assignment procedure. You have an open brace above int t; but no matching closing brace. Hope this helps you out!


No comments:

Post a Comment