-3

I compiled the following C++ program for array sorting on my PC using dev C++ 6.30 with TDM-GCC 9.2.0 compiler. I get the following (undesirable) output:

2 4 5 9 23 69

Now, by using an online compiler of programizer, I get the following (desired/expected) output:

2 4 5 9 23 88

This is the code:

#include <iostream>
using namespace std;

int main()
{
    int i, cnt, x;
    //int Arr[6];
    
    int Arr[6] = {2, 9, 23, 88, 5, 4, };
    for (cnt = 1; cnt < 6; cnt++)
    {
        for(i=0; i<6; i++)
        {
            if ( Arr[i] > Arr[i+1])
            {
                x = Arr[i];
                Arr[i] = Arr[i+1];
                Arr[i+1] = x;           
            }
        }
    }
    // Printing the sorted array
    for (int j=0; j<6; j++)
        cout << Arr[j] << "\t"; 
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 7
    You are accessing `Arr[i+1]`. What happens when `i=5`? – justANewb stands with Ukraine Aug 05 '22 at 05:47
  • 5
    When you have different behaviors on different platforms, most of time you encountered *undefined behaviors*. – Louis Go Aug 05 '22 at 05:54
  • @LouisGo then how should I sort array to get desire result in all platform... should I use pointers... – Mahmood Raza Aug 05 '22 at 06:03
  • @MahmoodRaza No, it's much simpler than that, replace `for(i=0; i<6; i++)` with `for(i=0; i<5; i++)` When you have `Arr[i+1]` your loop must stop **one before the end** of `Arr`. Otherwise you have an out of bounds array access. – john Aug 05 '22 at 06:05
  • Proper indentation and better logic. The hints being given are that you go out of bounds of the array. – sweenish Aug 05 '22 at 06:06
  • @john I am bothering you but why did I receive entirely different result e.g. 69 as 6th element of array. How did it happen... you can skip if it is complicated' – Mahmood Raza Aug 05 '22 at 06:11
  • @MahmoodRaza Your code accessed past the end of your array. The number 69 just happened to be what was in the next memory location after your array. It could have been any number. Or accessing a memory location outside of your array could have crashed your program. – john Aug 05 '22 at 06:14
  • LOL i<6 does not make sense as i+1 doesn't exist thank you john... But amazing thing is that I got logical error instead of compilation error – Mahmood Raza Aug 05 '22 at 06:14
  • @MahmoodRaza C++ almost never stops you doing bad things. When you make errors like that it's called *undefined behaviour*. That means what it says, the behaviour of your program is undefined by C++. Anything could happen, including 'random' numbers, crashes, program working as expected and more. It's a very important concept to understand, which beginners often struggle with. When they make an error they expect C++ to tell them so, but it doesn't work like that. – john Aug 05 '22 at 06:17
  • Oh I remembered the concept of memory location allocated to array... Next memory location I got it thank you john helping me and educating me.... – Mahmood Raza Aug 05 '22 at 06:17

1 Answers1

0

In fact, this sorting algorithm does not work, and also you have some mistakes in your array initialization.

You did this when you tried to initialize your array:

int Arr[6] = {2, 9, 23, 88, 5, 4, };

That , in the end of your array is not necessary.

This is how it should be:

int Arr[6] = {2, 9, 23, 88, 5, 4 };

You should also use this to sort your array:

for (cnt = size - 1; cnt >= 0; cnt--)
{
    for(i = 0; i < cnt; i++)
    {
        if ( Arr[i] > Arr[i+1])
        {
            x = Arr[i];
            Arr[i] = Arr[i+1];
            Arr[i+1] = x;           
        }
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mahdy.n
  • 56
  • 8