如何在 c 中创建一个二维数组并使用指针和函数显示它? [英] How do i create a 2D array in c and display it using pointer and function?
本文介绍了如何在 c 中创建一个二维数组并使用指针和函数显示它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写 2 个函数,一个用于读取矩阵(二维数组),另一个用于打印出来.到目前为止,我有:
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 <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
");
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
"); /* 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("
");
}
}
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
");
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
"); /* Print the entered data */
print_matrix(rows,cols,matrix);
free_matrix(rows, matrix); /* Free the matrix */
return 0;
}
执行:
:~$ 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
这篇关于如何在 c 中创建一个二维数组并使用指针和函数显示它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文