WLL WLL - 1 month ago 6
C Question

Increase number in fix interval

I am figuring out on how to increase the number in fix interval in the array. Let's say the number I input is 3, then the first row of the array will have(3,3,3,3),then the second row will have(3,4,5,6),third row(3,5,7,9) and so on. From my code, I only manage to get (3,4,5,6,7) for the first row. I need help.

#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;
}

Answer

Your assign function shall look like this:

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];
}

Try to use your defines (NROW, NCOL) in ALL your code, else it makes no sence to define something :)

Comments