a Write a function definition for a userdefined function tha

a)

Write a function definition for a user-defined function that calculates the (floating-point double) mean of a specified column in a 2-D array that has NROWS rows and NCOLS columns. (NROWS and NCOLS are pre-defined symbolic constants) The parameters should be the double array and the desired column, as specified in the following function prototype: double col_mean(double x[NROWS, NCOLS], int col);

b)

Same as #a, but calculating the standard deviation rather than the mean

Xi avg 1

Solution

a) To find Mean of given column in given matrix

  

// Here I am writing all the code to better understanding.

#include<stdio.h>
#include<math.h>

#define NROWS 3   // Fixing No of rows is 3
#define NCOLS 3       // Fixing No of columns is 3
double col_mean (double mat[NROWS][NCOLS], int col);   // funtion declaration
void main(){
   double matrix[NROWS][NCOLS] = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};       // Take constant 2d-mtrix
   int col = 1;       // Assume column 1
   double mean = col_mean(matrix,col);
   printf(\" Mean of column is :: %lf\ \", mean);  
}

double col_mean (double mat[NROWS][NCOLS], int col){
   int i,count = 0,sum= 0;
   for (i= 0; i<NROWS; i++){       // Iterating the loop
       sum += mat[i][col];       // sum of all elements
       count +=1;
   }
   return (sum/count);           // returning mean of elements
}

// Sample Output ::

// Mean : 5.00

b) To find standard deviation of column in given matrix

// Here also I added all the code to get things clearly

#include<stdio.h>
#include<math.h>

#define NROWS 3   // Fixing No of rows is 3
#define NCOLS 3       // Fixing No of columns is 3
double col_mean (double mat[NROWS][NCOLS], int col);  
double x_mean (double mat[NROWS][NCOLS], int col);   // Mean funtion declaration
double x_sd (double mat[NROWS][NCOLS], int mean,int col);   // Sd function declaration
void main(){
   double matrix[NROWS][NCOLS] = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};       // Take constant 2d-mtrix
   int col = 1;       // Assume column 1
   double mean = x_mean(matrix,col);   // Finding the mean
   double standardDeviation = x_sd(matrix,col,mean);   // Finding the sd
   printf(\" standardDeviation of column is :: %lf\ \", standardDeviation);
}

double x_mean (double mat[NROWS][NCOLS], int col){   // This fuunction is to find the mean of the column
   int i,count = 0,sum= 0;
   for (i= 0; i<NROWS; i++){
       sum += mat[i][col];
       count ++;
   }
   return (sum/count);
}

double x_sd (double mat[NROWS][NCOLS], int mean,int col){   // This fuunction is to find the standard deviation of the column
   int i,count = 0,sum= 0,element,difference;
   for (i = 0; i<NROWS; i++){
       element = mat[i][col];
       sum += (element-mean) * (element-mean);       // Squaring of the difference
       count ++;                   // finding N
   }
   return sqrt(sum/count);                   // Square root function
}

// Sample output ::
// Mean : 5.0
// Deviation : 5.477226

a) Write a function definition for a user-defined function that calculates the (floating-point double) mean of a specified column in a 2-D array that has NROWS
a) Write a function definition for a user-defined function that calculates the (floating-point double) mean of a specified column in a 2-D array that has NROWS

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site