@anduril & laserlight:
Here is a link to the instructions in my assignment: Instructions
@anduril:
How am I going to use pointers if I used your method? And I would not be following instructions if I didn't pass the parameter cnt to functions initialize and add.
Also, let me note that the parameters for the add function is lacking (as I figured because it actually needs an offset). Anyways, I think I have solved the add function part.
But still, thank you for your advices.
Now here comes a new problem. I'm adding a union function but there seems to be something wrong with it again (I guess I have a knack for getting things wrong all the time). I do understand what union means and thus, know how to get it. However, I can't implement my algorithm in C correctly. Please help me once again.
Here's my union function:
Code:
void getUnion(int result[],int *count,int s1[],int count1, int s2[],int count2)
{
int i, j;
for(i=0; i < MAX_RESULT; i++)
{
for(j=0; j < count2; j++)
{
if(s2[i] != s1[j] || contains(result, i-1, s1[j]) != 1)
{
count = &result[i];
*count = s1[j];
}
if(i >= count1)
{
count = &result[i];
*count = s2[i];
}
}
}
display(result,i);
}
Nothing gets displayed.
And by the way, here's my working add function:
Code:
void add(int s[],int elem,int *count, int cnt) //cnt here is the offset
{
count = &s[cnt];
*count = elem;
}
And here is my modified main function:
Code:
#include<stdio.h>
#include "Set.h"
int main()
{
int elements1[MAX];
int elements2[MAX];
int resultSet[MAX_RESULT]; //I have added #define MAX_RESULT 20 in the header file
int *cnt1 = 0, *cnt2 = 0, *cnt3 = 0;
int counter1 = 0, counter2 = 0;
int choice = 0;
int item;
int offset1 = 0, offset2 = 0;
int set;
int count1 = 0, count2 = 0;
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.) Determine if an element exists in a set\n");
printf("4.) Determine if list is empty\n");
printf("5.) End");
while( offset1 < MAX || offset2 < MAX )
{
printf("\n\n? ");
scanf("%d", &choice);
if(choice == 1)
{
if(offset1 < MAX)
{
printf("\nEnter element: ");
scanf("%d", &item);
add(elements1, item, cnt1, offset1);
printf("\n\nElement added.\n");
offset1++;
}
else printf("\nArray full! Cannot add another element!\n");
}//end choice 1 -- add element to set 1
else if(choice == 2)
{
if(counter2 < MAX)
{
printf("\nEnter element: ");
scanf("%d", &item);
add(elements2, item, cnt2, offset2);
printf("\n\nElement added.\n");
offset2++;
}
else printf("\nArray full! Cannot add another element!\n");
}//end choice 2 -- add element to set 2
else if(choice == 3)
{
printf("\nFrom what set? ");
scanf("%d", &set);
printf("\nEnter element: ");
scanf("%d", &item);
if(set == 1)
{
if( contains(elements1, count1, item) == 1)
printf("\nElement exists.\n");
else printf("\nNo such element exists.\n");
}
else if(set == 2)
{
if( contains(elements2, count2, item) == 1)
printf("\nElement exists.\n");
else printf("\nNo such element exists.\n");
}
else printf("\nInvalid input.\n");
}//end choice 3 -- determine if element exists
else if(choice == 4)
{
printf("\nWhat set? ");
scanf("%d", &set);
if(set == 1)
{
if( isEmpty(offset1) == 1 )
printf("\nSet 1 is empty.\n");
else printf("\nSet 1 is not empty.\n");
}
else if(set == 2)
{
if( isEmpty(offset2) == 1 )
printf("\nSet 2 is empty.\n");
else printf("\nSet 2 is not empty.\n");
}
else printf("\nInvalid input.\n");
}//end choice 4 -- determine if list is empty
else if(choice == 5) break;
else
{
printf("\n\nInvalid choice.\n");
}
if(offset1 == MAX && offset2 == MAX)
printf("\nCan no longer add another element to either sets.\n\n");
}
printf("\nSet 1:\n");
display(elements1, counter1);
printf("\nSet 2:\n");
display(elements2, counter2);
printf("\n\nUnion:\n");
getUnion(resultSet, cnt3, elements1, offset1, elements2, offset2);
return 0;
}
Thanks in advance for any help.