在移动时在2D数组中分配nodeid ++时覆盖nodeid

时间:2014-01-26 15:37:31

标签: c arrays overwrite

我正在尝试将更新的nodeid值分配给新的数组元素,但每次我这样做时,都会用我分配nodeid的数组元素的初始值覆盖nodeid值。这就是我所说的:

#include <stdio.h>
#include "motion.h"

int motion (int nodeid, int row, int column, int direction) {

    nodeid = field [row][column];

    while (((row < (LENGTH - 1))&&(row > 0))||((column < (WIDTH - 1))&&(column > 0))){
        if (direction == 0){
            nodeid++;

            nodeid = field [row][column + 1];
            column++;
        }
        else if (direction == 1){
            nodeid++;
            nodeid = field [row][column - 1];
            column--;
        }
        else if (direction == 2) {
            nodeid++;
            nodeid = field [row - 1][column];
            row--;
        }
        else if (direction == 3) {
            nodeid++;
            nodeid = field [row + 1][column];
            row++;
        }
    }

    return 0;
}

这是我的自定义头文件,motion.h:

#define LENGTH 18
#define WIDTH 12
#define SAMPLES 8
enum direction {Right, Left, Up, Down};
int field [LENGTH][WIDTH];
int column;
int row;
int nodeid;
int direction;
int motion (int nodeid, int row, int column, int direction);
int print_field (int field[][ WIDTH ], int row, int column);

和我的源代码,main.c:

#include <stdio.h>
#include "motion.h"

int main() {

int samples;
int i;
int j;
int k;

printf ("How many samples do you want?\n");
scanf ("%d", &samples);

for (i = 0; i < samples; i++){
       printf ("Indicate nodeid, row, column and direction\n");
       scanf ("%d %d %d %d", &nodeid, &row, &column, &direction);
for(j = 0; j < LENGTH; j++)
{
       for(k = 0; k < WIDTH; k++){
       field[j][k] = 0;
}
}
motion (nodeid, row, column, direction);
printf ("\n%d", print_field(field, row, column));
}

return 0;
}
~

如您所见,nodeid = field [row] [column + 1]等代码会覆盖nodeid ++(据我所知)。如何在不覆盖nodeid的情况下正确分配nodeid?

更新:好消息和坏消息。好消息是它现在显示我的答案。坏消息:

Indicate nodeid, row, column and direction
_____________________________
|0.0000.0000.0000.0000.0000.0000.0000.0000.0000.000|
|0.0000.0000.0000.0000.0000.0000.0000.0000.0000.000|
|0.0000.0000.0000.0000.0000.0000.0000.0000.0000.000|
_____________________________

正如你所看到的,我在栏内的所有结果都是0,还有点和点。

这是我的print_Field函数的代码:

#include <stdio.h>
#include "motion.h"

int print_field(int field[][ WIDTH ], int row, int column){

int i;
int j;
int k;
int t;

for ( k = 0; k < WIDTH * 6; k++){
    printf("_");
}

printf ("\n");

for ( i = 0; i < LENGTH; i++){
printf ("|");

for (j = 0; j < WIDTH; j++){
printf( "%.3f", field [i][j]) ;
printf (" ");
}

printf ("\n");

for ( i = 0; i < LENGTH; i++){
printf ("|");

for (j = 0; j < WIDTH; j++){
printf( "%.3f", field [i][j]) ;
printf (" ");
}
printf ("\b|");
printf ("\n");
}
for (t = 0; t < WIDTH * 6; t++){

printf("_");
}
printf ("\n");
return 0;
}

经过多次修补,我得到了这个:

 ________________________________________________________________________

 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 |0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
 ________________________________________________________________________

显然,我的motion.c代码中还有一些东西阻止了非零数字的显示。

1 个答案:

答案 0 :(得分:0)

你的任务倒退了。而不是

 nodeid = field [row][column + 1];

 field [row][column + 1] = nodeid;

此外,您应该在运动功能中使用枚举常量(右,左等)。 你应该在main中定义column,row,nodeid和direction,而不是globatable。

所以,我会像这样重写它:

int motion (int nodeid, int row, int column, int direction) {

    field [row][column] = nodeid;

    while (row < LENGTH - 1 && row > 0 && column < WIDTH - 1 && column > 0){
        if (direction == Right)
            field[row][++column] = ++nodeid;
        else if (direction == Left)
            field[row][--column] = ++nodeid;
        else if (direction == Up)
            field[--row][column] = ++nodeid;
        else if (direction == Down)
            field[++row][column] = ++nodeid;
    }

    return 0;
}