从输入动态分配矩阵 - C

发布于 2024-12-11 23:12:10 字数 3016 浏览 0 评论 0原文

我一直在尝试这段代码,但效果不是很好。

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]);

这是我遇到问题的地方。我试图让用户输入一些输入来创建矩阵,但我在尝试使其正常工作时遇到了很多问题。有人可以帮我开始这个吗?

附言。有关更多详细信息,我正在 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;
    }

任何帮助都会很好,任何关于我下一步应该做什么的指示也很棒。非常感谢大家。

I've been trying this code and it's not working out very well.

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

Here I created a function that call's malloc and exits upon error, so that I could use it in my next function:

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]);

Here is where I am having problems. I'm trying to have the user enter some input for creating the matrix, but I'm having lots of problems trying to get this working. Can some one help me start this off?

PS. For more details, I'm creating functions in functions.c, this are what I have so far. I have a main.c which calls these functions so that later on I can add, subract, and transpose, but as of now I am trying to enter in data which is what I'm having lot of trouble with. Here is my main.c where I call the functions.

/* 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 '

Any help will be good and any pointers in what I should do next is great also. Thank you so so much every one.

: exit (1); default : fprintf (stderr, "Command not found. \n"); } } while (error != 1); return 0; }

Any help will be good and any pointers in what I should do next is great also. Thank you so so much every one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2024-12-18 23:12:10

您好,这是使用 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);
}

Hi this is a sample code to create one matrix using malloc.
(This should give you some insight on how to create an array of matrices. If it doesn't then let me know.)

#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);
}
给我一枪 2024-12-18 23:12:10

这是一个为二维数组创建和释放内存的示例...
(可以轻松修改为其他类型)

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;

}

Here is an example of creating and freeing memory for a two dimendional array...
(easily modifiable for other types)

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

Use it like this:

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文