我似乎得到第一个矩阵的输入,但是当我要求用户输入第二个矩阵的输入时,程序崩溃了......为什么这个?无法弄清楚,我甚至尝试分配记忆,结果是一样的......
#include <stdlib.h>
#include <stdio.h>
#define MAXCOLUMNS 10
// dealing with 2D arrays, passing to function etc
void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_columns);
void print_sum (int (*a)[MAXCOLUMNS], int (*b)[MAXCOLUMNS], int (*c)[MAXCOLUMNS], int n_rows, int n_columns);
int main() {
int i;
int rows;
int columns;
int (*two_d_array)[MAXCOLUMNS];
int (*two_d_array2)[MAXCOLUMNS];
int (*output)[MAXCOLUMNS];
printf("enter the number of rows\n");
scanf("%d", &rows);
printf("enter the number of columns\n");
scanf("%d", &columns);
printf("enter data into array number 1\n");
read_input(two_d_array, rows, columns);
printf("enter data for 2d array number 2\n");
read_input(two_d_array2, rows, columns);
print_sum(two_d_array, two_d_array2, output, rows, columns);
return 0;
}
void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_columns) {
int i;
int j;
for (i = 0; i < n_rows; ++i) {
for (j = 0; j < n_columns; ++j) {
printf("enter details for rows number %d and column number %d\n", i + 1, j + 1);
scanf("%d", (*(a+i)+j));
getchar();
}
}
}
void print_sum (int (*a)[MAXCOLUMNS], int (*b)[MAXCOLUMNS], int (*c)[MAXCOLUMNS], int n_rows, int n_columns) {
int i;
int j;
// computing sum
for (i = 0; i < n_rows; i++) {
for (j = 0; j < n_columns; j++) {
*(*(c+i)+j) = *(*(a+i)+j) + *(*(b+i)+j);
}
}
// printing sum
for (i = 0; i < n_rows; i++) {
printf("\n");
for (j = 0; j < n_columns; j++) {
printf("%d\t", *(*(c+i)+j));
}
}
}
答案 0 :(得分:0)
代表C99
#include <stdlib.h>
#include <stdio.h>
#define MAXCOLUMNS 10
void read_input(int rows, int cols, int a[rows][cols]);
void print_sum (int rows, int cols, int in1[rows][cols], int in2[rows][cols], int out[rows][cols]);
int main(void) {
int i, rows, columns;
printf("enter the number of rows\n");
scanf("%d", &rows);
printf("enter the number of columns\n");
scanf("%d", &columns);
//if(columns > MAXCOLUMNS){ fprintf(stderr, "too large!"); return 1); }
int array1[rows][columns];
int array2[rows][columns];
int array3[rows][columns];
printf("enter data into array number 1\n");
read_input(rows, columns, array1);
printf("enter data for 2d array number 2\n");
read_input(rows, columns, array2);
print_sum(rows, columns, array1, array2, array3);
return 0;
}
void read_input(int rows, int cols, int a[rows][cols]){
int i, j;
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
printf("enter details for rows number %d and column number %d\n", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
}
void print_sum (int rows, int cols, int a[rows][cols], int b[rows][cols], int c[rows][cols]){
int i, j;
for (i = 0; i < rows; i++){
printf("\n");,,
for (j = 0; j < cols; j++){
c[i][j] = a[i][j] + b[i][j];
printf("%d\t", c[i][j]);
}
}
}
for int **(2D_ARRAY)
#include <stdio.h>
#include <stdlib.h>
int main(void){
int rows = 3;
int cols = 5;
int **array;
int r, c;
//allocate
array = malloc(rows * sizeof(int*));
for(r = 0; r < rows ; ++r){
array[r] = malloc(cols * sizeof(int));
}
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
array[r][c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", array[r][c]);
}
printf("\n");
}
}
表示int(**)[SIZE]
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int main(void){
int rows = 3;
int cols = 5;
int (**array)[MAX];
int r, c;
//allocate
array = malloc(rows * sizeof(int (*)[MAX]));
for(r = 0; r < rows ; ++r){
array[r] = malloc(sizeof(int[MAX]));
}
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
(*array[r])[c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", (*array[r])[c]);
}
printf("\n");
}
}
for int(*)[SIZE]
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int main(void){
int rows = 3;
int cols = 5;
int (*array)[MAX];
int r, c;
//allocate
array = malloc(rows * sizeof(int[MAX]));
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
array[r][c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", array[r][c]);
}
printf("\n");
}
}