溫馨提示×

c語言二維數組輸入的方法是什么

小億
866
2023-08-10 04:24:25
欄目: 編程語言

在C語言中,可以使用雙層循環來輸入二維數組。以下是一種常用的方法:

#include <stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int arr[rows][cols];
printf("Enter the elements of the array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}
printf("The entered array is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}

在上述代碼中,首先要求用戶輸入二維數組的行數和列數。然后,根據輸入的行數和列數定義一個二維數組。接下來,使用雙層循環逐個輸入二維數組的元素。最后,再次使用雙層循環打印出輸入的二維數組。

1
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女