117k views
1 vote
g Write a complete C program to do the following: 1) Create a double array arr_d of 5 elements and initiate it with the values 1.0, 2.0, 3.0, 4.0, 5.0 2) Create a double pointer pDouble pointing to this array 3) Use pointer arithmetic and a loop structure to go thru the element of the array, print each element 4) Calculate and print the sum and average of all the elements of the array

1 Answer

5 votes

Answer:

#include <stdio.h>

void main(){

//sub part 1

double arr_d[] = {1.0,2.0,3.0,4.0,5.0};

//sub part 2

double *pDouble = arr_d;

double sum = 0.0;

int i;

//sub part 3

for (i = 0; i < 5; i++){

// Print value pointed by the pointer

printf("%lf ", *(pDouble + i));

}

//sub part 4

for (i = 0; i < 5; i++){

sum = sum + *(pDouble + i);

}

printf("\\Sum of the elements in an array : %lf",sum);

printf("\\Average of the elements in an array : %lf",sum/5);

}

User Ararat Harutyunyan
by
7.1k points