make the data array a global array (declare the array outside of main) and use a
ID: 3883330 • Letter: M
Question
make the data array a global array (declare the array outside of main) and use an unsigned int for the size because you will have a larger maximum value that you can use:
const unsigned int N = 100;
int array[N];
int main() {...
In addition, this means that you do not need to have a function for sorting using bubble sort. You copy that code into main and sort the array.
Also, keep in mind that every computer has a maximum integer. This value is found by including the <climits> header and is called INT_MAX. You can output this value so that you are aware of the largest value.
Finally, using the random number generator requires the including of time.h, srand(), and rand().
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
using namespace std;
const unsigned int N = 100;
int array[N];
int main() {
int i, j;
for(i=0;i<N;i++)
array[i] = rand()%INT_MAX + 1;
cout<<"initial array generated by random numbers"<<endl;
for(i=0;i<N;i++)
cout<<array[i]<<" ";
cout<<endl;
for (i = 0; i < N-1; i++)
{
for (j = 0; j < N-i-1; j++)
{
if (array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
cout<<"sorted array is"<<endl;
for(i=0;i<N;i++)
cout<<array[i]<<" ";
cout<<endl;
cout<<"INT_MAX ="<<INT_MAX<<endl;
// your code goes here
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.