Join 149,925 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,929 people online right now. Registration is fast and FREE... Join Now!
Below is my example for 3 integers, but my problem is when I need to input 8 integers and the Output must be: a) Ascending b) Descending c) Highest No. d) Lowest No.
that would be very long to type since you will have 8! (Eight Factorial) Combinations = 40320 Combinations... Is there a way to shorten this up? without using the Bubble Sort or Arrays? I am using the old version of Turbo C v2.01... plz help me... it would mean a lot to me thx...
#include <stdio.h> main() { int a,b,c; clrscr(); scanf("%d %d %d",&a,&b,&c); if ((a<b)&&(b<c)) printf("%d %d %d\n%d %d %d\n%c\n%a",a,b,c,c,b,a); else if ((a<c)&&(a<b)&&(c<b)) printf("%d %d %d\n%d %d %d\n%b\n%a",a,c,b,b,c,a); else if ((a<b)&&(b<a)&&(c<a)) printf("%d %d %d\n%d %d %d\n%a\n%b",b,c,a,a,c,b); else if ((b<a)&&(b<c)&&(a<c)) printf("%d %d %d\n%d %d %d\n%c\n%b",b,a,c,c,a,b); else if ((c<b)&&(c<a)&&(a<b)) printf("%d %d %d\n%d %d %d\n%a\n%c",c,b,a,a,b,c); else if ((c<a)&&(c<b)&&(a<b)) printf("%d %d %d\n%d %d %d\n%b\n%c",c,a,b,b,a,c); getch(); }
This post has been edited by acbarbosa: 8 Jan, 2008 - 05:27 AM
Is it simply the bubble sort you are not allowed to use? Or any standardized sorting algorithm?
not at all... But if I use the bubble sort, it will not be then connected with our topic "using IF-Else"... The program must have "If..If Else.." in it...
You could uses a array and the letters you are using would be the elements inside of it. Then just uses some loops with if statements inside of them to sort. The loops are just to control the current element you are working with.
I cant type of some examples right now since im at the office but I can post some up if no one else dose by the time im home.
You could uses a array and the letters you are using would be the elements inside of it. Then just uses some loops with if statements inside of them to sort. The loops are just to control the current element you are working with.
I cant type of some examples right now since im at the office but I can post some up if no one else dose by the time im home.
Thx.. I look forward for your help... really thx... But i cant imagine how to do arrays... Since it wasnt discussed to us yet... but i know how to do the bubble sort and selection sort.. I dont how to insert loops with if else statements inside it...
Your printfs didn't work so well for me. The format is off on a few.
Basically, once have your values in any kind of over, the other requirements are a given. Here's how I'd aproach the problem:
CODE
void showResults(const int a, const int b, const int c) { printf("Ascending: %d %d %d\n",a,b,c); printf("Descending: %d %d %d\n",c,b,a); printf("Highest: %d\n",c); printf("Lowest: %d\n",a); }
void test(const int a, const int b, const int c) { printf("Starting Order: %d %d %d\n",a,b,c); if ((a<b)&&(a>c)&&(b>c)) showResults(c,a,b); else if ((a<b)&&(a<c)&&(b>c)) showResults(a,c,b); else if ((a<b)&&(a<c)&&(b<c)) showResults(a,b,c); else if ((a>b)&&(a>c)&&(b>c)) showResults(c,b,a); else if ((a>b)&&(a>c)&&(b<c)) showResults(b,c,a); else if ((a>b)&&(a<c)&&(b<c)) showResults(b,a,c); else printf("oops."); }
Tested it, works fine, you can get rid of the "oops".
Hope this helps.
This post has been edited by baavgai: 8 Jan, 2008 - 10:30 AM
You could uses a array and the letters you are using would be the elements inside of it. Then just uses some loops with if statements inside of them to sort. The loops are just to control the current element you are working with.
I cant type of some examples right now since im at the office but I can post some up if no one else dose by the time im home.
Thx.. I look forward for your help... really thx... But i cant imagine how to do arrays... Since it wasnt discussed to us yet... but i know how to do the bubble sort and selection sort.. I dont how to insert loops with if else statements inside it...
If you have not gone over arrays yet then its probably not a good idea to uses them. Mainly because the teacher might think someone else did the assignment. The upside is it could show him you are reading ahead and want to learn. That its really up to you if you would like to uses them or not. I would suggest learning about them before using them but their are a couple Tutorials you can read.
Also else if statements in loops are the same as outside loops. They need to sometimes bet set up a bit different depending on the type of loop and what you want to do. I am not the best at explaining programming things but a loop is a circle basically in code. Once the program is prompted to enter the loop it will keep going threw it until something tells it to stop and move on. Thats mostly a base explanation of it and someone might be able to explain it a little easier.
One thing you have to remember and I found this last year when taking C++ is some things are meant to be long. The instructor is trying to show you how things would look and work with out loops, arrays, and other things. If he wants you to just uses things a certain way its most of the time for your benefit and will help you in the end.
I will try and get the code up but might not tonight. The code baavgai posted should help you out.