Welcome to Dream.In.Code
Become a C++ Expert!

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!




Else-If Problems how to shorten? without using arrays

 
Reply to this topicStart new topic

Else-If Problems how to shorten? without using arrays, Turbo C v2.01 Problem

acbarbosa
8 Jan, 2008 - 04:48 AM
Post #1

New D.I.C Head
*

Joined: 8 Jan, 2008
Posts: 4

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
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Else-If Problems How To Shorten? Without Using Arrays
8 Jan, 2008 - 05:06 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
Is it simply the bubble sort you are not allowed to use? Or any standardized sorting algorithm?
User is online!Profile CardPM
+Quote Post

acbarbosa
RE: Else-If Problems How To Shorten? Without Using Arrays
8 Jan, 2008 - 05:25 AM
Post #3

New D.I.C Head
*

Joined: 8 Jan, 2008
Posts: 4

QUOTE(Amadeus @ 8 Jan, 2008 - 06:06 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...
User is offlineProfile CardPM
+Quote Post

lockdown
RE: Else-If Problems How To Shorten? Without Using Arrays
8 Jan, 2008 - 06:12 AM
Post #4

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

acbarbosa
RE: Else-If Problems How To Shorten? Without Using Arrays
8 Jan, 2008 - 06:22 AM
Post #5

New D.I.C Head
*

Joined: 8 Jan, 2008
Posts: 4

QUOTE(lockdown @ 8 Jan, 2008 - 07:12 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... biggrin.gif
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Else-If Problems How To Shorten? Without Using Arrays
8 Jan, 2008 - 10:30 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,280



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
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
User is offlineProfile CardPM
+Quote Post

lockdown
RE: Else-If Problems How To Shorten? Without Using Arrays
8 Jan, 2008 - 11:19 AM
Post #7

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
QUOTE(acbarbosa @ 8 Jan, 2008 - 07:22 AM) *

QUOTE(lockdown @ 8 Jan, 2008 - 07:12 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... biggrin.gif


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.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 02:55PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month