Input a random positive integer. Then, create an integer array with a size the same as the inputted integer.
Then, using loops, add random integer values into the array one by one. The number of values to be stored is dependent on the inputted odd integer on the first instruction.
Print out the list's new order in this manner, with each number separated by a space per row:
second-half values
middle value
first-half values
Refer to the sample output for a clearer view on how it is to be printed out.
#include <iostream>
#include <ctime>
#include <stdlib.h>
int main() {
using namespace std;
// Prompt user to enter number of chars
int N, i,j;
cout << "Enter size of array: ";
cin >> N;
int *arr = new int[N];
srand(time(NULL));
for ( i = 0; i < N; ++i) {
*(arr + i) = rand()%100 + 1; // Random int range from 1 to 100
}
cout<<endl<<"array: "<<endl;
for ( i = 0; i < N; ++i) {
cout << *(arr + i)<<' ';
}
if(N%2==1)
j = N/2;
else
j = N/2-1;
cout<<endl<<endl;
cout<<"Second-half values:"<<endl;
for ( i = N/2; i < N; ++i) {
cout << *(arr + i)<<' ';
}
cout<<endl<<endl;
cout<<"Middle value:"<<endl;
cout << *(arr + j)<<' ';
cout<<endl<<endl;
cout<<"First-half values:"<<endl;
for ( i = 0; i < N/2; ++i) {
cout << *(arr + i)<<' ';
}
// Free memory
delete [] arr;
return 0;
}
Comments
Leave a comment