Click here to Skip to main content
15,882,412 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
 <pre>int main(){
int i,j,t,n;
cout<<"Enter number of elements:"<<endl;cin>>n;
int M[n];
for(i=0;i<n;i++){
    cout<<"Enter element M["<<i<<"]=";
    cin>>M[n];
    cout<<endl;
}
   for(i=0;i<n-1;i++){
    for(j=i+1;j<n;j++){
        if(M[i]<M[j]){
            t=M[i];
            M[i]=M[j];
            M[j]=t;

        }


    }

   }
for(i=0;i<n;i++){

    cout<<M[i]<<"\t";
   }


return 0;
}



What I have tried:

i tried to sort the arrays using the bubble sort method but it is just showing random numbers
Posted
Updated 8-Aug-23 7:30am
v2

The problem is in the following loop:
C++
for(j=i+1;j<n;j++){
    if(M[i]<M[j]){ // this sill sort into descenfing order
        M[i]=t;   // this is the wrong way round
        M[i]=M[j];
        t=M[j];   // and so is this

    }

At the first marked line you set M[i] to the value of t, but t has never been initialised.
The code should be:
C++
for(j = i + 1; j < n; j++){
    if (M[i] < M[j]){ // probably need a test for M[i] greater than M[j]
        // swap M[i] and M[j]
        t = M[i];    // save the value of M[i]
        M[i] = M[j]; // set M[i] to M[j]
        M[j] = t;    // set M[j] to t - which is the original value of M[i]

    }


[edit]
Actually the main problem is in the preceding loop:
C++
for(i=0;i<n;i++){
    cout<<"Enter element M["<<i<<"] : ";
    // using the wrong index variable adds the value to an element beyond
    // the range of M, so none of the array elements are initialised.
    cin>>M[n]; // n is the number of elements, not the current index

It should be:
C++
for(i = 0; i < n; i++)
{
    cout << "Enter element M[" << i << "] : ";
    cin>>M[i]; // the index item should be i not n


[/edit]
 
Share this answer
 
v2
Comments
Lộc Nguyễn Gia 8-Aug-23 12:07pm    
Enter number of elements:
5
Enter element M[0]=1

Enter element M[1]=2

Enter element M[2]=3

Enter element M[3]=4

Enter element M[4]=5

268501009 268501009 268501009 4200464 4200464
Process returned 0 (0x0) execution time : 7.036 s
Press any key to continue.





what does this even mean,thank you for your answer.
Richard MacCutchan 8-Aug-23 12:51pm    
It means that you have ignored my answer.
Lộc Nguyễn Gia 8-Aug-23 12:57pm    
noooo, i fix the code exact the way you told me too :<
Richard MacCutchan 8-Aug-23 13:21pm    
Please edit your question and show the actual code you are using, so I can test it myself.
Lộc Nguyễn Gia 8-Aug-23 13:30pm    
i updated it , i do the exact same
To add to what Richard has said, you would have spotted the problem yourself in minutes if you had used the debugger to look at your code while it's running.
Debuggers are a tool that lets you add breakpoints, step through your code line by line, and look at the current value of variables to help you understand why code doesn't do exactly what you expect, and you should get used to using one because compiling does not mean your code is right! :laugh:

Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C++
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.
And learning to use it will save you huge amounts of time in future!
 
Share this answer
 
Comments
Lộc Nguyễn Gia 8-Aug-23 12:07pm    
thank you for the answers
OriginalGriff 8-Aug-23 12:51pm    
You're welcome!
Try to replace
C++
M[i]=t;
M[i]=M[j];
t=M[j];

with
C++
t=M[i];
M[i]=M[j];
M[j]=t;


[Edit]
Add this
C++
cout << __TIMESTAMP__ << endl;

it will tell when the program was compiled. It allow you to see if the program you run is the one with corrections.
 
Share this answer
 
v2
Comments
Lộc Nguyễn Gia 8-Aug-23 12:08pm    
Enter number of elements:
5
Enter element M[0]=1

Enter element M[1]=2

Enter element M[2]=3

Enter element M[3]=4

Enter element M[4]=5

268501009 268501009 268501009 4200464 4200464
Process returned 0 (0x0) execution time : 7.036 s
Press any key to continue.



what does this even mean? Thanks for the answer
Patrice T 8-Aug-23 13:06pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

And update source too.
Lộc Nguyễn Gia 8-Aug-23 14:55pm    
thanks man, this is just the result from the code i fixed and run
Think I would change your bubbling
... left out the entry for size and values entered

...

bool fBubble = false;
do{
    fBubble = false;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            //
            if (M[j] < M[i]) {   // is position j LE position i
                int t = M[j];       // get the smaller
                M[j] = M[i];
                M[i] = t;
                fBubble = true; // changed
            }
        }
    }
} while (fBubble);
//
for (int i = 0; i < n; i++) {
    cout << M[i] << "\t";
}

would like to add about debugging, it is a simple process and can save time worrying about code
 
Share this answer
 
Comments
Lộc Nguyễn Gia 8-Aug-23 14:54pm    
thanks, i just want to know what is wrong with my code
Rick York 8-Aug-23 15:42pm    
Debugging it is the easiest and fastest way to find out what is wrong with your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900