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
}