Thread: Small problem with printing elements of an array

  1. #16
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    i see that

    i cahnged it around alot. Generally, if you have better format, the code is easier to understand. I was thinking about it, and what if you want to let the user enter a negative number? why dont you make a character array, and have one character that you enter to exit the loop?

  2. #17
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    Quote Originally Posted by xixpsychoxix
    i cahnged it around alot. Generally, if you have better format, the code is easier to understand. I was thinking about it, and what if you want to let the user enter a negative number? why dont you make a character array, and have one character that you enter to exit the loop?
    i'm not too concerned about changing that right now, I just need to finish the assignment. I've added a couple things, but it still needs some more.

    what i still have to do is :
    Print out whether the first set entered by the user is a subset of the second
    Print out the union and intersection of the two sets

    ...whatever that means. I'll be working on it for a while now.




    Code:
    #include <stdio.h>
    
    
    void printset(int [] );   //function that prints the entered numbers in an array
    void searcher(int [], int );  //function that prompts user to enter a number and searches set 1 for that number
    
    int main()
    {
    	int i = 0, j = 0, srchlz, a[25] = {0}, b[25] = {0};
    
    	printf("Choose some positive integers for set 1 (-1 to end set) :\n");
    	for(i=0;i<25;i++)
    	{
    		scanf("%d",&a[i]);
    		 if (a[i] == -1)
      {
       i = 25;
      }	
    
    
    	
    	}
    
    	printf("Choose some more for set 2 (-1 etc):\n");
    	for(j=0;j<25;j++)
    	{
    		scanf("%d",&b[j]);
    		 
    if (b[j] == -1)
      {
       j = 25;
      }	
    	
    	}
    
    
    
    
    printf("Elements of set 1 are: \n");  
    printset(a);								//run printset function for the first set of numbers
    
    
    printf("Elements of set 2 are: \n");
    printset(b);								//run printset function for second set 
    
    
    printf("enter a number to search for in set 1:  ");
    scanf("%d", &srchlz);
    printf("\n");
    searcher(a,srchlz);			//run function to search for user specified number in set 1
    
    
    
    return 0;
    
    }
    	void printset(int c[])		//printing function
    	{
    		int k;
    	printf("{");
    	k = 0;
     for (k=0;k<25;k++)
     {
    	if(c[k] != 0 && c[k] !=-1)
    	printf("%d, ",c[k]);
    
    	else if ( c[k] =-1)			//quit printing numbers when -1 is entered
    	break;
    	
    	
    	}
    	
    		printf(" }\n");
    printf("the number of elements in this set is: %d\n",k);
    	printf("\n\n");	
    }
    
    	void searcher(int d[], int srchlz)		///number searching function
    	{
    		int l;
    		for(l=0;l<25;l++)
    		{
    		if (srchlz != d[l])
    			continue;	
    				
    			
    		else if (srchlz == d[l])
    			
    			printf("element %d is in set 1\n\n", srchlz);
    			l = 25;												//function has found number in set, no need to search further
    			
    		}	
    
     if( l = 25 && srchlz != d[0])						//this is probably confusing, eh?
    				printf("element %d is not in set 1\n\n", srchlz);			//function didn't locate the specified number
    		
    
    	}

  3. #18
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    apparently last night i changed it so you can enter negative numbers, but -1 to quit.

    Code:
     if (a[i] == -1)
      {
       i = 25;
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  2. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 01:51 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. C Filling and Printing a 2-d array
    By Smoot in forum C Programming
    Replies: 3
    Last Post: 11-13-2003, 08:42 PM
  5. Replies: 2
    Last Post: 08-03-2003, 10:01 AM