Thread: Need help debugging this

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    @laserlight: I'm trying to initialize a set. In my main function, I initialized both set 1 and set 2.
    @anduril: I can't implement the function the way you said I should because this is a homework where the header file is already given; meaning, I should use pointers with arrays. =/

    Anyways, I think I have already solved that part. I'm now trying to put an add function to my program. However, it's also having some problems. Please care to check where I went wrong.

    This is now my main:

    Code:
    #include<stdio.h>
    #include "Set.h"
    
    #define MAX 10
    
    int main()
    {
    	int elements1[MAX];
    	int elements2[MAX];
    	int *cnt1 = 0;
    	int *cnt2 = 0;
    	int counter1 = 0;
    	int counter2 = 0;
    	int choice = 0;
    	int item;
    	
    	initialize(elements1, cnt1);
    	printf("Set 1 initialized.\n");
    	initialize(elements2, cnt2);
    	printf("Set 2 initialized.\n");
    	printf("\nDisplaying set 1...\n");
    	display(elements1, counter1);
    	printf("\nDisplaying set 2...\n");
    	display(elements1, counter2);
    	printf("\n\nMENU:\n");
    	printf("1.) Add element to Set 1\n");
    	printf("2.) Add element to Set 2\n");
    	printf("3.) End\n\n");
    
    	while(counter1 < MAX || counter2 < MAX && choice != 3)
    	{
    		printf("? ");
    		scanf("%d", &choice);
    		switch(choice)
    		{	
    			case 1:
    				if(counter1 < MAX)
    				{
    					printf("\nEnter element: ");
    					scanf("%d", &item);
    					add(elements1, item, cnt1);
    					cnt1++;
    					counter1++;
    				}
    				else printf("\nArray full! Cannot add another element!\n");
    				break;
    			
    			case 2:
    				if(counter2 < MAX)
    				{
    					printf("\nEnter element: ");
    					scanf("%d", &item);
    					add(elements2, item, cnt2);
    					cnt2++;
    					counter2++;
    				}
    				else printf("\nArray full! Cannot add another element!\n");
    				break;
    			
    			default:
    				printf("\nInvalid choice.\n");
    				break;
    		}
    		
    		printf("\nSet 1:\n");
    		display(elements1, counter1);
    		printf("\nSet 2:\n");
    		display(elements2, counter2);
    		printf("\n\n");
    	}
    
    	printf("\nCan no longer add another element to either sets.\n");
    
    	return 0;
    }
    And this is the add function (which can only edit the first element of the array, btw)

    Code:
    void add(int s[],int elem,int *count)
    {
    	count = s;
    	*count = elem;
    }
    And just for you to have an idea about how this program would run, here's the modified initialize function:

    Code:
    void initialize(int s[], int *count)
    {	
    	int i;
    	for(i=0; i < MAX; i++)
    	{
    		count = &s[i];
    		*count = 0;
    	}
    }
    Last edited by We'reGeeks; 01-06-2011 at 05:40 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    One more thing. I think you need to brush up on operator precedence in C. The following line has a problem since && is stronger than ||:

    Quote Originally Posted by We'reGeeks View Post
    while(counter1 < MAX || counter2 < MAX && choice != 3)[/code]
    That is equivalent to:
    Code:
    while(counter1 < MAX || (counter2 < MAX && choice != 3))
    When what I think you want is:
    Code:
    while((counter1 < MAX || counter2 < MAX) && choice != 3)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help debugging in Pelles C IDE
    By gaurav9991 in forum C Programming
    Replies: 3
    Last Post: 10-30-2010, 07:15 AM
  2. help on debugging
    By Masterx in forum C++ Programming
    Replies: 56
    Last Post: 01-30-2009, 10:09 AM
  3. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  4. Problem in debugging in Eclipse
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 08-21-2007, 09:53 AM
  5. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM