-
interesting problem
hi, i am currently studying C programming, but find this quite odd, i have an array of values, and i am trying to get a fuction to pick a value of an array and print it, however it comes up with 0 every time. any ideas. here is the code so far.
Code:
include <stdio.h>
void display(int mapdis[10][10])
{
/* the start of the array */
printf("\n\n\n");
printf("WELCOME TO THE MAPPING PROGRAM\n");
printf("\n");
printf("\n");
int row, col;
for (row=0; row<10;row++)
{
for (col=0;col<10;col++)
{
printf("%4d", mapdis[row][col]);
}
printf("\n");
}
}
void sum(int mapdist [10][10])
{
int x, y;
printf("enter the first Point\n");
scanf("%d", &x);
printf("enter the Second Point\n");
scanf("%d", &y);
printf("the Distance selected is %d\n " + mapdist[x][y]);
printf("x is %d\n ", x);
printf("y is %d\n ", y);
}
int main(void)
{
int mapdist[10][10] ={0,4,5,6,7,8,9,8,7,6,
2,0,4,5,6,7,8,9,8,7,
1,2,0,4,5,6,7,8,9,8,
2,3,4,0,6,7,8,9,8,7,
3,4,5,6,0,8,9,8,7,6,
4,5,6,7,8,0,8,7,6,5,
5,6,7,8,9,8,0,6,5,4,
6,7,8,9,8,7,6,0,4,3,
7,8,9,8,7,6,5,4,0,2,
8,9,8,7,6,5,4,3,2,0};
display(mapdist);
sum(mapdist);
return 0;
}
any suggestions would be useful thanks :D
-
Works fine for me.
Code:
int mapdist[10][10] = {
{0,4,5,6,7,8,9,8,7,6},
{2,0,4,5,6,7,8,9,8,7},
{1,2,0,4,5,6,7,8,9,8},
{2,3,4,0,6,7,8,9,8,7},
{3,4,5,6,0,8,9,8,7,6},
{4,5,6,7,8,0,8,7,6,5},
{5,6,7,8,9,8,0,6,5,4},
{6,7,8,9,8,7,6,0,4,3},
{7,8,9,8,7,6,5,4,0,2},
{8,9,8,7,6,5,4,3,2,0}
};
^Makes it easier to see whats going on
Put this line
at the top of the display() function as it may be affecting the program some how.
-
I'd guess this is the problem
Code:
printf("the Distance selected is %d\n " + mapdist[x][y]);
Maybe that should be a comma.
Maybe you actually wanted to add two things there, but forgot the comma and other operand.
Either way you likely want a comma after the closing "
-
the comma was causing the problem, thank you
-
just trying to stick a switch statement in there, thing is where to put it, i cant seem to run it from main, or as function. all i want it to do is select which part to run i.e.
Code:
display(mapdist);
sum(mapdist);
how do i go about that.
-
Like
Code:
int choice = 1;
switch ( choice ) {
case 0: display(mapdist); break;
case 1: sum(mapdist); break;
default: /* error? */break;
}
-
yes, but sticking this in main, after the array mapdist has no effect whatsoever which leaves me confused. it runs display(mapdist) and sum(mapdist) but the switch that these two are in have no effect
-
You do know that a switch does not prompt for a choice, right? It simply takes whatever argument you give it, and drops to that case statement. You still have to put in a prompt some place asking them to pick. Does anyone actually think any more?
Quzah.
-
yeah the prompt is the part i'm looking at,
something like
Code:
int menu = scanf("%d", &menu);
switch ( menu ) {
case 1: display(mapdist); break;
case 2: point(mapdist); break;
case 3: sum(mapdist); break;
default: printf("Error, please choose a valid option\n");
is what i thought would work, however it just runs the same thing over and over, in this case it runs point(mapdis). why it is doing that i dont know, i want it to allow it to run a selection, then come back to the menu, and use an exit case(which i'm going put in later)
-
> int menu = scanf("%d", &menu);
Read the manual page.
scanf() returns the number of successful conversions and assignments - which given a single %d gives you a choice of 0, 1 and EOF.
Start with something simple like
int result = scanf("%d", &menu);