having some probs with my code, just a minor error. if anyone can help my aim-ricco4014, itd be much appreciated, the assignment is due in 2 hrs ><
having some probs with my code, just a minor error. if anyone can help my aim-ricco4014, itd be much appreciated, the assignment is due in 2 hrs ><
Write a program which reads three integer values and orders them.The program will then print them in ascending order
(smallest integer first) and state if there are duplicate inputs.
When executed, if there are no duplicate inputs your program’s output should be as follows:
Please enter integer #1 : 4
Please enter integer #2 : -5
Please enter integer #3 : 10
Output Message: -5 4 10 No duplicates found.
On the other hand, if there are duplicate inputs your program’s output should be as follows:
Please enter integer #1 : 2
Please enter intefer #2 : 4
Please enter integer #3 : 2
Output Message: 2 2 4 Duplicates found.
-----this is what the output should be and this is my code---- for some reason when i put in duplicates it displays them a bunch of times in the output message.
#include <stdio.h>
int main( void )
{
int i1;
int i2;
int i3;
printf("Please enter integer #1:");
scanf("%d", &i1);
printf("Please enter integer #2:");
scanf("%d", &i2);
printf("Please enter integer #3:");
scanf("%d", &i3);
if (i1 <= i2 && i1 <= i3)
{
{
printf("%d ", i1);
}
if (i2 <= i3)
{
printf("%d %d ", i2, i3);
}
if (i3 <= i2)
{
printf("%d %d ", i3, i2);
}
}
if (i2 <= i1 && i2 <= i3)
{
{
printf("%d ", i2);
}
if (i1 <= i3)
{
printf("%d %d ", i1, i3);
}
if (i3 <= i1)
{
printf("%d %d ", i3, i1);
}
}
if (i3 <= i1 && i3 <= i2)
{
{
printf("%d ", i3);
}
if (i1 <= i2)
{
printf("%d %d ", i1, i2);
}
if (i2 <= i1)
{
printf("%d %d ", i2, i1);
}
}
if (i1 == i2 || i2 == i3 || i1==i3)
{
printf("Duplicates Found.\n");
}
if (i1 != i2 && i2 != i3 && i1 != i3)
{
printf("No Duplicates Found.\n");
}
return 0;
}
Last edited by ricco401; 02-06-2008 at 06:00 PM.
Unfortunately for you, we don't do homework.
Fortunately for you, if it's really just a minor error, post it here and I'm sure someone will be able to help you with it.
In other words, we'll help you do your homework, but we won't write it for you.
And posting it here is a much better idea than waiting for someone to contact you with aim. Here, many people can read it with very little effort, and possibly help you out. If it's easy to do something, people will be more inclined to do it.
[edit] And there's another thing -- I managed to read your post before you posted your second post! Imagine that.
Use code tags: [code] code here [/code] [/edit]
[edit=2]Why do you think? If i1==i2, what do you think is going to happen here?for some reason when i put in duplicates it displays them a bunch of times in the output message.
That's right, both conditions are true, and so both if statements will be executed.Code:if (i1 <= i2) { printf("%d %d ", i1, i2); } if (i2 <= i1) { printf("%d %d ", i2, i1); }
I think you're approaching the problem the wrong way. Why don't you actually try to sort the numbers first. If you do that, determining whether there are any duplicates will become much simpler. [/edit]
Last edited by dwks; 02-06-2008 at 06:02 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
:P well thanks, any help would be appreciated
The reason being, of course, that you told the computer to do so.Originally Posted by ricco401
Take the case of 2 2 4 as in your sample input. Which of your if statements will be true? Consequently, which print statements will print? What can you do about it?
PS: Despite the name of your thread, there are no loops in your program. There aren't any such things as if/else loops anyway.
sorry im quite new to the language, so since the input "2 2 4" would satisfy more then 1 if statement, do i need to link them all together or how do i fix it?
Ah, you could try my suggestion. First sort the three numbers. Printing them is very easy, if they're already sorted. Then just compare i1 to i2, and i2 to i3, to determine whether or not there are any duplicates.
If you don't sort the three numbers, you end up with code like this: http://www.cs.mtu.edu/~shene/COURSES...ap03/sort.html
*shudders*
If you do:
Right, so how do you sort three numbers?Code:printf("%d %d %d\n", i1, i2, i3);
Give it a try, and if you can't figure it out, let us know . . . .
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
The fact that "2 2 4" would satisfy more than 1 if statement indicates that you're not asking the right questions.
Currently, you're asking "is i1 less than or equal to i2" and "is i2 less than or equal to i1". You don't want to ask both of these questions, because you'll get misleading answers in some cases. Think through how you would decide which of three numbers is the smallest, using your flowchart-drawing gadget if you like. But by the time you get to the end of your asking-questions-phase, you should have one (and only one!) ordering of the three numbers that "fits". Then, once you know that ordering, you can move on to printing them out.
As to "linking them all together", if you mean by that "hey can I use else statements too", then the answer is definitely yes.