Thread: What's wrong with this?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    55

    What's wrong with this?

    I am trying to get this thing work out but i am completely helpless now. I have problem in that bold part.




    Code:
    	for (i=0; i<N; i++)
    	       {
    			
    		a[i+1]=a[i];
    		b[i+1]=b[i];
    		val=random()%MAX;
    		printf("val=%d\n",val);
    		if (val==0)
    			{
    				x=x+1;
    				a[i+1]=x;
    			}
    			else if (val==1)
    			{
    				y=y+1;
    				b[i+1]=y;
    			}
    			else if (val==2)
    			{	
    				x=x-1;
    				a[i+1]=x;
    			}
    			else {
    				y=y-1;
    				b[i+1]=y;
    				}
    		for (q=0; q<i+1; q++)
    			{
    				if (a[q]==a[i+1] && b[q]==b[i+1])
    				goto found;								
    			}
    				
    	found:
    	printf("exit2\n");
    			}

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    What are you trying to do and what does it do?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    55
    Sorry i posted on worng part . I am writing C code not C++.
    I am trying to check final value instored at a and b with their previous values and just got to found and print exit2.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    and how does it not work?

    can you post a compilable example?

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    55
    Basically the random number help to get me the values of a[] and b[]. Then with and for statement i am just trying to get those values of a [] and b[] and trying to compare.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    can you post a compilable example?

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    55
    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MAX 4
    #define ISEED 1000
    #define N 3   			
    #define M 1 			
    
    double a[N+1], b[N+1];
    int main()
    	{
    		int val, i, j,v,f, q;
    		
    		
    		srandom(ISEED);
    
    		for (j=0; j<M; j++)
    		{
    			
    			for (v=0; v<N+1; v++)
    				{
    					a[v]=0; b[v]=0;
    				}
    			int x=0,y=0;
    			for (i=0; i<N; i++)
    				{
    			
    				a[i+1]=a[i];
    				b[i+1]=b[i];
    				val=random()&#37;MAX;
    				printf("val=%d\n",val);
    				if (val==0)
    					{
    						x=x+1;
    						a[i+1]=x;
    						}
    					else if (val==1)
    						{
    							y=y+1;
    							b[i+1]=y;
    						}
    					else if (val==2)
    						{	
    							x=x-1;
    							a[i+1]=x;
    						}
    					else {
    							y=y-1;
    							b[i+1]=y;
    						}
    				for (q=0; q<i+1; q++)
    					{
    						if (a[q]==a[i+1] && b[q]==b[i+1])
    							goto found;
    								
    							
    					}
    				
    				found:
    					
    				printf("exit2\n");
    			}
    
    	}

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    55
    it should print exit2 once only but it's not so.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Well, it's repeating because your found label is inside a loop.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    55
    i am just testing my program thru that printf statement. I just want to break both the loop i.e i and q when the condition is true.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    55
    if (a[q]==a[i+1] && b[q]==b[i+1])
    when this condition is true the loop should break and the loop of for(i.... ) should also break.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Ok, then put the found label on the other side of the }.

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Then introduce a flag,
    Code:
    int flag = 1;
    
    for (i = 0; (i < N) && (flag != 0); i++)
    {
        for(q = 0; (q < i + 1) && (flag != 0); q++)
        {
    
        }
    }
    Don't use goto.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    55
    Thanx robwhit that worked. I was just wandering and thinking it in complicated ways.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Some people recommend using a goto to break out of nested loops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM