0

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;
    } 
}
nm10563
  • 43
  • 1
  • 9
  • Problems seeking debugging help should mention the desired behavior. – sjsam Jul 02 '16 at 18:04
  • And where do you return a pointer to the allocated array from the function? Where do you calculate `mean`? Are you supposed to do anything with the data that `readMatrix` returns? What is the `matrix` argument for? It seems to me that you are missing some basic understanding of C, and need [a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Some programmer dude Jul 02 '16 at 18:04
  • @JoachimPileborg readMatrix holds the values that the user inputs into a matrix.The dimension of the matrix are up to the user as well. Then the second function should take each row of the previous matrix and find the mean for each row. The mean of each row should then be stored in the new array. – nm10563 Jul 02 '16 at 18:26

1 Answers1

0

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.

VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • And if i wanted to find the average of each columns I would just switch (mean/cols) to (mean/rows), right? – nm10563 Jul 02 '16 at 18:54
  • 1) Allocate memory as `malloc(cols*sizeof(float));`; 2) `for (i=0;i – VolAnd Jul 02 '16 at 19:09
  • alright one more thing. If i wanted to find the average of the elements of the array pointed to by matrix. I would just divide the mean by (rows*cols), right? – nm10563 Jul 02 '16 at 21:02
  • Yes, number of elements in matrix is `rows×cols`, but remember about changing loops to calculate sum – VolAnd Jul 02 '16 at 21:16
  • would the loop look like this or am i going in the wrong direction `for(i=0;i – nm10563 Jul 02 '16 at 22:40
  • Loops looks O.K. but `matrix[i][j]` will cause a problem, because your data structure has type `float*` and expression `matrix[i*cols+j]` was used when data written to memory – VolAnd Jul 03 '16 at 07:25