Respuesta :
Answer:
The c++ program is as per the given scenario. Â
#include <iostream> Â
using namespace std; Â
int main() { Â
int len = 50; Â
double alpha[len]; Â
int j; Â
for(j=0;j<len; j++) Â
{ Â
// first 25 elements are square of their index value Â
if(j<len/2) Â
alpha[j]=j*j; Â
// last 25 elements are cube of their index value Â
if(j>=len/2 && j<len) Â
alpha[j]=j*j*j; Â
} Â
cout<<"The elements of the array are"<<endl; Â
for(j=0; j<len; j++) Â
{ Â
// after 10 elements are displayed, new line is inserted Â
if(j==10 || j==20 || j==30 || j==40) Â
cout<<endl; Â
cout<<alpha[j]<<"\t"; Â
} Â
} Â
OUTPUT Â
The elements of the array are Â
0 1 4 9 16 25 36 49 64 81 Â
100 121 144 169 196 225 256 289 324 361 Â
400 441 484 529 576 15625 17576 19683 21952 24389 Â
27000 29791 32768 35937 39304 42875 46656 50653 54872 59319 Â
64000 68921 74088 79507 85184 91125 97336 103823 110592 117649 Â
Explanation:
This program declares an array of double data type of size 50. The size of array 50 is assigned to an integer variable, len. Â
int len = 50; Â
double alpha[len]; Â
The variable len holds value 50; hence, len/2 will have value 25. Â
The first half of the elements are initialized to square of their index value. The second half of the elements are initialized to cube of their index values. Â
This is done using a for loop as shown. The variable j used in the for loop is declared outside the loop. Â
int j; Â
for(j=0;j<len; j++) Â
{ Â
// first 25 elements are square of their index value Â
if(j<len/2) Â
alpha[j]=j*j; Â
// last 25 elements are cube of their index value Â
if(j>=len/2 && j<len) Â
alpha[j]=j*j*j; Â
} Â
After initialization is done, the elements of the array are displayed, 10 elements in one line.
This is achieved by the following.
for(j=0; j<len; j++)
  {
    // after 10 elements are displayed, new line is inserted
    if(j==10 || j==20 || j==30 || j==40)
      cout<<endl;      Â
    cout<<alpha[j]<<"\t";
  }