c - 增加固定间隔的数量

标签 c

我正在研究如何增加数组中固定间隔的数量。假设我输入的数字是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/

相关文章:

c - 在c中使用分隔符分割字符串

c++ - connect() 中的 EHOSTDOWN 和 EHOSTUNREACH 是否致命?

c - x86 内联汇编标志

c - 使用 VS2010 libsndfile 的奇怪行为

c - atoi() 是 C 标准的一部分吗?

c - C 中的通用二分搜索

c++ - 如何使用 vtkImageImport 创建在删除 vtkImageImport 后仍然存在的 vtkImageData?

c - 哪一个执行时间更少,为什么?

c - 这个无锁栈的pop函数中的free()安全吗?

c - 不带 () 的 sizeof 有什么作用?