Ich versuche, 2 Funktionen zu schreiben, eine zum Lesen der Matrix (2D-Array) und andere, um es auszudrucken. Bisher habe ich:Erstellen Sie ein 2D-Array mit Zeiger/malloc, und drucken Sie es dann
/* Read a matrix: allocate space, read elements, return pointer. The
number of rows and columns are given by the two arguments. */
double **read_matrix(int rows, int cols){
double **mat = (double **) malloc(sizeof(double *)*rows);
int i=0;
for(i=0; i<rows; i++){
/* Allocate array, store pointer */
mat[i] = (double *) malloc(sizeof(double)*cols);
//what to do after??
return mat;
}
dann die Druckmatrixfunktion, nicht sicher, ob es richtig
void print_matrix(int rows, int cols, double **mat){
for(i=0; i<rows; i++){ /* Iterate of each row */
for(j=0; j<cols; j++){ /* In each row, go over each col element */
printf("%f ",mat[i][j]); /* Print each row element */
}
}}
ist und hier ist die wichtigste Funktion, die ich laufen bin mit:
#include <stdio.h>
#include <stdlib.h>
double **read_matrix(int rows, int cols);
void print_matrix(int rows, int cols, double **mat);
void free_matrix(int rows, double **mat);
int main(){
double **matrix;
int rows, cols;
/* First matrix */
printf("Matrix 1\n");
printf("Enter # of rows and cols: ");
scanf("%d %d",&rows,&cols);
printf("Matrix, enter %d reals: ",rows*cols);
matrix = read_matrix(rows,cols);
printf("Your Matrix\n"); /* Print the entered data */
print_matrix(rows,cols,matrix);
free_matrix(rows, matrix); /* Free the matrix */
return 0;}
Voting zu schließen, weil es wie eine Frage für den Code-Review Stack statt Stackoverflow sieht. Code sieht grundsätzlich gut aus. –
Was ist dein genaues Problem? Ich kann hier keine Frage sehen ... – Christoph
@BenJackson: Es ist als [offtopic auf der Überprüfung.stackexchange] geschlossen (http://codereview.stackexchange.com/q/17833/6143). Es ist keine gute Erfahrung. – jfs