我正在研究如何增加数组中固定间隔的数量。假设我输入的数字是3,那么数组的第一行将有(3,3,3,3),那么第二行将有(3,4,5,6),第三行将有(3,5 ,7,9) 等等。从我的代码中,我只能获得第一行的 (3,4,5,6,7) 。我需要帮助。
#include < stdio.h >
#define NROW 4
#define NCOL 5
void initialize(int a[4][5]) {
int x, y;
for (x = 0; x < 4; x++) {
for (y = 0; y < 5; y++)
a[x][y] = 0;
}
}
void disp_arr(int a[4][5]) {
int x, y;
for (x = 0; x < 4; x++) {
for (y = 0; y < 5; y++) {
printf("%i ", a[x][y]);
}
printf("\n");
}
}
int assign(int a[4][5], int starting_no) {
int x, y;
for (x = 0; x < 1; x++) {
for (y = 0; y < 5; y++) {
a[0][y] = starting_no;
starting_no++;
}
}
return a[3][4];
}
int main(void) {
int a[4][5], b;
initialize(a);
disp_arr(a);
printf("Please select a starting number :\n");
scanf("%i", & b);
assign(a, b);
disp_arr(a);
printf("The biggest number in the array is : %i \n");
return 0;
}
最佳答案
您的分配函数应如下所示:
int assign(int a[NROW][NCOL], int starting_no)
{
int x, y, old_starting_no;
old_starting_no = starting_no;
for (x = 0; x<NROW; x++)
{
starting_no = old_starting_no;
for (y = 0; y<NCOL; y++)
{
a[x][y] = starting_no;
starting_no += x;
}
}
return a[NROW-1][NCOL-1];
}
尝试在所有代码中使用您的定义(NROW、NCOL),否则定义某些内容没有任何意义:)
关于c - 增加固定间隔的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40237695/