Write a C program that declares an array alpha of 50 components of type double. Initialize the array so that the first 25 components are equal to the square of the index variable, and the last 25 components are equal to three times the index variable. Output the array so that 10 elements per line are printed.

Respuesta :

Answer:

The c program for the following scenario is given below.

#include<stdio.h>

int main() {    

   double alpha [50];    

   for( int k = 0; k < 25; k++ )

   {

       alpha[k] = ( k * k );

   }    

   for( int k = 25; k < 50; k++ )

   {

       alpha[k] = ( 3 * k );

   }    

   for( int k = 0; k < 50; k++ )

   {

       if( k == 10 || k == 20 || k == 30 || k == 40 )

           printf("\n");            

       printf( "%lf", alpha[k]);

       printf(" ");

   }    

   return 0;    

}

 

OUTPUT

0.000000 1.000000 4.000000 9.000000 16.000000 25.000000 36.000000 49.000000 64.000000 81.000000  

100.000000 121.000000 144.000000 169.000000 196.000000 225.000000 256.000000 289.000000 324.000000 361.000000  

400.000000 441.000000 484.000000 529.000000 576.000000 75.000000 78.000000 81.000000 84.000000 87.000000  

90.000000 93.000000 96.000000 99.000000 102.000000 105.000000 108.000000 111.000000 114.000000 117.000000  

120.000000 123.000000 126.000000 129.000000 132.000000 135.000000 138.000000 141.000000 144.000000 147.000000

Explanation:

The program only uses one array of data type double and one variable of data type integer. The variable k is used in for loop.

The elements in the alpha array are initialized inside for loop.  

First loop initializes the first 25 elements to the square of its index variable, k.

Second loop initializes next 25 elements to thrice the value of the index variable, k.

The third loop displays the elements of the array.

Since the variable k is an integer, the values of the elements of the array are integers. But, the array is declared as double data type hence, the values are displayed in the format of double numbers.

Only 10 elements are displayed per line.

for( int k = 0; k < 50; k++ )

   {

// new line is inserted after 10 elements are displayed

       if( k == 10 || k == 20 || k == 30 || k == 40 )

           printf("\n");            

       printf( "%lf", alpha[k]);

       printf(" ");

   }

ACCESS MORE