205k views
2 votes
1. Declare two dimensional double array with 3 rows and 4 columns. a) Use scanf to read the elements for this array. b) Write a function that computes the average of the above array. c) Write a function that computes the minimum of the above array.

User JP Ventura
by
5.3k points

1 Answer

3 votes

Answer:

Following are the program in the C Programming Language:

#include <stdio.h> //header file

float avgs(int a[3][4]){ //define function

int s =0, c=0; //set integer type variable

float avg=0; //set float type variable

//set for loops to find sum of an array

for (int i = 0; i < 3; i++){

for (int j = 0; j < 4; j++){

s = s + a[i][j];

c +=1;

}

}

//find average of an array

avg = s / c;

return avg;

}

int min(int a[3][4]){//define function

int m = a[0][0]; //set integer type variable

//set for loop for traversing

for (int i = 0; i < 3; i++)

{ for (int j = 0; j < 4; j++){

if(m > a[i][j])

m = a[i][j];

}

}

return m;

}

int main(){//define main function

int a[3][4];//set integer type Array

printf("Enter the elements of an array:\\");

for (int i = 0; i < 3; i++)

for (int j = 0; j < 4; j++)

scanf("%d", &a[i][j]);

printf("\\ Average of an array: %f", avgs(a));

printf("\\ Smallest element of an array: %d", min(a));

return 0;

}

Step-by-step explanation:

Here, we define header file "stdio.h" for print or scan the data.

Then, we define float type function "avgs()" for find the average of an array inside it.

  • we set two integer type variable for sum and count
  • we set float type variable to store average of an array
  • we set the for loops for the sum of an array.
  • we divide sum from count to find average and return it.

Then, we define integer type function "min()" to find the minimum value of an array.

  • we set integer type variable to store array elements
  • we set the for loops for traversing the array
  • we set if statement to find minimum value of an array.
  • we return the minimum value of an array.

Then, we define integer type "main()" function to call both the functions.

  • we set integer type array variable and get input from the user in it with the help of for loops.
  • we call both the functions with message and return 0.

User CaBieberach
by
6.1k points