Thread: interesting problem

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Question 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

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    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

    Code:
    int row, col;
    at the top of the display() function as it may be affecting the program some how.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    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 "

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    the comma was causing the problem, thank you

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like
    Code:
    int choice = 1;
    switch ( choice ) {
      case 0: display(mapdist); break;
      case 1: sum(mapdist); break;
      default: /* error? */break;
    }

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    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

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    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)

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 12:09 PM
  2. Interesting Problem with IOCP and AcceptEx() :: Winsock
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 10-07-2003, 09:16 PM
  3. Interesting number theory problem
    By Zach L. in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-20-2003, 07:45 AM
  4. Interesting virtual function problem.
    By Sebastiani in forum C++ Programming
    Replies: 12
    Last Post: 09-02-2003, 10:08 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM