我试图将更新的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而不覆盖它?
更新:好消息和坏消息。好消息是它现在显示我的答案。坏消息:
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代码中仍然存在某些阻止非零数字显示的内容。
最佳答案
您的作业已倒退。代替
nodeid = field [row][column + 1];
尝试
field [row][column + 1] = nodeid;
此外,还应在运动函数中使用枚举常量(右,左等)。
并且您应该在main内部定义列,行,nodeid和方向,而不是在世界范围内定义。
所以,我会这样重写它:
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;
}