Sorting out the numbers in descending order

#include <iostream>

using namepace std;

void main (void){
int num1, num2, num3;

cout << "Enter 3 numbers: " << endl;
cin >> num1 >> num2 >> num3;

if ((num1 > num2) && (num1 > num3)){
if (num2 > num3){
cout << num1 << " " << num2 << " " << num3;
}
else {
cout << num1 << " " << num3 << " " << num2;
}
}

if ((num3 < num1) && (num3 < num2)){
if ((num3 > num2)) {
cout << num3 << " " << num2 << " " << num1;
}
else {
cout << num3 << " " << num1 << " " << num2;
}
}

if ((num2 > num1) && (num2 > num3)){
if ((num1 > num3)){
cout << num2 << " " << num1 << " " << num3;
}
else {
cout << num2 << " " << num3 << " " << num1;
}
}
}


^^
I'm new to c++ and wish to learn this subject and im trying to practice with my c++. I was given the task that requires input from the user and sorting it out in descending order. I thought the above code should work but it doesnt sort out the numbers. Could anyone give me a clue to where I've gone wrong?
You are entering the realm of sorting algorithms. There are plenty. Its actually an entire subject of its own. Since you are a beginner start with the "insertion sort algorithm" AVOID BUBBLESORT.
Last edited on
what do u mean by "insertion sort algorithm"?
Last edited on
Do you know anything about for loops, functions and arrays? You have to know about those to understand the code for sorting numbers in the order you want
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

/* shellsort: sort v[0]...v[n-1] into increasing order */
void shellsort(int v[], int n)
{
int gap, i, j, temp;
for (gap = n/2; gap > 0; gap /= 2)
for (i = gap; i < n; i++)
for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap) {
temp = v[j];
v[j] = v[j+gap];
v[j+gap] = temp;
}
}

int main (){
int array[10],i,j;
for (i=0;i<=9;i++)
scanf("%d",&array[i]);
shellsort(array, 10);
for (j=0;j<=9;j++)
printf("%d",array[j]);
}



There you go. Enjoy! And yes, its in C not C++

This should really only be attempted if you are very comfortable with for loops and basic shell sort algorithm. Otherwise, dont even try. Familiarize yourself with these first.
Last edited on
I have a basic understanding of loops, functions and arrays but i havent mastered them fully yet though. But thank you for telling me these stuffs. Im still trying to get a hang of it :)

Anyways the above code doesnt work sir. It just accepts the input numbers but doesnt print the numbers out in descending order. :(
oh wait nevermind i found my error and fixed it already thx
Topic archived. No new replies allowed.