2

I am trying to use bubble sort method for array with dynamically determined size. Here's the code:

#include <iostream>

using namespace std;

int main()
{
    int n;
    cout<<"Enter n";
    cin>>n;
    int arr[n],swap;
    cout<<"Enter number"<<endl;
    cin>>arr[n];
    for(int i=0;i<n-1;i++)
    for(int j=0;i<n-i-1;j++)
        if(arr[j]>arr[j+1])
    {
        swap=arr[j];
        arr[j]=arr[j+1];
        arr[j+1]=swap;
    }
    for(int k=0;k<n;k++)
        cout<<"arr["<<k<<"]="<<arr[k]<<endl;
    return 0;
}

When I define the elements of the array in this way the program works:

const n=5;
int arr[n]={1,2,3,4,5)

But I need to enter the size of the array (n) and its elements from the keyboard. But when I run my code the program crashes after I enter the first number. Is there a way to fix it?

5
  • C++ arrays don't support runtime size-declaration. Use std::vector instead! Commented Dec 16, 2015 at 6:46
  • 1
    @princess I said C++, not C99. Commented Dec 16, 2015 at 6:51
  • @HappyCoder Right :) Commented Dec 16, 2015 at 6:52
  • 1
    This has nothing to do with bubble sort but rather with how to read in a dynamically sized array. For that, you will find hundreds of solutions here. However, if you haven't heard of std::vector before, it also means that you never finished a C++ tutorial, so do that first. Commented Dec 16, 2015 at 7:23
  • His bubble sort logic is wrong as well, his second loop still checks for i while it has to check for j Commented Dec 16, 2015 at 7:24

4 Answers 4

3
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n, swap, temp;
    vector<int> arr;
    cout<<"Enter n";
    cin>>n;

    // Loop and accept the n values.
    // You may need to take care of the new line.
    for(int i = 0; i < n; ++i)
    {
        cout << "Enter a number : ";
        cin >> temp;
        arr.push_back(temp);
    }

    for(int i=0;i<n-1;i++)
    for(int j=0;j<n-i-1;j++)
        if(arr[j]>arr[j+1])
    {
        swap=arr[j];
        arr[j]=arr[j+1];
        arr[j+1]=swap;
    }
    for(int k=0;k<n;k++)
        cout<<"arr["<<k<<"]="<<arr[k]<<endl;
    return 0;
}

Notice how a loop is used to extract n values from user. Also using a std::vector relieves you from writing code for a runtime sized array with new and delete.

Also, you inner loop was checking i<n-i-1 and incrementing j which should be j<n-i-1 instead, otherwise j will increment indefinitely till INT_MAX.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you a_pradhan, it's strange. The program again crashes but now when i enter all numbers.
Because your inner loop is wrong! See my corrected version.
Thank you! Your code works. I am a beginner and I want to understand the code. For example, what is the purpose of this line arr.push_back(temp);
That line is responsible for actually pushing the input value to the vector.
3

The key word is dynamic allocation. In C, the function is malloc. In Cpp, it can be new and delete. Although a vector can work well, it is just a kind of method by STL. Notice that my code may have safe problem.

#include <iostream>

using namespace std;

int main()
{
    int n,temp;
    cout<<"Enter n:";
    cin>>n;
    //dynamic allocation
    int *arr=new int[n];
    cout<<"Enter number."<<endl;
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    //bubble sort
    for(int i=0;i<n;i++){
        for(int j=i;j<n;j++){
            if(arr[i]>arr[j]){
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }
    //output the array
    for(int i=0;i<n;i++){
        cout<<"arr["<<i<<"]="<<arr[i]<<endl;
    }
    delete [] arr;
    return 0;
}

1 Comment

vector is not a method!
1

You can't take an integer array like that, you need to run a loop. You can take a string like that. There are lot of errors in the bubble sort logic as well, try the below code snippet. It should work fine. You need to dynamically allocate the array for arr

int n,r,swap,i,*arr;
cout<<"Enter n\n";
cin>>n;
arr = (int *)malloc((n)*sizeof(int));
cout<<"Enter numbers\n"<<n<<endl;
for(i=0;i<n;i++)
{
    cin>>arr[i];

}
for(i=0;i<n;i++)
{
    for(int j=0;j<n-1;j++)//You're checking for i. you need to check for j
    {
        if(arr[j+1]<arr[j])
        {
            swap=arr[j];
            arr[j]=arr[j+1];
            arr[j+1]=swap;
        }
    }
}
//now print your arr

Includes : #include<stdlib.h>

Comments

0

As mentioned in my comment, you CANNOT dynamically declare the size of arrays in C++ (use std::vector if you are willing to achieve that).

You can then do this:

....
cin >> n;
vector<int> arr(n); // reserves space for `n` integers in the memory
....

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.