I'm trying to print out the averages in the function void printavg(float *matrix,float *rowAve,float *colAve, float overallAve, int rows, int cols);
but when I tried to do a test run on printing out the average for rows I didn't get the right answer. What's the proper way to print out the avg using the printavg function. Here's an example of what the program should do:
Enter row dimension <must be between 1 and 5>: 3
Enter column dimension <must be between 1 and 5>: 2
Enter 6 data values for 3 x 2 matrix, in by-row order
1
2
3
4
5
6
1.00 2.00 1.50
3.00 4.00 3.50
5.00 6.00 5.50
3.00 4.00
Overall average is: 3.500000
Enter 0 to continue with a new matrix
Enter any other number to terminate the program:
Here's the code that I have so far
#include <stdio.h>
#include <stdlib.h>
// Function Prototypes
float *readMatrix(int rows, int cols);
float *rowavg(float *matrix, int rows, int cols);
float *colavg(float *matrix, int rows, int cols);
float overallavg(float* matrix, int rows, int cols);
void printavg(float *matrix, float *rowAve, float *colAve, float overallAve, int rows, int cols);
#define MAX_DIM 15
int main(void)
{
int done = 0;
int rows, cols;
float *dataMatrix;
float *rowAveVector;
float *colAveVector;
float overallAve;
while (!done)
{
// Prompt user to enter row and column dimensions of matrix (must be > 0)
do
{
printf("Enter row dimension (must be between 1 and %d): ", MAX_DIM);
scanf("%d", &rows);
} while(rows <= 0 || rows > MAX_DIM);
do
{
printf("Enter column dimension (must be between 1 and %d): ", MAX_DIM);
scanf("%d", &cols);
} while(cols <= 0 || cols > MAX_DIM);
dataMatrix = readMatrix(rows, cols);
if (dataMatrix == NULL)
{
printf ("Program terminated due to dynamic memory allocation failure\n");
return (0);
}
rowAveVector = rowavg(dataMatrix, rows, cols);
colAveVector = colavg(dataMatrix, rows, cols);
if(rowAveVector == NULL || colAveVector == NULL)
{
printf("malloc failed. Terminating program\n");
return (0);
}
overallAve = overallavg(dataMatrix, rows, cols);
//Print Averages
printAverages(dataMatrix, rowAveVector, colAveVector, overallAve, rows, cols);
free(dataMatrix);
free(rowAveVector);
free(colAveVector);
//Check if user wants to enter a new matrix
printf("Enter 0 to continue with a new matrix\n");
printf("Enter any other number to terminate the program: ");
scanf("%d", &done);
}
//That's it, we are done
return (0);
}
float *readMatrix(int rows, int cols)
{
int i=0;
int j=0;
int elements=0;
float *m=malloc(rows*cols*sizeof(float));
if (m==NULL)
{
printf("error\n");
return NULL;
}
printf("Enter values for the matrix: ");
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
elements = i*cols+j;
scanf("%f", &m[elements]);
}
}
return m;
}
float *rowavg(float *matrix, int rows, int cols)
{
if (matrix==NULL)
{
return NULL;
}
int i=0;
int j=0;
float mean=0;
float *Average_array=malloc(rows*sizeof(float));
if (Average_array==NULL)
{
return NULL;
}
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
mean+=matrix[i*cols+j];
}
Average_array[i]=(float)(mean/cols);
}
return Average_array;
}
float *colavg(float *matrix, int rows, int cols)
{
if (matrix==NULL)
{
return NULL;
}
int i=0;
int j=0;
float mean=0;
float *Average_array=malloc(cols*sizeof(float));
if (Average_array==NULL)
{
return NULL;
}
for (i=0;i<cols;i++)
{
for (j=0;j<rows;j++)
{
mean+=matrix[j*cols+i];
}
Average_array[i]=(float)(mean/rows);
}
return Average_array;
}
float overallavg(float* matrix, int rows, int cols)
{
float sum=0;
int i=0;
float elements=0;
float mean;
elements=rows*cols;
for (i=0;i<elements;i++)
{
sum+=matrix[i];
}
mean=sum/elements;
return mean;
}
void printavg(float *matrix, float *rowAve, float *colAve, float overallAve, int rows, int cols)
{
printf("%f",rowAve[rows,cols]);
}
Related
I am trying to create a program to find the transpose of a matrix my dynamic memory allocation. However, while entering the elements of the matrix I can't input more than one element, its only taking the a[0][0] as the input and the program is ending after that.
#include <stdio.h>
#include <stdlib.h>
void createMatrix(int **, int, int);
void inputElements(int **, int, int);
void transpose(int **, int **, int, int);
void display(int **, int, int);
void main()
{
int **matrix, **trans, rows, cols;
printf("\nEnter number of rows in the matrix: ");
scanf("%d", &rows);
printf("\nEnter number of columns in the matrix: ");
scanf("%d", &cols);
createMatrix(matrix, rows, cols);
createMatrix(trans, cols, rows);
inputElements(matrix, rows, cols);
transpose(matrix, trans, rows, cols);
printf("\nMATRIX:\n");
display(matrix, rows, cols);
printf("\nTRANSPOSE OF THE MATRIX:\n");
display(trans, rows, cols);
}
void createMatrix(int **a, int r, int c) //for allocating memory for the matrix
{
int i, j;
a = (int **)malloc(sizeof(int *) * r);
for(i = 0; i < r; i++)
a[i] = (int *)malloc(sizeof(int) * c);
}
void inputElements(int **a, int r, int c) //for entering matrix elements
{
int i, j, t;
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("\nEnter matrix element[%d][%d]: ", i + 1, j + 1);
fflush(stdin);
getchar();
scanf("%d", &(a[i][j]));
}
}
}
void transpose(int **a, int **t, int r, int c) //for finding out the transpose of the matrix
{
int i, j;
for (i = 0; i < c; i++)
{
for (j = 0; j < r; j++)
t[i][j] = a[j][i];
}
}
void display(int **a, int r, int c) //for displaying the matrix
{
int i, j;
for (i = 0; i < r; i++)
{
printf("\n");
for (j = 0; j < c; j++)
printf("\t%d", a[i][j]);
}
}
There are many posts about scanf in loops I've seen, but I wasn't able to connect my issue with any of them.
Write a C program to convert 1D array to 2D array using pointers.
Follow the given steps to implement the above code.
Ask the user to input the number of rows ( m ) and the number of columns ( n ) of the 2D array.
So the total number of elements of 1D array = ( m * n ) elements.
Call the function input_array to store elements in 1D array.
void input_array (int *arr,int size) // size = m * n
Call the function print_array to print the elements of 1D array.
void print_array (int *arr, int size)
Call the function array_to_matrix to convert 1D array to 2D array.
void array_to_matrix(int **matrix, int *arr, int row, int col)
Call function print_matrix to print the elements of the 2D array.
void print_matrix(int **mat, int row, int col)
All functions should be called from the main(). Accessing and storing of elements in pointers should be carried out by using pointers only.
Code:
#include <stdio.h>
#include <stdlib.h>
#define max 100
void input_array (int *arr,int size);
void print_array (int *arr, int size);
void array_to_matrix(int **matrix, int *arr, int row, int col);
void print_matrix(int **matrix, int row, int col);
int main()
{
int m, n, arr[max], mat[max][max];
printf("Enter the number of rows(m):");
scanf("%d",&m);
printf("Enter the number of columns(n):");
scanf("%d",&n);
int size = m*n;
input_array (arr,size);
print_array (arr,size);
array_to_matrix(mat, arr, m, n);
print_matrix(mat, m, n);
}
void input_array (int *arr,int size)
{
int i;
for(i=0;i<size;i++)
{
printf("Enter element a[%d]",i);
scanf("%d",&arr[i]);
}
}
void print_array (int *arr, int size)
{
int i;
printf("\n 1D array is as follows : \n");
for(i=0;i<size;i++)
{
printf("%d",arr[i]);
}
}
void array_to_matrix(int **matrix, int *arr, int row, int col)
{
int i,j,k=0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
matrix[i][j] = arr[k];
k++;
}
}
}
void print_matrix(int **matrix, int row, int col)
{
int i,j;
printf("\n 2D matrix is as follows : \n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",matrix[i][j]);
}
printf("\n");
}
}
Error:
I am getting a segmentation fault. The problem I am having is related to pointer to the arrays and how to pass them to the function.
in
void main()
{
int m, n, arr[max], mat[max][max];
int size = m*n;
you compute size while m and n are not yet (possibly) initialized, so the value of size is undefined with undefined behavior
In
void array_to_matrix(int **matrix, int *arr, int row, int col)
{
int i,j,k=0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
matrix[i][j] = arr[k];
}
}
}
the signature of the function says matrix is an array of pointers to int, but this is not compatible with your main where you use a 2D array. To be compatible :
void array_to_matrix(int (*matrix)[max], int *arr, int row, int col)
and same for print_matrix :
void print_matrix(int (*matrix)[max], int row, int col)
or if you prefer :
void array_to_matrix(int matrix[][max], int *arr, int row, int col)
void print_matrix(int matrix[][max], int row, int col)
But :
Write a C program to convert 1D array to 2D array using pointers
"2D array using pointers" is an is an abuse of language and means a 1D array of pointer to int, there is no 2D array.
In your previous version you limited the dimensions to 100, you do not have a reason to do that, just allocate the arrays in the heap.
Note also k always values 0 in array_to_matrix, so you always use arr[0]
And :
void main()
is wrong, main returns an int
I also encourage you to always check the result of scanf to be sure a valid input was used, else you works with non initialized value with an undefined behavior. When you read the number of rows and columns check there are not less than 1
In print_array to separate the print value with a space will help to make the result readable
Finaly :
#include <stdio.h>
#include <stdlib.h>
void input_array (int *arr,int size);
void print_array (int *arr, int size);
void array_to_matrix(int ** matrix, int *arr, int row, int col);
void print_matrix(int ** matrix, int row, int col);
int main()
{
int m, n, * arr, ** mat;
int size, i;
printf("Enter the number of rows(m):");
if ((scanf("%d",&m) != 1) || (m < 1)) {
puts("invalid value for rows");
return -1;
}
printf("Enter the number of columns(n):");
if ((scanf("%d",&n) != 1) || (n < 1)) {
puts("invalid value for columns");
return -1;
}
size = m*n;
if (((arr = malloc(size * sizeof(int))) == NULL) ||
((mat = malloc(m * sizeof(int))) == NULL)) {
puts("not enouh memory");
exit(-1);
}
for (i = 0; i < m; ++i) {
if ((mat[i] = malloc(n * sizeof(int))) == NULL) {
puts("not enouh memory");
exit(-1);
}
}
input_array (arr,size);
print_array (arr,size);
array_to_matrix(mat, arr, m, n);
print_matrix(mat, m, n);
/* free resources */
free(arr);
for (i = 0; i < m; ++i)
free(mat[i]);
free(mat);
return 0;
}
void input_array (int *arr,int size)
{
int i;
for(i=0;i<size;i++)
{
printf("Enter element a[%d]",i);
if (scanf("%d",&arr[i]) != 1) {
int c;
puts("invalid value, redo");
/* flush invalid value up to the end of line */
while ((c = getchar()) != '\n') {
if (c == EOF) {
puts("EOF, abort");
exit(-1);
}
}
i -= 1;
}
}
}
void print_array (int *arr, int size)
{
int i;
printf("\n 1D array is as follows : \n");
for(i=0;i<size;i++)
{
printf("%d ",arr[i]);
}
}
void array_to_matrix(int ** matrix, int *arr, int row, int col)
{
int i,j,k=0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
matrix[i][j] = arr[k++];
}
}
}
void print_matrix(int ** matrix, int row, int col)
{
int i,j;
printf("\n 2D matrix is as follows : \n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",matrix[i][j]);
}
printf("\n");
}
}
Compilation and execution :
pi#raspberrypi:/tmp $ gcc -g -Wall c.c
pi#raspberrypi:/tmp $ ./a.out
Enter the number of rows(m):2
Enter the number of columns(n):aze
invalid value for columns
pi#raspberrypi:/tmp $ ./a.out
Enter the number of rows(m):-1
invalid value for rows
pi#raspberrypi:/tmp $ ./a.out
Enter the number of rows(m):2
Enter the number of columns(n):3
Enter element a[0]1
Enter element a[1]aa
invalid value, redo
Enter element a[1]2
Enter element a[2]3
Enter element a[3]4
Enter element a[4]5
Enter element a[5]6
1D array is as follows :
1 2 3 4 5 6
2D matrix is as follows :
1 2 3
4 5 6
pi#raspberrypi:/tmp $
I have to write a program in C which finds the sum and the product of two matrices.
I wrote the functions but I get stuck at calling them in main. I don't know which variable is for rows and columns of result matrix.
#include <stdio.h>
void enterMatrix(int a[][10], int rows, int columns)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf("a(%d,%d)=",i,j);
scanf("%d",&a[i][j]);
}
}
}
void displayMatrix(int a[][10], int rows, int columns)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf("%d", a[i][j]);
printf(" ");
}
printf("\n");
}
}
void matrixSum(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
void matrixProduct(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
int i,j,k;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
c[i][j]=0;
for(k=0;k<columns;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
int main(void)
{
int a[10][10], b[10][10], sum[10][10], product[10][10];
int rowsA,columnsA,rowsB,columnsB;
printf("Number of rows for matrix A: \n");
scanf("%d",&rowsA);
printf("Number of columns for matrix A: \n");
scanf("%d",&columnsA);
printf("Number of rows for matrix B: \n");
scanf("%d",&rowsB);
printf("Number of columns for matrix B: \n");
scanf("%d",&columnsB);
printf("Enter first matrix: \n");
enterMatrix(a,rowsA,columnsA);
printf("Show first matrix: \n");
displayMatrix(a,rowsA,columnsA);
printf("Enter second matrix: \n");
enterMatrix(b,rowsB,columnsB);
printf("Show second matrix: \n");
displayMatrix(b,rowsB,columnsB);
if((rowsA==rowsB) && (columnsA==columnsB))
{
matrixSum(a,b,sum, ???, ???);
printf("The sum matrix is: \n");
displayMatrix(sum, ???, ???);
}
else
{
printf("Wrong information.");
}
if((rowsA==columnsB) && (rowsB==columnsA))
{
matrixProduct(a,b,product,???,???);
printf("The product matrix is \n");
displayMatrix(product,???,???);
}
return 0;
}
For matrixSum you just give rowsA and columnsA, as they are equal to rowsB and columnsB.
For matrixProduct you need three numbers: rowsA, columnsA and columnsB. rowsB is not needed, as it is equal to columnsA.
You need to change your matrixProduct function to use these three numbers at the correct places. Your current code doesn't work except when all numbers are equal.
Also your test if((rowsA==columnsB) && (rowsB==columnsA)) before calling matrixProduct only needs if(rowsB==columnsA).
if((rowsA==rowsB) && (columnsA==columnsB))
{
matrixSum(a,b,sum, rowsA, columnsA);
printf("The sum matrix is: \n");
displayMatrix(sum, rowsA, columnsA); // the sum has the same dimensions as A and B
}
else
{
printf("Both matrices don't have equal dimension.\n");
}
if(rowsB==columnsA)
{
matrixProduct(a,b,product,rowsA,columnsA,columnsB);
printf("The product matrix is \n");
displayMatrix(product,rowsA,columnsB); // the product matrix has the
// number of rows of A and the number of columns of B
}
else
{
printf("The number of columns of A needs to be equal to the number or rows of B.\n");
}
Your matrixProduct function could be adapted as follows:
void matrixProduct(int a[][10], int b[][10], int c[][10], int rowsA, int columnsA, int columnsB)
{
int i,j,k;
for(i=0;i<rowsA;i++)
{
for(j=0;j<columnsB;j++)
{
c[i][j]=0;
for(k=0;k<columnsA;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
My question is regrading the second function
float *rowavg(float *matrix,int rows,int cols)
So i'm suppose to dynamically allocate an array of floats with row elements and return NULL if allocation fails. I think i did this right, right? The other part that i'm trying to do is set the ith element of the new array to the average of the values in the ith row of the previous array. This part is where I'm getting got up. Did i call the previous array correctly(I believe no)?
#include <stdio.h>
#include <stdlib.h>
float *readMatrix(int rows, int cols);
float *rowavg(float *matrix, int rows, int cols);
float *colavg(float *matrix, int rows, int cols);
#define MAX_DIM 10
int main(void)
{
int done = 0;
int rows, cols;
float *dataMatrix;
float *rowAveVector;
float *colAveVector;
float overallAve;
while (!done)
{
do
{
printf("Enter row dimension (must be between 1 and %d): ", MAX_DIM);
scanf("%d", &rows);
} while(rows <= 0 || rows > MAX_DIM);
do
{
printf("Enter column dimension (must be between 1 and %d): ", MAX_DIM);
scanf("%d", &cols);
} while(cols <= 0 || cols > MAX_DIM);
dataMatrix = readMatrix(rows, cols);
if (dataMatrix == NULL)
{
printf ("Program terminated due to dynamic memory allocation failure\n");
return (0);
}
rowAveVector = rowAverage(dataMatrix, rows, cols);
colAveVector = colAverage(dataMatrix, rows, cols);
if(rowAveVector == NULL || colAveVector == NULL)
{
printf("malloc failed. Terminating program\n");
return (0);
}
}
float *readMatrix(int rows, int cols)
//Dynamically allocate an array to hold a matrix of floats.
//The dimension of the matrix is numRows x numCols.
//If the malloc fails, return NULL.
//Otherwise, prompt the user to enter numRows*numCols values
//for the matrix in by-row order.
//Store the values entered by the user into the array and
//return a pointer to the array.
{
int i=0;
int j=0;
int elements=0;
float *m=malloc(rows*cols*sizeof(float));
if (m==NULL)
{
printf("error\n");
return NULL;
}
printf("Enter values for the matrix: ");
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
elements = i*cols+j;
scanf("%f", &m[elements]);
}
}
return m;
}
float *rowavg(float *matrix, int rows, int cols)
{
int i=0;
float mean=0;
float *mat=malloc(rows*sizeof(float));
if(mat==NULL)
{
printf("error\n");
return NULL;
}
for (i=0;i<rows;i++)
{
readMatrix(rows,cols);
mean=
mat[i]=mean;
}
}
First of all, call of readMatrix(rows,cols); not needed in rowavg.
And if you want rowavg to return average values for each row in your matrix, try the following:
float *rowavg(float *matrix, int rows, int cols)
{
// check matrix befor use
if (matrix == NULL)
{
return NULL;
}
int i=0;
int j=0;
double mean; // variable name from original code in the question
float *avgarr = (float *)malloc(rows*sizeof(float));
if(avgarr == NULL)
{
return NULL;
}
for (i=0;i<rows;i++)
{
mean = 0.0;
for (j=0;j<cols;j++)
{
mean += matrix[i*cols+j];
}
avgarr[i] = (float)(mean/cols);
}
return avgarr;
}
And because in main you already have
rowAveVector = rowAverage(dataMatrix, rows, cols);
if(rowAveVector == NULL)
{
printf("malloc failed. Terminating program\n");
return (0); // here consider return value different from 0
}
you should not use printf("error\n"); in rowavg.
And think over using free after allocatied memory not needed any more.
Did I implement float overallavg(float* matrix, int rows, int cols)correctly? I think I didn't. I'm trying to return the average value of the elements of the array pointed to by matrix. Should I call the other two functions into the overall one and then divide by the total number elements in the array?
#include <stdio.h>
#include <stdlib.h>
float *readMatrix(int rows, int cols);
float *rowavg(float *matrix, int rows, int cols);
float *colavg(float *matrix, int rows, int cols);
float overallavg(float* matrix, int rows, int cols);
void printavg(float *matrix, float *rowAve, float *colAve, float overallAve, int rows, int cols);
#define MAX_DIM 10
int main(void)
{
int done = 0;
int rows, cols;
float *dataMatrix;
float *rowAveVector;
float *colAveVector;
float overallAve;
while (!done)
{
// Prompt user to enter row and column dimensions of matrix (must be > 0)
do
{
printf("Enter row dimension (must be between 1 and %d): ", MAX_DIM);
scanf("%d", &rows);
} while(rows <= 0 || rows > MAX_DIM);
do
{
printf("Enter column dimension (must be between 1 and %d): ", MAX_DIM);
scanf("%d", &cols);
} while(cols <= 0 || cols > MAX_DIM);
dataMatrix = readMatrix(rows, cols);
if (dataMatrix == NULL)
{
printf ("Program terminated due to dynamic memory allocation failure\n");
return (0);
}
rowAveVector = rowAverage(dataMatrix, rows, cols);
colAveVector = colAverage(dataMatrix, rows, cols);
if(rowAveVector == NULL || colAveVector == NULL)
{
printf("malloc failed. Terminating program\n");
return (0);
}
overallAve = overallAverage(dataMatrix, rows, cols);
//Print Averages
printAverages(dataMatrix, rowAveVector, colAveVector, overallAve, rows, cols);
free(dataMatrix);
free(rowAveVector);
free(colAveVector);
//Check if user wants to enter a new matrix
printf("Enter 0 to continue with a new matrix\n");
printf("Enter any other number to terminate the program: ");
scanf("%d", &done);
}
//That's it, we are done
return (0);
}
float *readMatrix(int rows, int cols)
{
int i=0;
int j=0;
int elements=0;
float *m=malloc(rows*cols*sizeof(float));
if (m==NULL)
{
printf("error\n");
return NULL;
}
printf("Enter values for the matrix: ");
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
elements = i*cols+j;
scanf("%f", &m[elements]);
}
}
return m;
}
float *readMatrix(int rows, int cols)
{
int i=0;
int j=0;
int elements=0;
float *m=malloc(rows*cols*sizeof(float));
if (m==NULL)
{
printf("error\n");
return NULL;
}
printf("Enter values for the matrix: ");
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
elements = i*cols+j;
scanf("%f", &m[elements]);
}
}
return m;
}
float *rowavg(float *matrix, int rows, int cols)
{
if (matrix==NULL)
{
return NULL;
}
int i=0;
int j=0;
float mean=0;
float *Average_array=malloc(rows*sizeof(float));
if (Average_array==NULL)
{
return NULL;
}
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
mean+=matrix[i*cols+j];
}
Average_array[i]=(float)(mean/cols);
}
return Average_array;
}
float *colavg(float *matrix, int rows, int cols)
{
if (matrix==NULL)
{
return NULL;
}
int i=0;
int j=0;
float mean=0;
float *Average_array=malloc(cols*sizeof(float));
if (Average_array==NULL)
{
return NULL;
}
for (i=0;i<cols;i++)
{
for (j=0;j<rows;j++)
{
mean+=matrix[j*cols+i];
}
Average_array[i]=(float)(mean/rows);
}
return Average_array;
}
float overallavg(float* matrix, int rows, int cols)
{
if (matrix==NULL)
{
return NULL;
}
int i=0;
int j=0;
float mean_1=0;
float mean_2=0;
float avg=0;
float elements=0;
float sum=0;
elements=rows*cols;
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
mean_1+=matrix[i*rows+j];
}
}
for (i=0;i<cols;i++)
{
for (j=0;j<rows;j++)
{
mean_2+=matrix[j*cols+i];
}
}
sum=mean_1+mean_2;
avg=sum/elements-1;
}
the following code:
cleanly compiles
contains the needed #include statements
Note: this line:
avg=sum/elements-1;
will not calculate the average of all the elements but rather a somewhat larger value because the total number of elements is being decremented by 1
Suggestion, in the function: overallavg() do not calculate separate 'mean' values, rather simply step through the whole array summing each element, then divide by the total number of elements
Suggestion, use an indent width of 4 spaces, as that is easily visible, even with a variable width font. 2 spaces with a variable width font just looks like the indented row is indented about 1/2 the width of a char. I.E. barely visible and very messy looking. (code is read many many times so should be written to be very easy to read.
#include <stdio.h> // printf() scanf()
#include <stdlib.h> // malloc, free()
float *readMatrix( size_t rows, size_t cols)
{
//int i=0;
//int j=0;
size_t elements=0;
float *m=malloc(rows*cols*sizeof(float));
if (m==NULL)
{
printf("error\n");
return NULL;
}
printf("Enter values for the matrix: ");
for (size_t i=0; i<rows; i++)
{
for ( size_t j=0; j<cols; j++)
{
elements = i*cols+j;
scanf("%f", &m[elements]);
}
}
return m;
}
float *rowavg(float *matrix, size_t rows, size_t cols)
{
if (matrix==NULL)
{
return NULL;
}
//int i=0;
//int j=0;
float mean=0;
float *Average_array=malloc(rows*sizeof(float));
if (Average_array==NULL)
{
return NULL;
}
for ( size_t i=0; i<rows; i++)
{
for ( size_t j=0; j<cols; j++)
{
mean+=matrix[i*cols+j];
}
Average_array[i] = (mean/(float)cols);
}
return Average_array;
}
float *colavg(float *matrix, size_t rows, size_t cols)
{
if (matrix==NULL)
{
return NULL;
}
//int i=0;
//int j=0;
float mean=0;
float *Average_array = malloc( cols*sizeof(float) );
if (Average_array==NULL)
{
return NULL;
}
for ( size_t i=0; i<cols; i++)
{
for (size_t j=0; j<rows; j++)
{
mean+=matrix[j*cols+i];
}
Average_array[i] = (mean/(float)rows);
}
return Average_array;
}
float overallavg(float* matrix, size_t rows, size_t cols)
{
#if 0
if (matrix==NULL)
{
return NULL;
}
#endif
//int i=0;
//int j=0;
float mean_1=0;
float mean_2=0;
float avg=0;
float elements=0;
float sum=0;
elements= (float)rows* (float)cols;
for ( size_t i=0;i<rows;i++)
{
for ( size_t j=0;j<cols;j++)
{
mean_1+=matrix[i*rows+j];
}
}
for ( size_t i=0; i<cols; i++)
{
for ( size_t j=0; j<rows ;j++)
{
mean_2+=matrix[j*cols+i];
}
}
sum=mean_1+mean_2;
avg=sum/elements-1;
return avg;
}