newbie - while loop and printf

This is a discussion on newbie - while loop and printf within the C Programming forums, part of the General Programming Boards category; Hi. I'm new to C programming and am taking an introductory course for this in uni. I'm working on an ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    4

    newbie - while loop and printf

    Hi. I'm new to C programming and am taking an introductory course for this in uni. I'm working on an assignment which requires us to use Dijikstra's alg to determine the shortest route from a given origin and destination. We were given 2 route files to work with and I coded it mainly using one file. Now using the second route file, when mapped, is like 2 sections routes joined by only one path, and when testing with routes where the origin and destination are in the same section, it works. But when tested with origins and destinations in different sections, it goes into an infinite loop. However, when i tried to debug it by printing out the arrays i was using to run the alg in the beginning at the for loop, it printed it all out and it actually worked. When i took away the printf, it stopped working.
    What i'm frustrated about is that how can a missing printf statement cause the code not to work?
    I've only taken up programming this year and had no foundations so I'm quite stoked about it.
    Thanks
    Nick

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post your code and let's check it out. Printf in and of itself only displays output. It doesn't change anything as far as your calculations go.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Code:
    int not_finished = 1;
            int iterations = 0;
            while(not_finished == 1)
    	{  
    		
    		
    		/* This is the printf statement that gets everything to work */
    		
    		/* for(int i = 0; i < MAX_NUM_NODES; i++)
    		{
    			printf("Node: %c  Previous: %c  Costs: %f  Status: %c\n", nodes[i], previous[i], costs[i], status[i]);
    		} */
    		
    	
    	
    		iterations ++;
    		printf("While loop %i times\n", iterations);
             	for(int counter1 = 0; counter1 < MAX_SEGMENTS; counter1++)
             	{
             		if((start[counter1] == curr_node) && (start[counter1] != 33))
             		{
             			for(int counter2 = 0; counter2 < MAX_NUM_NODES; counter2 ++)
             			{
             				if((nodes[counter2] == end[counter1]) && (costs[counter2] >= consumption[counter1] + costs[node_pos]))
             				{
             					previous[counter2] = curr_node;
             					costs[counter2] = costs[node_pos] + consumption[counter1];
             				}
             			}
             		}
             		
             		if((end[counter1] == curr_node) && (start[counter1] != 33))
             		{
             			for(int counter3 = 0; counter3 < MAX_NUM_NODES; counter3 ++)
             			{
             				if((nodes[counter3] == start[counter1]) && (costs[counter3] >= consumption[counter1] + costs[node_pos]))
             				{
             					previous[counter3] = curr_node;
             					costs[counter3] = costs[node_pos] + consumption[counter1];
             				}
             				
             			}
             		}
             	}  
             	/* for(int i = 0; i < MAX_NUM_NODES; i++)
    		{
    			printf("Node: %c  Previous: %c  Costs: %f  Status: %c\n", nodes[i], previous[i], costs[i], status[i]);
    		} */           
                	node_pos = costcomp(costs, status); 
                	printf("Node_pos: %d\n", node_pos);
            	curr_node = nodes[node_pos];
            	printf("Currentnode : %c\n", curr_node); /* STOPS HERE */
                    status[node_pos] = 49;  
                    if(curr_node == destination)
                    {
                    	not_finished = 0;
                    }
            }
    this is the code...

  4. #4
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Not in your highlighted printf(), but a couple of lines down:

    printf("While loop %i times\n", iterations);

    For printing integers, you want to use %d. I've never seen a %i, so maybe it's another option I'm not aware of. Correct me if I'm wrong, but I'd suggest changing that.

    Perhaps that is affecting it in some random and weird way?
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    whoopss.. typo.. yeah.. erm.. it doesn't affect the problem.. changed it back to %d and it still goes in an infinite loop. Only when i place that printf where commented on does it work. without the printf, it stops just before "current = nodes[curr_pos]; " then repeats the loop. So when my var current does not get updated it gets into this infinite loop. why doesn't it execute that?

  6. #6
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Are you initializing node_pos before entering the loop?

    I see you set a value here:
    node_pos = costcomp(costs, status);

    But that's towards the end of the first loop, and you're using node_pos earlier on...
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    yeah. i initialise node_pos at the beginning when i take in the origin, i have to look through my nodes array for the origin and set the node_pos to that position so that the while loop knows where to start. then i updates the position. because i can't use structures in the assignment i had to work with 4 parallel arrays.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Epo
    Not in your highlighted printf(), but a couple of lines down:

    printf("While loop %i times\n", iterations);

    For printing integers, you want to use %d. I've never seen a %i, so maybe it's another option I'm not aware of. Correct me if I'm wrong, but I'd suggest changing that.

    Perhaps that is affecting it in some random and weird way?
    Those two format specifiers are exactly the same thing. You can read up on most of the format specifiers in this FAQ entry.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  2. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  3. Help with a simple loop...
    By Optimus Prime in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 09:28 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. where is my indefinite loop here?
    By pancho in forum C Programming
    Replies: 9
    Last Post: 03-15-2002, 01:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21