HELP IN C

Just a quick note. If you flip your surrounding if statement, you can tuck the 'return FALSE' away at the top and leave everything outside the nasty braces.

Your internal if statement is almost there. I think what 'count' is is the number of elements in the array. If this is the case, you're accessing the item just after the last, which shouldn't exist. You need to access the 'i'th value and compare it to value. That would make more sense.

Also watch out for your for loop. The second part doesn't look right. It's saying 'i is from 0 to set->arrValue[count]' (again, arrValue[count] shouldn't exist). I believe what you're trying to say is 'i is from 0 to the last item's index'.

As a side note, surely 'count' should be a member of the structure Set?

I hope that gives you some clue.
 
The key to writing a good algorithm is planning the steps out before you implement them in code. I like to use comments to do just that and leave them in afterwards as code documentation. Seriously, you gotta comment as you go or else you'll come back to it later and not know what the heck you were thinking.

Code:
Boolean Set_Insert(Set* set, SetEntry value)
{
    // "The parameter set is a pointer to an initialized set."
    // [B]me[/B]:  Return with error value (FALSE) if uninitialized.

<insert code here>

    // "If the value already exists in the set, do not insert it again."
    // [B]me[/B]:  Search the set for the parameter value.

<insert code here>

    // "The function should return TRUE if the value was inserted or FALSE if
    //    the value already existed in the set.  If the value already exists in 
    //    the set, do not insert it again.  The parameter value is the value to
    //     be added to the set."
    // [B]me[/B]:  If value was found, return FALSE; otherwise insert the value
    //    into the set.

<insert code here>
}

It may not look like much but you wouldn't believe how much easier it is to write a solution when you break the steps down into their smallest parts. I didn't break it down as far as it should probably go but then again I don't want to give away the solution ;)
 
Ok, so I got the Set_Insert working.

I'm having trouble with the Set_Delete. I try to compile it but there's tons of errors.

Here's my entire code for you guys to compile if you want, I'll bold the Set_Delete function.

Header File:
Code:
#ifndef SET_H__
#define SET_H__

typedef enum boolean { FALSE, TRUE } Boolean;

typedef int SetEntry;

typedef struct set {
	
	int count; 
	SetEntry arrValue[50];
    
} Set;


void     Set_Create        (Set* set);
void     Set_Clear         (Set* set);
int      Set_Size          (const Set* set);
Boolean  Set_Empty         (const Set* set);

Boolean  Set_Insert        (Set* set, SetEntry value);
Boolean  Set_Delete        (Set* set, SetEntry value);
Boolean  Set_Contains      (const Set* set, SetEntry value);

void     Set_Union         (const Set* set1, const Set* set2, Set* ret);
void     Set_Intersection  (const Set* set1, const Set* set2, Set* ret);

double   Set_Similarity    (const Set* set1, const Set* set2);

void     Set_Print         (const Set* set);

#endif

Main:

Code:
#include "set.h"
#include <stdio.h>
#include <stdlib.h>

int main(void){
	
	Set helloSet;
	Set *pointer = &helloSet;
	Set_Create(pointer);
	//printf("%d\n", Set_Size(pointer));
    Set_Insert(pointer,20);
	Set_Insert(pointer,30);
	Set_Insert(pointer,20);
	Set_Insert(pointer,32);
	Set_Delete(pointer,30);
	//printf("%d\n", Set_Size(pointer));
	//Set_Insert(pointer,2);
	//printf("%d\n", Set_Size(pointer));
	Set_Print(pointer);
	return 0;
}

void Set_Create(Set* set)
{	
	if(set != NULL){
		set->count = 0;
	}
}

void Set_Clear(Set* set)
{
	if(set != NULL){
		set->count = 0;
	}
}

int Set_Size(const Set* set)
{
	if(set != NULL){

		return set->count;
	}	
    return -1;
}

Boolean Set_Empty(const Set* set)
{
	if(set->count == 0){
		return TRUE;
	}
	else{
		return FALSE;
	}
}

Boolean Set_Insert(Set* set, SetEntry value)
{
	if(set != NULL){
		if(!Set_Contains(set, value)){
			set->arrValue[set->count]= value;
			set->count++;
			return TRUE;
		}
	}

    return FALSE;
}

[b]Boolean Set_Delete(Set* set, SetEntry value)
{
	int i;
	int x;
	if(set!= NULL){ //Check to see if the set has anything in it
		if(Set_Contains(set, value()){//Check to see if it contains a value
			for(i=0; i<set->count;i++){//traverses array
				if(set->arrValue[i]=value){//checks if value I want to delete is there
					for(x=i;i<set->count;i++){//traverses array from where the value was found
						set->arrValue[i] =  set->arrValue[i+1]; //moves the element 1 over to the right
					}
				}
			}
			count--;
		}
	}
    return FALSE;
}
[/b]
Boolean Set_Contains(const Set* set, SetEntry value)
{
	int i;
	for(i=0; i<set->count;i++){
			if(set->arrValue[i] == value){
				return TRUE;
			}
		
		}
    return FALSE;
}

void Set_Union(const Set* set1, const Set* set2, Set* ret)
{
}

void Set_Intersection(const Set* set1, const Set* set2, Set* ret)
{
}

double Set_Similarity(const Set* set1, const Set* set2)
{
    return -1.0;
}

void Set_Print(const Set* set)
{
	int i;
	for(i=0; i<set->count;i++){

		printf("%d\n", set->arrValue[i]);
	}

}

EDIT: I'm an idiot and used the variables x and i in the same for loop statement lol.
 
I'm getting all these weird errors in my Set_Delete function..anyone know what the issue is?
 
As another aside, you can declare multiple variables of the same type using a comma.

Code:
int x, i;

Again, I'd recommend at the top:

Code:
if (set == NULL) return FALSE;

It makes your code slightly clearer.

Finally, there's a typical convention of '80 characters per line'. It has its roots from when terminals used to only be 80 characters long, but it makes code a lot clearer, and it also conforms with other standards like 120 characters per line.

What immediately seems to be wrong here is your if statement:

Code:
if (set->arrValue[i] = value)

You have to be careful with that. There's a common bug in C due to the fact two operators are remarkably easy to mix up. A fault on C's behalf.

The next problem lies in the second for loop. Not sure if that's what you meant in your edit last night. You initialise x and then increment and test i. I don't think that's what you're trying to do.

You may also want to watch what happens after you've moved everything down in that second for loop. It won't be a problem here, in a set, because the value only occurs once, but your outside for loop will keep going round, even when you've removed it.

If any of this is confusing, let me know and I'll try and clear it up.
 
Ok, I got delete and some of the functions running after a long time..I have one more function left and it's racking my brain I can't figure it out. I appreciate all the help you guys have given me, it helped a lot!!

The function I'm having trouble with is: Set_Similarity.

I'll give you all my code with the main function so you guys can just copy and paste it without having to create sets:

set.h
Code:
#ifndef SET_H__
#define SET_H__

typedef enum boolean { FALSE, TRUE } Boolean;

typedef int SetEntry;

typedef struct set
{
	
	int count; 
	SetEntry arrValue[50];
    
} Set;


void     Set_Create        (Set* set);
void     Set_Clear         (Set* set);
int      Set_Size          (const Set* set);
Boolean  Set_Empty         (const Set* set);

Boolean  Set_Insert        (Set* set, SetEntry value);
Boolean  Set_Delete        (Set* set, SetEntry value);
Boolean  Set_Contains      (const Set* set, SetEntry value);

void     Set_Union         (const Set* set1, const Set* set2, Set* ret);
void     Set_Intersection  (const Set* set1, const Set* set2, Set* ret);

double   Set_Similarity    (const Set* set1, const Set* set2);

void     Set_Print         (const Set* set);

#endif

set.c
Code:
#include "set.h"
#include <stdio.h>
#include <stdlib.h>

int main(void){
	
	Set helloSet;
	Set *pointer = &helloSet;
	Set_Create(pointer);
	Set byeSet;
	Set *ptr = &byeSet;
	Set_Create(ptr);
	Set ret;
	Set *rett = &ret;
	Set_Create(rett);
	//printf("%d\n", Set_Size(pointer));
    Set_Insert(pointer,10);
	Set_Insert(pointer,8);
	Set_Insert(pointer,27);
	Set_Insert(pointer,6);
	Set_Insert(ptr, 10);
	Set_Insert(ptr, 22);
	Set_Insert(ptr, 2);
	Set_Insert(ptr, 6);
	Set_Insert(ptr, 1);
	//Set_Intersection(pointer, ptr, rett);
	//printf("%d\n", Set_Size(pointer));
	//Set_Insert(pointer,2);
	//printf("%d\n", Set_Size(pointer));
	Set_Print(rett);
	printf("%g", Set_Similarity(pointer, ptr));
	return 0;
}
//This function creates a set and initalizes everything in it. Set should be empty
void Set_Create(Set* set)
{	
	if(set != NULL)
	{

		set->count = 0;

	}

}
//This function removes every element inside the set resulting in an empty set
void Set_Clear(Set* set)
{
	if(set != NULL)
	{

		set->count = 0;

	}

}
//This function returns the number of elements in the set
int Set_Size(const Set* set)
{
	if(set != NULL){

		return set->count;
	}
	
    return -1;
}
//This function returns true if the set is empty, otherwise it returns false
Boolean Set_Empty(const Set* set)
{
	if(set->count == 0)
	{

		return TRUE;
	}

	else
	{

		return FALSE;
	}

}
//This function inserts a new value in the set. It must also ensure that duplicate values will not be inserted
Boolean Set_Insert(Set* set, SetEntry value)
{
	if(set != NULL)
	{

		if(!Set_Contains(set, value))
		{

			set->arrValue[set->count]= value;
			set->count++;
			return TRUE;

		}

	}

    return FALSE;
}
//This function removes a value from the set
Boolean Set_Delete(Set* set, SetEntry value)
{
	int i,x;
	if(set!= NULL){

		if(Set_Contains(set, value))
		{

			for(i=0; i<set->count;i++)
			{

				if(set->arrValue[i]==value)
				{

					for(x=i;x<set->count;x++)
					{
						set->arrValue[i] =  set->arrValue[i+1];
					}

				}

			}

			set->count--;
		}

	}

    return FALSE;
}
//This function checks whether a value exists in a set
Boolean Set_Contains(const Set* set, SetEntry value)
{
	int i;
	for(i=0; i<set->count;i++)
	{

			if(set->arrValue[i] == value)
			{
				return TRUE;
			}
		

		}

    return FALSE;
}
//This function computes the union of 2 sets
void Set_Union(const Set* set1, const Set* set2, Set* ret)
{
	int i, x;
	for(i=0; i<set1->count; i++)
	{

		for(x=0; x<set2->count; x++)
		{

			if(set1->arrValue[i] != set2->arrValue[x])
			{
				Set_Insert(ret, set1->arrValue[i]);
				Set_Insert(ret, set2->arrValue[x]);
			}

		}

	}
	
}
//This function computes the intersection of 2 sets
void Set_Intersection(const Set* set1, const Set* set2, Set* ret)
{
	int i, x;
	for(i=0; i<set1->count; i++)
	{
		for(x=0; x<set2->count; x++)
		{
			if(set1->arrValue[i] == set2->arrValue[x])
			{
				Set_Insert(ret, set2->arrValue[i]);
			}

		}

	}

}
//This function computes the similiarity of 2 sets
double Set_Similarity(const Set* set1, const Set* set2)
{	
	Set simSet;
	Set *ptrsimSet = &simSet;
	Set_Create(ptrsimSet);
	
	Set simSet2;
	Set *ptrsimSet2 = &simSet2;
	Set_Create(ptrsimSet2);

    Set_Union(set1, set2, ptrsimSet);
	Set_Intersection(set1, set2, ptrsimSet2);

	int simSetSize = ptrsimSet->count;
	int simSet2Size = ptrsimSet2->count;
	
	double setDivision = simSetSize/simSet2Size;

	return setDivision;

}
//This function prints the contents of a set
void Set_Print(const Set* set)
{

	int i;
	for(i=0; i<set->count;i++)
	{
		printf("%d\n", set->arrValue[i]);
	}

}

The description for Set_Similarity is this:
We can measure how similar two sets are by dividing the size of their intersection by the size of their
union. The result is a value between 0 and 1, inclusive. If the sets are identical, then their intersection
will be the same as their union, so their similarity measure will be 1. If the sets have no elements in
common, then their intersection will be empty, so the similarity measure will be 0. If the sets are not
identical but have some elements in common, their similarity measure will fall somewhere between 0
and 1. For example, given A and B as defined above, we have:
similarity(A, B) = |A?B| / |A?B| = 3 / 9 = 0.333333

I keep getting 3?

Hopefully you guys can help..this is the last function.
 
well for one, you appear to be doing union/intersection instead of intersection/union

try to create better variable names to avoid this confusion in the future
 
Back
Top