Thread: Help passing and storing

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    24

    Help passing and storing

    I need help passing values, i'm still unclear how to pass these and
    also if i want to search two (sorted) arrays for same values how would i do that and how would i then take that value and store it
    into another array

    Thanks

    Code:
    #include <stdio.h>
    #define SIZE 15
    
    /*
    In algebra, the intersection of two sets is defined as a new set that contains all the values common in both sets.  
    For instance, consider the following two sets, A and B:
    
    A  =  {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    B  =  {2, 4, 8, 12, 14, 20, 25, 28, 30, 32}
    
    The only values common to both sets are 2, 4, and 8.  The intersection of A and B, which is denoted as A n B, is
     A n B  =  {2, 4, 8}
    
    Write a simple c program that illustrates how array contents can be processes to perform an operation such as 
    finding the intersection of two sets.  The program will ask the user to enter two sets of values, each stored 
    in an array.  Then it will scan the two arrays looking for values common to both.  The common values will be 
    stored in a third array, whose contents are displayed on the screen.
    
    Use the following variables as needed in the program:
    
    Set1			An array of integers to hold the first set
    Set2			An array of integers to hold the second set
    Intersection	An array of integers to hold the intersection of the set1 and set2
    numIntValue	An integer holding the number of intersecting values.
    */
    
    int getArray(int set1[SIZE], int set2[SIZE]);
    int findIntersection(int set1[SIZE], int set2[SIZE], int intersection[SIZE], int numIntValue);
    void displayIntValue(int intersection[SIZE], int numIntValue);
    
    int main()
    {
    	int set1[SIZE];
    	int set2[SIZE];
    	int intersection[SIZE];
    	int numIntValue = 0;
    
    	getArray(set1, set2);
    	findIntersection(set1, set2, intersection, numIntValue);
    	displayIntValue(intersection, numIntValue);
    
    }
    /*
    getArray()
    set1 and set2 are passed into the function. It prompts the user to enter values for each array.
    */
    int getArray(int set1[SIZE], int set2[SIZE])
    {
    	int count;
    	int pass;
    	int i;
    	int hold;
    
    	printf("Enter 15 numbers for the first set: ");
    
    	for (count=0; count < SIZE; count++ )
    	{
    		scanf( "%i", (set1 + count));
    	}
    
    	printf("Enter 15 numbers for the second set: ");
    
    	for (count = 0; count < SIZE; count++ )
    	{
    		scanf( "%i", (set2 + count));
    	}
    
    	/*
    	Using bubble sort to sort the first array
    	*/
    	for ( pass = 1; pass < SIZE; pass++ )
    	{
    		for ( i = 0; i < SIZE - 1; i++ )
    		{
    			if ( set1[ i ] > set1[ i + 1 ] ) 
    			{
                hold = set1[ i ];
                set1[ i ] = set1[ i + 1 ];
                set1[ i + 1 ] = hold;
    			}
    		}
    	}
    	/*
    	Using bubble sort to sort the second array
    	*/
    	for ( pass = 1; pass < SIZE; pass++ )
    	{
    		for ( i = 0; i < SIZE - 1; i++ )
    		{
    			if ( set2[ i ] > set2[ i + 1 ] ) 
    			{
                hold = set2[ i ];
                set2[ i ] = set2[ i + 1 ];
                set2[ i + 1 ] = hold;
    			}
    		}
    	}
    	return 0; /* returns 0 for succesful completetion */
    }
    
    /*
    findIntersection()
    set1, set2, and intersection are passed into the function.  It scans the arrays for values that appear in both.  
    The intersection values are stored intersection. This function returns the number of intersecting values found.
    */
    int findIntersection(int set1[SIZE], int set2[SIZE], int intersection[SIZE], int numIntValue)
    {
    	int x = 0;
    	int i = 0;
    	int y = 0;
    	int a = 0;
    	int b;
    
    	for (b = 0; b <= SIZE; b++ )
    	{
    
    		if (set1[x] == set2[i])
    		{
    			intersection[y] = set1[x];
    			y++;
    			i++;
    			x++;
    			a++;
    			numIntValue ++;
    		}
    		else
    			i++;
    
    	}
    
    	return numIntValue;
    }
    /*
    displayIntValue()
    The intersection array and the numIntvalues variable are passed into the function.  
    If numIntValues contains a number greater than zero, the function displays that many elements in the intersection 
    array. If there are no intersecting values, the function displays a message indicating so.
    */
    void displayIntValue(int intersection[SIZE], int numIntValue)
    {
    	int x;
    
    	if (numIntValue == 0)
    	{
    		printf("There are no intesecting values !");
    	}
    	else
    	{
    		for (x = 0; x <= 1; x++ )
    		{
    			printf("These values did intersect: ");
    			printf("%i\n", intersection[x]);
    		}
    	}
    }
    Last edited by devilsknight; 07-02-2006 at 02:59 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about an isMemberOfSet() function

    int isMemberOfSet ( float value, float set[] );
    Returns true if value is a member of the set

    Do that for all the members of set1, and if they're also in set 2, then that's your intersection.

    Also, write a function to do the sort, then call it twice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    24

    Set

    Not quite understanding how that works...could someone explain more ?

    Any other suggestions ?
    And does my code look alright ?

    thanks for your time

    b

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Basically make a function that takes an integer and an integer array and checks to see if that integer is in the array. Then loop through array A passing each value along with the array B to the function. This will check if the digit is in array B. Some psudocode:
    Code:
    BOOLEAN intFind(INT value, INT set[], INT setSize) {
       for INT i from 0 to setSize
          if value is equal to set[i]
             return true
          else if value is greater than set[i]    // because it's ordered as you said
             return false
          increment i
    
       return false   // If you made it to the end without finding a match
    }
    
    /* ...and the usage... */
    
    SET A  =  {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    SET B  =  {2, 4, 8, 12, 14, 20, 25, 28, 30, 32}
    SET C
    
    INT j = 0
    for INT i from 0 to 10
        
        if intFind(A[i],B,10) is equal to true
            C[j] = A[i]
            increment j
        increment i
    Sorry for my sloppy psudocode. I don't know all of the "in"s and "out"s of it.
    Last edited by SlyMaelstrom; 07-02-2006 at 12:45 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    24

    How about this

    How about if i do something like this

    Code:
    int findIntersection(int set1[SIZE], int set2[SIZE], int intersection[SIZE], int numIntValue)
    {
    	int x = 0;
    	int i = 0;
    	int y = 0;
    	int a = 0;
    	int b;
    
    	for (b = 0; b <= SIZE; b++ )
    	{
    
    		if (set1[x] == set2[i])
    		{
    			intersection[y] = set1[x];
    			y++;
    			i++;
    			x++;
    			a++;
    			numIntValue ++;
    		}
    		else
    			i++;
    
    	}
    
    	return numIntValue;
    }
    from this it counts the numbers that match, but it will not pass the value to the array...why is that ? also when numIntValue reaches the function displayIntValue it equals zero...howcome ?

    i've also changed the code above too, on the first post
    Last edited by devilsknight; 07-02-2006 at 02:53 PM.

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by devilsknight
    also when numIntValue reaches the function displayIntValue it equals zero...howcome ?
    The basic problem here is that you're passing a copy of numIntvalue, and that copy dies as soon as you exit the function. What you want is to send it the address, or capture its return value somewhere.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    sweet ok fixed that problem

    now one last problem

    if i compare 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    to 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30

    i get no matches

    but if i compare 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
    to 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    i get the right number of matches

    i know it's something i did in the findIntersection...but what ?

    thanks for all the help

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Find out yourself. Follow the code logic with your own head and figure out what happens when you do that.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem passing data between functions
    By manutdfan in forum C Programming
    Replies: 9
    Last Post: 12-10-2006, 04:32 PM
  2. QUESTION!! parameter passing
    By jave in forum C Programming
    Replies: 8
    Last Post: 10-21-2005, 12:50 PM
  3. problems storing values in varibles
    By stodd04 in forum C Programming
    Replies: 7
    Last Post: 02-08-2005, 11:56 AM
  4. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 06:16 PM
  5. question about passing arg in constructor??
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2003, 07:31 PM