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