error with sorting

Mar 26, 2010 at 11:00am
hi. im new to this sorting stuff so please help me out here.
im assign to input 10 numbers, then sort then to descending order.
my problem is after i input 10 number there some negative numbers popping out that i dont know where it came and then the program ends....

Y_Y help me pls i need this done tommorrow.
#include <iostream>

using namespace std;

int main()
{
int a[10];
int i,j,temp,flag=0;
for(i=1,j=0;i<11,j<10;j++,i++)
{
cout<<"Enter number"<<i<<": ";
cin>>a[j];
}
for (i=0;i<10;i++)
{
for(j=0;j<(10-i);j++)
{
if (a[j]<a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
for(i=0;i<10;i++)
cout<<a[i];
return 0;
}
Mar 26, 2010 at 12:00pm
closed account (z05DSL3A)
Here is some code to do an ascending sort, I will leave it up to you to change it to descending.
You will also need to understand the code and change it, as your teacher will know that you have not written this.
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
#include <iostream>

using namespace std;

const int limit(10);  //  

int main()
{
    int data[limit] = {0};
    
    for(int i(0); i < limit; ++i)
    {
        cout << "Enter number " << (i + 1) << " : ";
        cin >> data[i];
    }

    //-- Bubble sort Pseudocode------------------------------------------------
    //do
    //  swapped := false
    //  for each i in 0 to length(A) - 2 inclusive do:
    //    if A[i] > A[i+1] then
    //      swap( A[i], A[i+1] )
    //      swapped := true
    //    end if
    //  end for
    //while swapped
    //
    //-- Bubble sort ----------------------------------------------------------

    bool swapped(false);
    do
    {
        swapped = false;
        for(int i(0); i < limit - 1; ++i)
        {
            if(data[i] > data[i+1])
            {   //swap
                int temp = data[i+1];
                data[i+1] = data[i];
                data[i] = temp;
                swapped = true;
            }
        }
    }while(swapped);
    //-- End of bubble Sort----------------------------------------------------

    cout << "Numbers sorted in ascending order: "; 
    for(int i(0); i < limit; ++i)
    {
        cout << data[i] << " ";
    }
    cout << endl;

    return 0;
}
Last edited on Mar 26, 2010 at 12:18pm
Mar 26, 2010 at 12:29pm
ill be checking this out.
but still be wanting to know what went wrong with my code.
thx~
Mar 26, 2010 at 12:39pm
closed account (z05DSL3A)
but still be wanting to know what went wrong with my code.

Good reply. :0)

From the looks of the code posted, the section
1
2
3
4
if (a[j]<a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

should be
1
2
3
4
5
6
if (a[j]<a[j+1])
{
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
}

p.s. To post code use the [code] [/code] tags.
Last edited on Mar 26, 2010 at 12:41pm
Mar 26, 2010 at 12:45pm
o.o
i didnt see that..

Hahaha thx now my program is working~

love this site!

mwuahh~
Mar 29, 2010 at 1:24pm
dude replace j< ( 10 - i ) by j< (9 - i ). your problem is solved..
Mar 29, 2010 at 3:49pm
You could use vectors here is a video

http://www.youtube.com/watch?v=rn6qNGI-g1U
Topic archived. No new replies allowed.