Help passing and storing

Printable View

• 07-02-2006
devilsknight
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]);                 }         } }```
• 07-02-2006
Salem
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.
• 07-02-2006
devilsknight
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
• 07-02-2006
SlyMaelstrom
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.
• 07-02-2006
devilsknight
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
• 07-02-2006
Happy_Reaper
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.
• 07-02-2006
devilsknight
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
• 07-03-2006
jafet
Find out yourself. Follow the code logic with your own head and figure out what happens when you do that.