Ordering numbers

Pages: 12
Hi, I'm a beginner and I am trying to order arrays. This is what I have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>

using namespace std;

int main()
{
    int arr[4];
    int t;
    int ar[4]{35,11,36,47};
    /*if(ar[0]<ar[1]){ar[0]=arr[1];ar[1]=arr[0];};
    for(int i=1; i<4; i++)
    {
        t=i+1;
        if (ar[i]>arr[t]){arr[i]=ar[t];arr[t]=ar[i];}
        else {arr[i]=ar[i];arr[t]=ar[t];};

    }
    for(int d=0;d<4; d++)
    {
        cout<<arr[d]<<",";
    }*/
    if(ar[0]<ar[1])
        {
            ar[0]=arr[1];
            ar[1]=arr[0];
        }

    else
    {
        arr[1]=ar[0];
        arr[1]==ar[0];
    }

    if(arr[1]<ar[2])
    {
        t=ar[2];
        arr[1]=ar[2];
        arr[2]=t;
    }

    else
        {
            arr[1]=ar[0];
            arr[1]==ar[0];
        }

    if(arr[2]<ar[3])
        {
            t=ar[3];
            arr[2]=ar[3];
            arr[3]=t;
        }

    else
        {
            arr[1]=ar[0];arr[1]==ar[0];
        }

    if(arr[4]<ar[3])

        {
            t=ar[4];
            arr[3]=ar[4];
            arr[4]=t;
        }

    else
        {
            arr[1]=ar[0];arr[1]==ar[0];
        }

        for(int d=0;d<4; d++)
    {
        cout<<arr[d]<<",";
    }


    return 0;
}


the commented part is the part that I tried to use a for loop instead of hard coding it, but it didn't work. can you help me by giving me an example and its even better if you can help me on the for loop.

thanks
Last edited on
I would suggest doing a search for Sorting algorithms so that you get a better understanding of what is involved as there are several ways to do it. There are a few examples here:https://www.cs.cmu.edu/~adamchik/15-121/lectures/Sorting%20Algorithms/sorting.html

The biggest thing you are doing wrong with what you have is that you are not swapping the values like you think you are.
1
2
3
//ar[0] = 35, ar[1] = 11
ar[0]=ar[1]; // ar[0] = 11
ar[1]=ar[0]; // ar[1] = 11 

You need to have a temp variable to swap
1
2
3
temp = ar[0]; // temp = 35
ar[0] = ar[1]; // ar[0] = 11
ar[1] = temp; //ar[1] = 35 
Last edited on
can you do it using a for loop and not hard code it
this still doesnt work


#include <iostream>

using namespace std;

int main()
{
int arr[4];
int t;
int ar[4]{35,11,36,47};
/*if(ar[0]<ar[1]){ar[0]=arr[1];ar[1]=arr[0];};
for(int i=1; i<4; i++)
{
t=i+1;
if (ar[i]>arr[t]){arr[i]=ar[t];arr[t]=ar[i];}
else {arr[i]=ar[i];arr[t]=ar[t];};

}
for(int d=0;d<4; d++)
{
cout<<arr[d]<<",";
}*/
if(ar[0]<ar[1])
{
t = ar[0];
ar[0] = ar[1];
ar[1] = t;
}

else
{
arr[1]=ar[0];
arr[1]==ar[0];
}

if(arr[1]<ar[2])
{
t=ar[2];
arr[1]=ar[2];
arr[2]=t;
}

else
{
arr[1]=ar[0];
arr[1]==ar[0];
}

if(arr[2]<ar[3])
{
t=ar[3];
arr[2]=ar[3];
arr[3]=t;
}

else
{
arr[1]=ar[0];arr[1]==ar[0];
}

if(arr[4]<ar[3])

{
t=ar[4];
arr[3]=ar[4];
arr[4]=t;
}

else
{
arr[1]=ar[0];arr[1]==ar[0];
}

for(int d=0;d<4; d++)
{
cout<<arr[d]<<",";
}


return 0;
}
Last edited on
can you give me a whole sample program???
The really easy way - use a sorting function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <algorithm>

int main() {
    const unsigned int arrsize = 4;

    int arr[arrsize] = {35, 11, 36, 47};
    std::sort(arr, arr + arrsize);

    for (unsigned int i = 0; i < arrsize; ++i)
        std::cout << arr[i] << ' ';

    return 0;
}

Also, if you want us to look over your program, please use code tags and a readable indentation scheme.
thank!!!
Last edited on
can you make a hardcode for this sorting program with ifs and else ifs
Last edited on
You would need to know the size of the array before hand but why in the world would you want to? At least use a loop(s) and if(s).
can you sort an array using while loops???
You were hard coding!!!
Anything that can be done by a for loop can be done by while loop.
A while loop is just a less-restrictive for loop, so yes.
Try not using the arbitrary hard coding
can you give me an example
Hard coding is a bad habit
Search google for the definition of arbitrary and you'll know what I'm saying
Use rand()%min+max;
Stop smamming reports
THEN Y DID BOB THE ZEALOT REPORT ME ON SAYING THANKS???
Pages: 12