Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 25th, 2004, 01:04 AM
 maffinz8
Registered User

Join Date: Jul 2004
Posts: 6
Time spent in forums: 41 m 12 sec
Reputation Power: 0
program in ascending and descending order..

can you please help me to make a program that will accept 5 numbers then display the number in ascending and descending order..im just new in C.Tanx

#2
July 25th, 2004, 02:53 AM
 B-Con
Crypto-Con

Join Date: Apr 2004
Location: Frisco, Texas
Posts: 6,704
Time spent in forums: 1 Month 6 Days 6 h 45 m 21 sec
Reputation Power: 1235
It's pretty simple, really.... You want C, right?

Also, it'll matter if you want to only accept integers, or if you want to use doubles, I'll just use integers here....

Code:
```int main()
{
int num[5],idx,temp,unordered;

// This scans in the numbers
for (idx=0; idx < 5; idx++) {
printf("Enter a number: ");
scanf("%d", &num[idx]);
}

// This is the loop test we'll make to see if the list is ordered or not, if it isn't, we'll make our switches
while (unordered) {
unordered=0;
idx--;
for (idx=0; idx < idx; idx++) {
if (num[idx] > num[idx+1]) {
unordered = 1;
temp = num[idx];
num[idx] = num[idx+1];
num[idx+1] = temp;
}
}
}
for (idx=0; idx < 5; idx++)
printf("%d \n",num[idx]);

return 0;
}```

That should be right, I don't have a compiler handy
__________________
- "Cryptographically secure linear feedback shift register based stream ciphers" -- a phrase that'll get any party started.
- Why know the ordinary when you can understand the extraordinary?
)

#3
July 25th, 2004, 04:03 AM
 wannabe
=) wannabe?

Join Date: Jul 2002
Location: florida
Posts: 2,153
Time spent in forums: 21 h 58 m 25 sec
Reputation Power: 14
i ran that and it just prints the numbers as i entered them

#4
July 25th, 2004, 06:59 AM
 DaWei_M
Lord of Dorkness

Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,521
Time spent in forums: 4 Weeks 20 h 13 m 16 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
It doesn't execute the body of the "for", as idx is never less than idx. Anyway, displaying a list in forward and reverse orders doesn't require rearranging the values, just displaying in opposite directions.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > program in ascending and descending order..