1

My program needs to read in the size of an array from the command line, it then should fill the array with random numbers. It should then display the sorted and unsorted contents of the array.

Created one for loop in order to read random numbers into the array and to display unsorted contents. Created a second for loop nested in the first one in order to sort the contents within the array.

#include <time.h>
#include <iostream>
#include <iomanip>
#include <cstdlib>


using namespace std;

int main(int argc, char * argv[], char **env)
{

    int SIZE = atoi(argv[1]);
    int *array = new int[SIZE];



    for(int i = 0; i < SIZE; i++)
    {
        array[i] = rand()% 1000;
        cout << i << ": " << array[i] << "\n";

        for(int j = 0; j < SIZE; j++)
        {
            if(array[j] > array[j + 1])
            {
                swap(array[j], array[j +1]);
            }

            cout << i << ": " << array[i] << "\n";
        }


    }


    return 0;
    }



I expect an output like

0: 350
1: 264
2: 897

0:264
1:350
2:897

I'm getting an output like
0:41
0:41
0:41
0:41
1:46
1:46
1:0
1:0
2:334
2:334
2:334
2:334
2
  • 1
    A suggestion. Separate the code that fills the array with data and code that sorts the array. This won't solve your problem but it will make it easier to debug. As posted, your program has undefined behavior since you are accessing elements of the array that have not been properly initialzed. Commented Oct 12, 2019 at 7:20
  • 1
    How to debug small programs Commented Oct 12, 2019 at 7:22

2 Answers 2

2

Because you are attempting to continually sort the array before it's initialized. Also, you have undefined behavior when j == SIZE-1 because it's illegal to compare array[j] > array[j+1] at that point. array[j+1] is an invalid index.

Instead of this:

for(int i = 0; i < SIZE; i++)
{
    array[i] = rand()% 1000;
    cout << i << ": " << array[i] << "\n";

    for(int j = 0; j < SIZE; j++)
    {
        if(array[j] > array[j + 1])
        {
            swap(array[j], array[j +1]);
        }

        cout << i << ": " << array[i] << "\n";
    }
}

This instead:

   // randomly initialize the array
   for(int i = 0; i < SIZE; i++)
   {
        array[i] = rand()% 1000;
        cout << i << ": " << array[i] << "\n";
   }

   // sort
   for (int i = 0; i < SIZE; i++)
   {
       for(int j = i+1; j < SIZE; j++)
       {
            if(array[i] > array[j])
            {
                swap(array[i], array[j]);
            }
       }
   }

   // print results
   cout << "finished sorting" << endl;
   for(int i = 0; i < SIZE; i++)
   {
        cout << i << ": " << array[i] << "\n";
   }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the feedback, I really appreciate it. It works now :))
1

The immediate issue is that your code is attempting to sort the array even before it's been fully initialized.

You need to split your main loop into three:

  • the first one to initialize the array;
  • the second one to sort the array (this would include an inner loop used by bubble sort);
  • the third one to print the results.

Separately, you also need to think carefully about the boundary conditions of your loops.

1 Comment

Thanks a lot for your help, works perfect now. The art of programming sure is the bees knees

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.