2011-10-26 134 views
1

我一直在尝试这个代码,它不能很好地工作。从输入动态分配矩阵 - C

void *matrix_allocate_variable (int size) 
{ 
void *p1; 
if ((p1=(int*)malloc(size))==NULL){ 
    printf("out of memory.\n"); 
    exit(1); 
    } 
return p1; 
} 

在这里,我创建了一个调用的malloc和退出时的错误,这样我就可以在我的下一个函数使用它的功能:

void matrix_new(int **matrices, int *row_counts, int *column_counts, char specifier) 
{ 

int index, i; 
index= (int)(specifier-'A'); 


    scanf("%d",&row_counts[index]); 
    scanf("%d",&column_counts[index]); 

    matrices[index]= (int*)matrix__allocate_variable(sizeof(int)*        (row_counts[index]*column_counts[index]); 

这里就是我有问题。我试图让用户输入一些用于创建矩阵的输入,但是我有很多问题试图使其工作。有人能帮我开始吗?

PS。有关更多详细信息,我将在functions.c中创建函数,这是迄今为止我所拥有的功能。我有一个main.c,它调用了这些函数,以便稍后我可以添加,删除和转置,但是现在我试图输入数据,这是我遇到的很多麻烦。这里是我的main.c,我称之为功能。

/* Pointer to the set of matrix registers. */ 
int **matrices = NULL; 
/* row_counts[i] stores the number of rows in matrix i */ 
int *row_counts = NULL; 
/* column_counts[i] stores the number of columns in matrix i */ 
int *column_counts = NULL; 

/********************************************************************** 
Skeleton code part B: suggested form for selected variable initializations 
**********************************************************************/ 
/* Initialize the matrix set. */ 
matrices = (int**) matrix_allocate_variable(...); 
column_counts = (int *)matrix_allocate_variable(...); 
row_counts = (int *)matrix_allocate_variable(...); 
char call[2]; 
int error = 2; 



     do { 
      printf ("> "); 
      if (scanf ("%1s", call) !=1) { 
        fprintf (stderr, "Command not found. \n"); 
        exit (1); 
      } 


switch (call [0]) { 

        case 'A':  matrix_new(matrices,row_counts,column_counts,'A'); 
              break; 
        case 'B':  matrix_new(matrices,row_counts,column_counts,'B'); 
              break; 
        case 'C':  matrix_new(matrices,row_counts,column_counts,'C'); 
              break; 
        case 'D':  matrix_new(matrices,row_counts,column_counts,'D'); 
              break; 
        case '+':  matrix_add(matrices,row_counts,column_counts); 
              break; 
        case '^':  matrix_tranpose(matrices,row_counts,column_counts); 
              break; 
        case '*':  matrix_multiply(matrices,row_counts,column_counts); 
              break; 
        case '$':  exit (1); 

        default :  fprintf (stderr, "Command not found. \n"); 

      } 
    } while (error != 1); 
    return 0; 
    } 

任何帮助都会很好,我接下来要做的任何指示都很棒。非常感谢你的每一个。

+0

http://stackoverflow.com/questions/16004668/c-allocating-a-matrix-in-a-function/27366086#27366086 上面你会发现,我已经与由程序函数以任何可能的方式为C(gcc C11/C99)分配和操作矩阵。 – 42n4

回答

3

嗨,这是一个示例代码,使用malloc创建一个矩阵。
(这应该给你如何创建矩阵阵列的一些见解。如果没有然后让我知道。)

#include <stdio.h> 
#include <stdlib.h> 

// Creates a matrix given the size of the matrix (rows * cols) 
int **CreateMatrix(int rows, int cols) { 
    int **matrix = malloc(sizeof(int*) * rows); 
    int row; 
    for (row = 0; row < rows; row++) { 
    matrix[row] = malloc(sizeof(int) * cols); 
    } 
    return matrix; 
} 

// Take input for the matrix. 
void MatrixInput(int **matrix, int rows, int cols) { 
    int row, col; 
    for (row = 0; row < rows; row++) { 
    for (col = 0; col < cols; col++) { 
     scanf("%d", &matrix[row][col]); 
    } 
    } 
} 

void PrintMatrix(int **matrix, int rows, int cols) { 
    int row, col; 
    for (row = 0; row< rows; row++) { 
    for (col = 0; col < cols; col++) { 
     printf("%d ", matrix[row][col]); 
    } 
    printf("\n"); 
    } 
} 

int main() { 
    int **matrix; 
    int rows = 5; 
    int cols = 4; 
    matrix = CreateMatrix(rows, cols); 
    MatrixInput(matrix, rows, cols); 
    PrintMatrix(matrix, rows, cols); 
} 
0

下面是创建和释放内存的例子两个dimendional阵列...
(容易更改为其他类型)

int ** Create2D(int **arr, int cols, int rows) 
{ 
    int space = cols*rows; 
    int y; 

    arr = calloc(space, sizeof(int)); 
    for(y=0;y<cols;y++) 
    { 
     arr[y] = calloc(rows, sizeof(int)); 
    } 
    return arr; 
} 

void free2DInt(int **arr, int cols) 
{ 
    int i; 
    for(i=0;i<cols; i++) 
     if(arr[i]) free(arr[i]); 
    free(arr); 
} 

使用方法如下:

#include <ansi_c.h> 
int main(void) 
{ 
    int **array=0, i, j; 
    array = Create2D(array, 5, 4);//get the actual row/column values from reading the file once. 
    for(i=0;i<5;i++) 
     for(j=0;j<4;j++) 
      array[i][j]=i*j; //example values for illustration 
    free2DInt(array, 5); 

    return 0; 

}