2016-07-02 9 views
0

Meine Frage ist die zweite FunktionDynamische Zuordnung und Arrays

float *rowavg(float *matrix,int rows,int cols) 

So Neueinstufung ich bin suppose, um dynamisch eine Reihe von Schwimmern mit Zeilenelementen zuzuordnen und NULL zurück, wenn Zuweisung fehlschlägt. Ich denke, ich habe das richtig gemacht, oder? Der andere Teil, den ich versuche zu tun, ist, das i-te Element des neuen Arrays auf den Durchschnitt der Werte in der i-ten Zeile des vorherigen Arrays zu setzen. In diesem Teil werde ich aufstehen. Habe ich das vorherige Array richtig aufgerufen (ich glaube nein)?

#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; 
    } 
} 
+0

Probleme, die Debugging-Hilfe suchen, sollten das gewünschte Verhalten erwähnen. – sjsam

+0

Woher geben Sie einen Zeiger auf das zugewiesene Array von der Funktion zurück? Wo berechnen Sie "gemein"? Solltest du irgendetwas mit den Daten machen, die 'readMatrix' zurückgibt? Wofür ist das Matrix-Argument? Es scheint mir, dass du ein grundlegendes Verständnis von C verpasst hast und [ein gutes Anfängerbuch] brauchst (http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). –

+0

@JoachimPileborg readMatrix enthält die Werte, die der Benutzer in eine Matrix eingibt. Die Dimension der Matrix hängt ebenfalls vom Benutzer ab. Dann sollte die zweite Funktion jede Zeile der vorherigen Matrix nehmen und den Mittelwert für jede Zeile finden. Der Mittelwert jeder Zeile sollte dann in dem neuen Array gespeichert werden. – nm10563

Antwort

0

Zu allererst Aufruf von readMatrix(rows,cols); nicht in rowavg benötigt.

Und wenn Sie rowavg wollen in Ihrer Matrix für jede Zeile Mittelwerte zurück, versuchen Sie Folgendes:

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; 
} 

Und weil in main Sie haben bereits

rowAveVector = rowAverage(dataMatrix, rows, cols); 
    if(rowAveVector == NULL) 
    { 
     printf("malloc failed. Terminating program\n"); 
     return (0); // here consider return value different from 0 
    } 

Sie nicht printf("error\n"); verwenden sollten in rowavg.

Und denken Sie über free nach allocatied Speicher mit nicht mehr benötigt.

+0

Und wenn ich den Durchschnitt von jeder Spalte finden wollte, würde ich nur wechseln (mean/cols) zu (mean/rows), oder? – nm10563

+0

1) Speicher als 'malloc (cols * sizeof (float));'; 2) 'für (i = 0; i VolAnd

+0

in Ordnung noch eine Sache. Wenn ich den Durchschnitt der Elemente des Arrays finden wollte, auf die Matrix zeigt. Ich würde nur den Mittelwert durch (Zeilen * Spalten) teilen, richtig? – nm10563