C++: Help with program using array structures

This is a discussion on C++: Help with program using array structures within the C++ Programming forums, part of the General Programming Boards category; Hi. I'm using Visual C++ right now. We are to make a program that uses an array structure but I ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    28

    Unhappy C++: Help with program using array structures

    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!

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455
    Hi,

    Here is a list of a few thing that have gone wrong.
    1. struct and class shouldn't have the same name.
    Code:
    struct List
    {
    	int elements[MAX];
    	int count;
    };
    2. you have never declared a struct member in your class.

    3. You use the function "initialize" just like the constructor "Set()", then why don't you just use the constructor instead of that function?
    Code:
    class Set
    {
    private: 
       List L1;
    public:
    Set();
    ~Set();
    };
    
    // Constructor:
    Set::Set()
    {
    for(int i = 0;i < MAX;i++)
       L1.elements[i] = 0; //initialze all the member of the vector to zero. 
    L1.count = 0; // set count to zero. 
    }
    
    // here is your add function
    void Set::add(int x) 
    {
    L1.elements[count] = x;
    L1.count++;
    }
    Code:
    int main()
    {
    Set set_example; // set count = 0; set all elements to zero
    set_example.add(10); // set element[0] to 10 and count = 1; do this MAX times to change MAX elements. 
    set_example.display() // to show all elements. 
    }

    for the function "isEmpty", it's better to use boolean type to return true/false instead of returning "int". That's my two cents.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    I know this is pretty easy but I just need someone to help me correct my code...I've been playing with it for a while now but I can't seem to get it right. Please help.

    Set.h
    Code:
    #pragma once
    #define MAX 10
    
    
    struct Sample
    {
    	int elements[MAX];
    	int count;
    };
    
    
    class Set
    {
    	//Builder
    	~Set(void);
    	Set(void);
    	
    	//Operations
    public:
    	void initialize(Sample s);
    	void add(Sample s, int elem);
    	void display(Sample s); 
    	int contains(Sample s, int elem);
    	bool isEmpty(Sample s);
    	void getUnion(Sample s1, Sample s2); 
    	void intersection(Sample s1, Sample s2); 
    	void difference(Sample s1, Sample s2); 
    	int subset(Sample s1, Sample s2); 
    	int disjoint(Sample s1, Sample s2);
    	int equal(Sample s1, Sample s2);
    	
    
    };

    Set.cpp
    Code:
    #include "Set.h"
    
    Set::Set()
    {
    }
    
    Set::~Set(void)
    {
    }
    
    void Set::initialize(Sample x)
    {
    	for(int i = 0;i < MAX;i++)
    		x.elements[i] = 0; //initialize all the member of the vector to zero. 
    	x.count = 0; // set count to zero. 
    }
    
    void Set::add(Sample x, int y)
    {
    	x.elements[count]=y;
    	x..count++;
    	cout << " \n" << y;
    }
    
    void Set::display(Sample x)
    {
    	int i;
    	for(i=0;x.elements[i]!='\0';i++)
    	{
    		cout << " " << x.elements[i];
    		count++;
    		cout << "There are %d elements in the set.\n" << count;
    	}
    }
    	
    int Set::contains(Sample x, int y)
    {
    	int i;
    	for(i=0;i<count;i++)
    	{
    		if(x.elements[i]==y)
    			cout << "%d found!" << y;  
    		else
    			cout << "%d not found!" << y;
    	}
    }
    
    bool Set::isEmpty(Sample x)
    {
    	if(x.count = 0)
    		return 1;
    	else return 0;
    }
    
    void Set::getUnion(Sample x, Sample y)
    {
    	int i, j;
    	for(i=0;x.elements[i]!='\0';i++)
    	{
    		cout<< " " << x.elements[i];
    	}
    	for(j=0;y.elements[j]!='\0';j++)
    	{
    		if(x.elements[i]!=y.elements[j])
    			cout<< " " << y.elements[j];	
    	}
    }
    			
    void Set::intersection(Sample x, Sample y)
    {
    	int i, j, count;
    	Set results();
    	for(i=0;x.elements[i]!='\0';i++)
    	{	
    		for(j=0;y.elements[j]!='\0';j++)
    		{
    			if(x.elements[i]==y.elements[j])
    			{
    				results.add(x.elements[i]);
    				count++;
    			}
    		}
    	}
    }
    I have no code for the Main.cpp yet...Please help. This is a problem involving sets...

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    If anyone could link me to an example that solves a similar problem, that would be very much appreciated. I REALLY DON'T KNOW HOW TO USE C++! T___T

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Quote Originally Posted by nimitzhunter View Post
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of structures
    By feesha in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2003, 08:03 AM
  2. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 03:03 PM
  3. my program wont read vector array
    By MKashlev in forum C++ Programming
    Replies: 5
    Last Post: 08-10-2002, 03:51 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21