Hi. I'm using Visual C++ right now. We are to make a program that uses an array structure but I don't know how to implement it. We are to implement the following functions:
void initialize(int s[], int *count);
- simply set count to 0
void display(int s[], int count);
- display on the screen all valid elements of the array, from 0..count-1
void add(int s[],int elem,int *count);
- simply store elem in the array indexed by count then increment count
int contains(int s[],int count, int elem);
- search the array elements for the value elem
int isEmpty(int count);
- the array is empty if count is 0
Here's what I got so far:
Set.h
Code:
#pragma once
#define MAX 10
struct Set
{
int elements[MAX];
int count;
};
class Set
{
//Builder
~Set(void);
Set(void);
//Operations
public:
void initialize(int s[], int *count);
void display(int s[], int count);
void add(int s[],int elem,int *count);
int contains(int s[],int count, int elem);
int isEmpty(int count);
}
[/PHP]
Set.cpp
Code:
#include "Set.h"
Set::Set(void)
{
}
Set::~Set(void)
{
}
void Set::initialize(int s[], int *count)
{
int i;
for(i=MAX;s[i]>=0;i--)
{
s--;
}
return *count = 0;
}
void Set::display(int s[], int count)
{
int i;
for(i=0;s[i]!=null;i++)
{
cout << "%d ", s[i];
count++;
}
}
void Set::add(int s[],int elem,int *count)
{
int i;
for(i=0;s[i]!=null;i++)
{
*count++;
s++;
}
s+=elem;
*count++;
cout << "Result: %d", s[*count];
}
int Set::contains(int s[],int count, int elem)
{
for(i=0;i<count;i++)
{
if(s[i]==elem)
cout << "%d found!", elem;
else
cout << "%d not found!", elem;
}
}
int Set::isEmpty(int *count)
{
if(count = 0)
return 0;
}
Please do correct me if there's anything wrong with what I'm doing. It's been a while since I last programmed using C++ and I really can't remember a thing >.<
I got the following errors for Set.cpp:
1>error C2011: 'Set' : 'struct' type redefinition
1>see declaration of 'Set'
1>'{' : missing function header (old-style formal list?)
Now, if you could just give me an idea as to how I should implement these in Main.cpp, that would be much help. Thanks!