Hey, it's been quite awhile since I last posted, but I'm stuck. What I'm supposed to do is look at this code and tell what would appear on the output screen. I ran the program and found that it should display: 2 3 6 1 5 0. However, I just don't see how those numbers are obtained. Here's the code:

#include<iostream>
using namespace std;
int main()
{
int a[ 6 ]={ 2, 6, 3, 7, 1, 5 }, t = 0;
for( int i = 0; i < 6; i++ )
{
      if ( a [ i + 1 ] < a[ i ] )
     {
          t = a[ i ];
          a[ i ] = a[ i + 1 ];
          a[ i + 1 ] = t;
      }
}
for( int k = 0; k < 6; k++ )
 
       cout << a[ k ] << " ";
 
return 0;
}

Thanks ahead of time for the help.

Dani AI

Generated

As pointed out, the loop runs past the end of the array and that is undefined behavior. was also on target: the simple fix is to stop the loop one element earlier (or to check i + 1 < n). The version in the first post performs only a single left-to-right pass of adjacent swaps (one bubble pass), so it never fully sorts — and the out-of-bounds access is why the final printed contents looked wrong on that run.

What a single adjacent-swap pass does: for each adjacent pair it swaps when the right element is smaller, so some elements move one position per pass but the whole array is not sorted. When the loop index exceeds the valid range it reads/writes memory outside the array; whatever happened to be in that nearby memory was copied into the last in-array slot on that run, producing the surprising value. That result is not guaranteed — it is simply undefined behavior.

Safe fixes and a full sort example:

size_t n = sizeof(a) / sizeof(a[0]);

// Single safe pass (no out-of-bounds)
for (size_t i = 0; i + 1 < n; ++i)
    if (a[i+1] < a[i])
        std::swap(a[i], a[i+1]);

// Full bubble sort (deterministic)
for (size_t pass = 0; pass + 1 < n; ++pass) {
    for (size_t i = 0; i + 1 < n - pass; ++i)
        if (a[i+1] < a[i])
            std::swap(a[i], a[i+1]);
}

Practical tips: compile with warnings enabled (-Wall -Wextra), run with AddressSanitizer (-fsanitize=address) or valgrind to catch bounds errors, and prefer std::array/std::vector with .at() when you need bounds-checked access. For real code, use std::sort instead of hand-rolled bubble sort unless the exercise requires it.

Recommended Answers

All 5 Replies

The code contains an error -- it accesses an element of the array that does not exist, which probably explains why one of the results is 0 instead of 7.

Anyway, the reason for the strange arrangement of the final results is that the function only sorts part of the array. A complete bubble sort algorithm requires two loops, not one.

if you don't do for(i = 0; i < SIZE - 1(5 in this example); i++) you try to reach the array[SIZE(6 in this example)] which doesn't have any user defined value...

Thanks for the replies. So there is a problem with the code? That's what my professor provided in the review sheet.

sometimes professors make mistakes too.

lol, even though some professors don't want to admit that they do. Well, it's nice to know that it wasn't me, I've been looking at that for a few hours. Thanks again for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.