创建一个二维数组使用指针/的malloc,然后打印出来 [英] Creating an 2D array using pointer/malloc, and then print it out

查看:199
本文介绍了创建一个二维数组使用指针/的malloc,然后打印出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写两个功能,一个用于读取矩阵(二维数组)等进行打印出来。到目前为止,我有:

  / *读取矩阵:分配空间,阅读元素,返回指针。该
   行数和列数由两个给出参数。 * /
双** read_matrix(INT行,诠释COLS){

    双**垫=(双**)的malloc(的sizeof(双*)*行);
    INT I = 0;
    对于(i = 0; I<行;我++){
    / *分配阵列,存储指针* /
    垫[I] =(双*)malloc的(的sizeof(双)* COLS);
   //后做什么?

     返回垫;
}
 

然后打印矩阵功能,不知道这是否是正确的。

 无效print_matrix(INT行,诠释COLS,双**垫){
  对于(I = 0; I&其中;行;我++){/ *各行的迭代* /
    为(J = 0; J< COLS; J ++){/ *在每一行,去了每一个山坳元* /
    的printf(%F,垫[I] [J]); / *打印每个行元素* /
}
}}
 

和这里的主要功能是我用来运行:

 的#include< stdio.h中>
#包括< stdlib.h中>


双** read_matrix(INT行,诠释COLS);
无效print_matrix(INT行,诠释COLS,双**垫);
无效free_matrix(INT行,双**垫);

诠释的main(){

  双**矩阵;
  INT行,COLS;
  / *第一矩阵* /
  的printf(黑客帝国1 \ N);
  的printf(请输入#行列数的:);
  scanf函数(%D,和放大器;行,和放大器; COLS);
  的printf(黑客帝国,输入%D实数:行* COLS);
  矩阵= read_matrix(行,COLS);
  的printf(你的矩阵\ N); / *打印输入的数据* /
  print_matrix(行,COLS,矩阵);
  free_matrix(行矩阵); / *免费矩阵* /

    返回0;}
 

解决方案

试试这个。也许对你有帮助。

 的#include< stdio.h中>
#包括< stdlib.h中>

双** read_matrix(INT行,诠释COLS);
无效print_matrix(INT行,诠释COLS,双**垫);
无效free_matrix(INT行,双**垫);

双** read_matrix(INT行,诠释COLS){

    双**垫=(双**)的malloc(的sizeof(双*)*行);
    INT I = 0,J = 0;
    对于(i = 0; I<行;我++)
    / *分配阵列,存储指针* /
        垫[I] =(双*)malloc的(的sizeof(双)* COLS);

       对于(i = 0; I<行;我++){
           为(J = 0; J< COLS; J ++){
               scanf函数(%LF,和放大器;垫[I] [J]);
           }
       }
     返回垫;
}

无效print_matrix(INT行,诠释COLS,双**垫){
    INT I = 0,J = 0;
  对于(I = 0; I&其中;行;我++){/ *各行的迭代* /
        为(J = 0; J< COLS; J ++){/ *在每一行,去了每一个山坳元* /
            的printf(%LF,垫[I] [J]); / *打印每个行元素* /
        }
        的printf(\ N);
    }
}

无效free_matrix(INT行,双**垫){
    INT I = 0;
    对于(i = 0; I<行;我++)
        免费(垫[I]);
    免费(垫);
}

诠释的main(){

  双**矩阵;
  INT行,COLS;
  / *第一矩阵* /
  的printf(黑客帝国1 \ N);
  的printf(请输入#行列数的:);
  scanf函数(%D,和放大器;行,和放大器; COLS);
  的printf(黑客帝国,输入%D实数:\ N,行* COLS);
  矩阵= read_matrix(行,COLS);
  的printf(你的矩阵\ N); / *打印输入的数据* /
  print_matrix(行,COLS,矩阵);
  free_matrix(行矩阵); / *免费矩阵* /

  返回0;
 }
 

执行:

 :〜$ GCC exam.c
:〜$ ./a.out
矩阵1
输入行列数排名:3
4
矩阵,输入12实数:
1
2
3
4
五
6
7
8
9
9
0
1
你的矩阵
1.000000 2.000000 3.000000 4.000000
5.000000 6.000000 7.000000 8.000000
9.000000 9.000000 0.000000 1.000000
 

I am trying to write 2 functions, one to read the matrix (2D array) and other one to print it out. So far I have:

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

then the print matrix function, not sure if it is correct

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

and here is the main function I am using to run:

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

解决方案

Try this one. may be helpful to you.

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

double **read_matrix(int rows, int cols){

    double **mat = (double **) malloc(sizeof(double *)*rows);
    int i=0,j=0;
    for(i=0; i<rows; i++)
    /* Allocate array, store pointer  */
        mat[i] = (double *) malloc(sizeof(double)*cols); 

       for(i=0; i<rows; i++){
           for(j=0; j<cols; j++){
               scanf("%lf",&mat[i][j]);
           }
       }
     return mat;
}

void print_matrix(int rows, int cols, double **mat){
    int i=0,j=0;
  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("%lf ",mat[i][j]); /* Print each row element */
        }
        printf("\n");
    }
}

void free_matrix(int rows, double **mat){
    int i=0;
    for(i=0;i<rows;i++)    
        free(mat[i]);
    free(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: \n",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;
 }

Execution:

:~$ gcc  exam.c
:~$ ./a.out 
Matrix 1
Enter # of rows and cols: 3
4
Matrix, enter 12 reals: 
1
2
3
4
5
6
7
8
9
9
0
1
Your Matrix
1.000000 2.000000 3.000000 4.000000 
5.000000 6.000000 7.000000 8.000000 
9.000000 9.000000 0.000000 1.000000 

这篇关于创建一个二维数组使用指针/的malloc,然后打印出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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