@laserlight: I'm trying to initialize a set. In my main function, I initialized both set 1 and set 2.
@anduril: I can't implement the function the way you said I should because this is a homework where the header file is already given; meaning, I should use pointers with arrays. =/
Anyways, I think I have already solved that part. I'm now trying to put an add function to my program. However, it's also having some problems. Please care to check where I went wrong.
This is now my main:
Code:
#include<stdio.h>
#include "Set.h"
#define MAX 10
int main()
{
int elements1[MAX];
int elements2[MAX];
int *cnt1 = 0;
int *cnt2 = 0;
int counter1 = 0;
int counter2 = 0;
int choice = 0;
int item;
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.) End\n\n");
while(counter1 < MAX || counter2 < MAX && choice != 3)
{
printf("? ");
scanf("%d", &choice);
switch(choice)
{
case 1:
if(counter1 < MAX)
{
printf("\nEnter element: ");
scanf("%d", &item);
add(elements1, item, cnt1);
cnt1++;
counter1++;
}
else printf("\nArray full! Cannot add another element!\n");
break;
case 2:
if(counter2 < MAX)
{
printf("\nEnter element: ");
scanf("%d", &item);
add(elements2, item, cnt2);
cnt2++;
counter2++;
}
else printf("\nArray full! Cannot add another element!\n");
break;
default:
printf("\nInvalid choice.\n");
break;
}
printf("\nSet 1:\n");
display(elements1, counter1);
printf("\nSet 2:\n");
display(elements2, counter2);
printf("\n\n");
}
printf("\nCan no longer add another element to either sets.\n");
return 0;
}
And this is the add function (which can only edit the first element of the array, btw)
Code:
void add(int s[],int elem,int *count)
{
count = s;
*count = elem;
}
And just for you to have an idea about how this program would run, here's the modified initialize function:
Code:
void initialize(int s[], int *count)
{
int i;
for(i=0; i < MAX; i++)
{
count = &s[i];
*count = 0;
}
}