Thread: error in a for loop

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    63

    error in a for loop

    I am having an error in my code below and cant figure out why. It must be some stupid smalle error. After doing a lot of testing, I have conluded that the first time through goes fine. But when it loops through a subsequent times it automatically returns 0, though the if statement is not true. Any idea what is wrong here?

    edit: setCardinality returns the size of the array.

    Code:
    int isSubset(int a[], int b[]){
    	
    	int isMember(int x, int a[]);
    	int setCardinality(int a[]);
    	int i;
    
            /*problem somewhere in here, returns 0 on loops after first*/
    	for(i=0;i<=setCardinality(b);i++){
    		
    	if((isMember(a[i],b))==0){
    			return 0;
    		}
    	}
    
    	return 1;
    }
    Last edited by saahmed; 03-09-2006 at 06:59 PM.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    We don't know anything about the other functions though.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by OnionKnight
    We don't know anything about the other functions though.
    I didnt think they really mattered for the question, besides setCardinality.
    Anyway, the functions called: setCardinality just returns the size of the array. So, the for looping through each element of the array and stopping at the end of it.

    isMember just searches for the paricular number in the particular array. So in this case it is searching for the number at element i of a (a[i]) in array b. The function returns 0 if the number is found in the array and 1 if it is not.

    This function is supposed to determine if the two arrays are subsets, meaning that all of the elements in array can be found in array b. The problem is that after the first loop, the function only returns 0, regardless if the if statement is true or false.

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Then there is a problem with your isMember function and I can't really tell why because I don't know how it works. Post that one.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by OnionKnight
    Then there is a problem with your isMember function and I can't really tell why because I don't know how it works. Post that one.
    The isMember function is working fine I am pretty sure. Because I can search for a value and it will returns the correct answer. But, I will post it anyway:
    It returns 0 if the number is not found.

    Code:
    int isMember(int x, int a[]){
    	int setCardinality(int a[]);
    	int middle;
    	int low=0;
    	int high;
    	
    	high=setCardinality(a);
    
    	while(low<=high){
    
    		middle = (low+high)/2;
    
    		if(x==a[middle]){
    			return 1;
    		}
    
    		else if(x<a[middle]){
    			high=middle-1;
    		}
    
    		else{
    			low=middle+1;
    		}
    
    		
    	}
    	
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    I cant figure this out. Here is my whole file, it is incomplete. If somebody has visual studio can you just paste it in and see if you can figure out why isSubset is not returning the right value?

    I added printf statements to figure out where it was going and it doesnt even make sense to me. It is not even going to the if statement, yet it is returning 0.

    Code:
    #include<stdio.h>
    
    int setCardinality(int a[]);
    int isEmpty(int a[]); 
    int isMember(int x, int a[]); 
    int isSubset(int a[], int b[]); 
    void printSet(int a[]); 
    void setUnion(int a[], int b[]); 
    void setIntersection(int a[], int b[]);
    
    int main()
    {
    	
        int a[25];
        int b[25];
        int elementA = 0;
    	int elementB = 0;
        int countA = 0;
    	int countB = 0;
    	int searchElement;
    
        printf("Please enter your first set of numbers (enter -1 to end set)\n");
    
    	
        while (countA < 25)
        {
            scanf("%d",&elementA);
    
            if (elementA == -1)
            {
    		a[countA] = elementA;
    		
    		break;
            }
    
    		if (countA>=25){
                break;
            }
    
    		
    			
    		a[countA] = elementA;
    
    		countA++;
    	
    
            
    		
        }
    
    	printf("Please enter your second set of numbers (enter -1 to end set)\n");
    
        while (countB < 25)
        {
            scanf("%d",&elementB);
    
            if (elementB == -1)
            {
    			b[countB] = elementB;
    
    			countB++;
    
    			break;
            }
    
    		if (countB>=25){
                break;
            }
    
            b[countB] = elementB;
    
    		countB++;
        }
    	
    	printf("This is the first set you entered\n{");
    
    	printSet(a);
    
    	printf("}\n");
    
    	printf("This is the second set you entered\n{");
    	
    	printSet(b);
    
    	printf("}\n");
    
    	printf("Number of elements in Set 1 is %d\n", setCardinality(a));
    
    	printf("Number of elements in Set 2 is %d\n", setCardinality(b));
    
    	if(isEmpty(a)==1){
    		printf("The set 1 entered by you is empty.\n");
    	}
    
    	if(isEmpty(b)==1){
    		printf("The set 2 entered by you is empty.\n");
    	}
    
    	if(isSubset(a,b)==1){
    		printf("Set 1 is a subset of set 2");
    	}
    
    	if(isSubset(a,b)==0){
    		printf("Set 1 is not a subset of set 2");
    	}
    
    	setUnion(a,b);
    
    	printf("Please enter the element you would like to search for in Set 1:\n");
    	scanf("%d",&searchElement);
    
    	if(isMember(searchElement, a)==1){
    		printf("The element %d was found in set 1!\n", searchElement);
    	}
    
    	if(isMember(searchElement, a)==0){
    		printf("The element %d was not found in set 1.\n", searchElement);
    	}
    
    	return 0;
    
    }
    
    int setCardinality(int a[]){
    	
    	int i;
    	int numberElements=1;
    
    	if(a[0]==-1){
    		numberElements=0;
    	
    	}
    
    	if(a[0]!=-1){
    		for(i=1;a[i]!=-1;i++){
    		numberElements++;
    		
    		}
    	}
    	return (numberElements);
    
    }
    
    void printSet(int a[]){
    
    	int setCardinality(int a[]);
    	
    	int j;
    
    	for(j=0;j<setCardinality(a);j++){
    		printf("%d,", a[j]);
    	}
    }
    
    int isEmpty(int a[]){
    
    	int setCardinality(int a[]);
    
    	if(setCardinality(a)==0){
    		return 1;
    	}
    	else{
    		return 0;
    	}
    }
    
    int isMember(int x, int a[]){
    	int setCardinality(int a[]);
    	int middle;
    	int low=0;
    	int high;
    	
    	high=setCardinality(a);
    
    	while(low<=high){
    
    		middle = (low+high)/2;
    
    		if(x==a[middle]){
    			return 1;
    		}
    
    		else if(x<a[middle]){
    			high=middle-1;
    		}
    
    		else{
    			low=middle+1;
    		}
    
    		
    	}
    	
    	return 0;
    }
    
    int isSubset(int a[], int b[]){
    	
    	int isMember(int x, int a[]);
    	int setCardinality(int a[]);
    	int i;
    	int search;	
    
    	
    	for(i=0;i<=setCardinality(a);i++){
    
    		search=a[i];
    		
    		if(isMember(search,b)==0){
    			return 0;
    			printf("in if\n");
    		}
    		printf("in for, not in if\n");
    	}
    	
    	printf("here after for\n");
    	return 1;
    	
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    for(i=0;i<=setCardinality(a);i++)
    When used with arrays, this quite often goes off the end by one. Most often it is better to do this.
    Code:
    int i, card = setCardinality(a);
    for ( i = 0; i < card; ++i )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Your isMember function seems a bit weird to me. It starts in the middle and changes position depending on the difference between the value to be compared and the value of the current position in the array and from what I can tell the array isn't sorted so I don't see how this works.

    [edit] I also noticed now that the printf-debugging is done AFTER the return 0. Anything after it won't ever happen, compiler warnings should tell you that too.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by Dave_Sinkula
    Code:
    for(i=0;i<=setCardinality(a);i++)
    When used with arrays, this quite often goes off the end by one. Most often it is better to do this.
    Code:
    int i, card = setCardinality(a);
    for ( i = 0; i < card; ++i )
    That worked! Thanks a lot. I knew it was something stupid.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by OnionKnight
    Your isMember function seems a bit weird to me. It starts in the middle and changes position depending on the difference between the value to be compared and the value of the current position in the array and from what I can tell the array isn't sorted so I don't see how this works.

    [edit] I also noticed now that the printf-debugging is done AFTER the return 0. Anything after it won't ever happen, compiler warnings should tell you that too.
    There were no warnings or anything. isMember was working just fine.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Finally finsihed the assignment! Took forever, probably like 15 hours.

  12. #12
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I took a closer look at isMember and it does in fact not work. It works sometimes and the chances for getting a correct results gets higher for smaller arrays. Here's a tryout program using your function and a simple linear one.

    Code:
    #include <stdio.h>
    
    int setCardinality (int a[]);
    int isMember (int x, int a[]);
    int isMember_linear (int x, int a[]);
    
    int main ()
    {
    	int num;
    	int a[] = {23, 5, 4, 456, 21, 97, 1, 135, 135, 135, 135, 76334, 3235, 912, 54, 345, -1};
    
    	printf("Enter number to test for: ");
    	scanf("%d", &num);
    	puts(isMember       (num, a) ? "isMember        OK" : "isMember        Failed");
    	puts(isMember_linear(num, a) ? "isMember_linear OK" : "isMember_linear Failed");
    	return 0;
    }
    
    int setCardinality(int a[]){
    	
    	int i;
    	int numberElements=1;
    
    	if(a[0]==-1){
    		numberElements=0;
    	
    	}
    
    	if(a[0]!=-1){
    		for(i=1;a[i]!=-1;i++){
    		numberElements++;
    		
    		}
    	}
    	return (numberElements);
    
    }
    
    int isMember(int x, int a[]){
    	int setCardinality(int a[]);
    	int middle;
    	int low=0;
    	int high;
    	
    	high=setCardinality(a);
    
    	while(low<=high){
    		middle = (low+high)/2;
    
    		if(x==a[middle]){
    			return 1;
    		}
    
    		else if(x<a[middle]){
    			high=middle-1;
    		}
    
    		else{
    			low=middle+1;
    		}
    
    		
    	}
    	
    	return 0;
    }
    
    int isMember_linear(int x, int a[])
    {
    	int i;
    	for (i = 0; a[i] != -1; i++)
    		if (a[i] == x)
    			return 1;
    	return 0;
    }

Popular pages Recent additions subscribe to a feed