I'm trying to allocate updated nodeid values to new array elements, but everytime I do so, I overwrite my nodeid value with the initial value of the array element in which I allocate nodeid. Here's what I'm talking about:
#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;
}
and here's my custom header file, 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);
and my source code, 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;
}
~
As you can see, code such as nodeid = field [row][column + 1] overwrites nodeid++ (according to my knowledge). How do I properly allocate nodeid without overwriting it?
UPDATE: Good news and bad news. Good news is that it displays my answers now. Bad news:
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|
_____________________________
As you can see, all of my results are 0 inside the bar along with dots here and there.
This is the code for my print_Field function:
#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;
}
After much tinkering, I got this:
________________________________________________________________________
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
|0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000|
________________________________________________________________________
Apparently, there's still something iin my motion.c code that's preventing the non-zero numbers from displaying.