1

I'm a beginnner. This might sound stupid, as its a simple algorithm.The below program compiles successfully but when I run it does not prints the sorted array.The code works fine till the sort algorithm. Then it don't prints the sorted array. what am I doing wrong? Code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int n, i, k = -1;
    cout << "Enter the number of elements: ";
    cin >> n;

    int arr[n];
    cout << "Enter the elements to be sorted: ";
    for (i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    cout << "The unsorted array: \n";

    for(i = 0; i < n; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << endl;
    //BubbleSort.

    for(int e = n-1; e > 0; --e)
    {
        for (i = 0; i > e; ++i)
        {
            if (arr[i] > arr[i + 1]) {
                arr[i] = k;
                arr[i] = arr[i + 1];
                arr[i + 1] = k;
            }
        }
    }

    cout << "The sorted list: ";

    for (int x = 0; x > n; ++x)
    {
        cout << arr[x] << ' ';
    }

    system("pause");
    return 0;
}
3
  • 3
    Being a beginner is not an excuse of not debugging your own program Commented Aug 4, 2014 at 17:30
  • 1
    As @Matt says, you need to learn how to debug your program. Start by printing variables and checkpoints. Then check out a debugging tutorial if you want to go deeper. Commented Aug 4, 2014 at 17:33
  • OK, OK!!! For all of those who are having problems to my first sentence.I'm sorry. And I will surely learn to debug the code :p. Any nice tutorials though? :D Commented Aug 4, 2014 at 17:38

2 Answers 2

4

"beginners" mistake not really ?

Better use std::cout or a debugger to pin point the mistakes/flaws

Take care of the comparisons, and assignments

  • arr[i] = k; should be k = arr[i] ;

  • for (i = 0; i > e; ++i) should be for (i = 0; i < e; ++i)

and

  • for (int x = 0; x > n; ++x) should be for (int x = 0; x < n; ++x)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the quick reply! But, I modified the code as you suggested, but now its only printing the array as it is, not sorting it. O.O
You probably shouldn't encourage people to use SO as a debugging tool.
@EdwardMckinzie Make sure you understand how you should go about finding these errors.
You definitely shouldn't. These kind of questions aren't useful for future visitors.
@KarolyHorvath @ Keyser Sorry guys, its been a while since I answered a post, I tend forget community rule(s) now and then.
3

Recommend reread a tutorial about C++ and programming in general, some mistakes are basic ones:

  • Comparison operators
  • Assignment

The problematic code:

for (int e = n - 1; e > 0; --e) {
    for (i = 0; i < e; ++i) { // << change here, i > e   ->   i < e you are incrementing
        if (arr[i] > arr[i + 1]) {
            k = arr[i];            // change here, you save in a tmp variable to swap values
            arr[i] = arr[i + 1];
            arr[i + 1] = k;
        }
    }
}

cout << "The sorted list: ";

for (int x = 0; x < n; ++x) { // change here x > n    ->    x < n      same as before.
    cout << arr[x] << ' ';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.