动态分配与输入矩阵 - Ç [英] Dynamically Allocating a Matrix from Input - C

查看:124
本文介绍了动态分配与输入矩阵 - Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想这个code和它不工作得非常好。

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

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

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。有关详细信息,我创建functions.c 在的功能,这就是我有这么远。我有一个的main.c 它调用这些函数,这样以后我可以添加,subract和调换,但截至目前,我试图在数据进入这就是我有很多的麻烦。这里是我的的main.c ,我调用的函数。

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 '$':       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.

推荐答案

你好,这是一个简单的code到使用malloc创建一个矩阵。结果
(这应该给你如何创建矩阵阵列的一些见解。如果没有然后让我知道。)

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

这篇关于动态分配与输入矩阵 - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆