Answer to Question #335386 in C++ for Array 4

Question #335386

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.

1
Expert's answer
2022-04-30T05:42:08-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS