create and fill an array with random values, ask user to select array size. Afte
ID: 3667278 • Letter: C
Question
create and fill an array with random values, ask user to select array size. After initialization, display the array and then call a function that creates new array by sorting the current one. Display the contents of both arrays after the function call, properly labeling so the user can tell which is the initial array and which is the output array. Required functions: void initialize_array(int[], int); int * sort_array(int [], int); void print_array(int[], int);
I'm stuck on the beginning just trying to create an array and fill it with random numbers. Please help with that and beyond. Thank you in advance.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void initialize_array(int[],int);
{
return srand(time(NULL));
}
int main()
{
int x = 0;
cout << "Please enter size of array you would like to use." << endl;
cin >> x;
int array[x];
for (int i = 0; i < x; i++)
{
array[i] = initialize_array();
cout << array[i] << endl;
}
return 0;
}
Explanation / Answer
Use below code for random numbers and return them using an array and call this function #include /* function to generate and return random numbers */ int * getRandom( ) { static int r[10]; int i; /* set the seed */ srand( (unsigned)time( NULL ) ); for ( i = 0; i < 10; ++i) { r[i] = rand(); printf( "r[%d] = %d ", i, r[i]); } return r; } /* main function to call above defined function */ int main () { /* a pointer to an int */ int *p; int i; p = getRandom(); for ( i = 0; i < 10; i++ ) { printf( "*(p + %d) : %d ", i, *(p + i)); } return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.