• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

URGENT!!! Need help with C homework!!!!!

Scrapster

Diamond Member
I'm writing a program that sorts 3 numbers that you have entered in an ascending order.

My problem is that if the first and third numbers happen to be the same, the program will output the second number twice. BUT I need the program to output all three numbers.

Ex. of what it's SUPPOSED to do:

Input
Enter first# 5
enter second# 2
enter third# 5

output
first number is 2
second number is 5
third number is 5

Ex. of what it is doing INSTEAD

input
first# 5
second# 2
third# 5

output
first number is 2
second number is 2 <-----Adds an extra 2 instead of the other 5
third number is 5

The program does some other sorting stuff, which is working ok. This is my lone problem.

If you can give me some pointers I'd appreciate it.

link to program

Scraps
 
The way your program is designed, it will compare a value to itself, which will screw everything up. If you want to use &quot;if&quot; statements, find the middle value. When you know the middle value, you know the higher and lower value, so just set them all at one time.

Example:

if ((num2 <= num1) &amp;&amp; (num 2 >=num3))
{
num2=middle
num1=highest
num3=lowest
}

Keep on doing it for the other cases.
 
I think I had that EXACT same problem to solve like back in first or second year C.
If the monitor wasnt burning my eyes id read it all.
good luck
 
alldiff(f, s, t)
{
if(s < t)
print f, s, t;
else
print f, t, s;
}

twosame(f, s, t)
{
if(f < t)
print f, s, t;
else
print t, f, s;
}

main()
{
read values in f, s, t;

12 = f == s;
13 = f == t;
23 = s == t;

if(12 &amp;&amp; 23)
{
all numbers are the same;
return;
}

if(12)
{
twosame(f, s, t);
return;
}

if(13)
{
twosame(f, t, s);
return;
}

if(23)
{
twosame(s, t, f);
return;
}

if(f < s &amp;&amp; f < t)
{
alldiff(f, s, t);
return;
}
else if(s < f &amp;&amp; s < t)
{
alldiff(s, f, t);
return;
}
else
{
alldiff(t, f, s);
return;
}
}
 
Dangit, I was afraid of that.

I'll try to correct it the way you guys described.

I have a feeling like I'm typing way more code than necessary.
 
Try this.

------------------------------------------------------------------
int fls = 0; // first < second
int flt = 0; // first < third
int slt = 0; // second < third

if(first < second)
fls = 1;
if(first < third)
flt = 1;
if(second < third)
slt = 1;


if(fls &amp;&amp; slt)
{
low = first;
middle = second;
high = third
}
else
if(!fls &amp;&amp; !slt)
{
low = third;
middle = second;
high = first
}
else
if(flt &amp;&amp; !fls &amp;&amp; slt)
{
low = second;
middle = first;
high = third;
}
else
if(!flt &amp;&amp; slt &amp;&amp; !fls)
{
low = second;
middle = third;
high = first;
}
else
if(flt &amp;&amp; !slt)
{
low = first;
middle = third;
high = second;
}
else
if(!flt &amp;&amp; fls)
{
low = third;
middle = first
high = second;
}


---------------------------------------------

let me know if the logic is too tricky.

Drew
 
Scrapster, alldiff and twosame are functions. I did it that way because the logic was the same. Using a function for a section of logic that is going to be used more than once reduces code size and reduces the chances for error and make the code easier to read. If you have not covered them yet you could just replace the function call with the actual logic.
 
1/2 or more of programming is debugging. You'd would feel much better if you can debug the program by yourself. Try adding &quot;watch&quot; to all 3 number variables and track them as trace thru the program line by line. BTW, your program looks unnecessaily long for such a small job. I would suggest you sort them as they are entered.


input# A
store A in tempvar
store A in highVar

input# B
store B in tempvar
compare highvar and tempvar, (if then statement)
either tempvar = midvar, or {highvar=midvar, tempvar=highvar)

input#C
store C in tempvar
compare highvar and tempvar w >=, (if then statement)
either tempvar = midvar, or {midvar = lowvar, highvar=midvar, tempvar=highvar)
 
Back
Top