Okay, we had an assignment in computer class that I have been working on for awhile but what I've got so far isn't working. It seems that it may be a compiler issue, but since I can't get to a linux compiler (what the program will be evaluated on), and since I suspect I have some other errors, I figured I would ask it here. I am not asking for you to rewrite the program, but I would like help as soon as possible, as it is due shortly.
Here's the problem:
1. Define a class called NumberSet that will be used to stored a set of integers.
Use a vector to implement the set (i.e. to store the elements).
2. Create a constructor that takes as an input paramter an array of integers for the
initial value in the set. Clearly you need to tell the constructor the size of your
array (i.e. the number of elements in the array).
3. Then write member functions to
a) (void add(int A)) add an element to the set. If the element is already in the
set, do nothing.
b) (void remove(int A)) remove an element from the set. If the set does not already contain
the element, do nothing.
c) (void clear()) clear the entire set. If the set is already empty, do nothing.
d) (int size()) return the number of element in the set. Return 0 if the set is empty.
e) (void output()) output all elements in the set properly (in any order).
4. Define your ADT class in separate files so that it can be compiled separately. To compile the project, use makefile. Put the main function in its own file separate from the ADT files. In your main
function, make sure you test all member functions (i.e. the constructor,
add, remove, size, and output).
What I have so far are the three files containing the actual code, plus the makefile, which was given to me.
File: SSApp1.cpp
#include <iostream>
#include "SSassg1.h"
using namespace std;
int main()
{
int next;
int horcounter = 0;
int nextcounter = 0;
int array_size_horizontal = 0;
int *array1;
cout << "Please enter the quantity of integers you will enter: ";
cin >> array_size_horizontal;
array1 = new int[array_size_horizontal];
cout << "Please enter " << array_size_horizontal << " integers, then press return: ";
while (cin >> next)
{
nextcounter++;
array1[horcounter++] = next;
if (nextcounter > array_size_horizontal-1)
{
break;
}
}
NumberSet runonce;
int input1;
cout << "To test the add function, please enter an integer and press return: ";
cin >> input1;
runonce.add(input1);
int input2;
cout << "To test the remove function, please enter an integer and press return: ";
cin >> input2;
runonce.remove(input2);
cout << "Now testing size function...\n";
runonce.size();
cout << "Now testing output function...\n";
runonce.output();
cout << "The program is about to end. Now testing clear function...\n";
runonce.clear();
cout << "Goodbye\n";
return 0;
}
File: SSassg1.cpp
#include <iostream>
#include <vector>
#include "SSassg1.h"
using namespace std;
NumberSet::NumberSet(int array1[])
{
//Constructor
for (int e=0; e < array_size_horizontal; c++
{
v1.push_back(array1[e]);
}
delete [ ]array1; //to prevent dangling pointer; now that the values are stored in the vector, they aren't needed here
}
NumberSet::NumberSet( )
{
//Body intentionally left empty (Default Constructor)
}
void NumberSet::add(int A) //need it to test for each element's value and when it gets to the end, if none are equal, then use push_back
{
for (int i=0; i<v1.size( ); i++)
{
if (v1[i] == A)
{
add = 1;
}
else
{
add = 2;
}
switch (add)
{
case 1:
cout << "The number is already present";
for (i; i<v1.size(); i++)
{
}
break;
case 2:
i++;
break;
default:
cout << "error in prgram";
}
if (v1[(v1.size()-1)] == A)
{
add = 1;
}
else
{
v1.push_back(A);
}
}
}
void NumberSet::remove(int A)
{
for (int i=0; i<v1.size( ); i++)
{
if (v1[i] == A);
v1.erase(v1.begin() + i);
i--;
}
}
void NumberSet::clear() //clears entire vector
{
for (int i=0; i<v1.size ( ); i++)
{
v1.erase (v1.begin() + i);
i--;
}
}
int NumberSet::size() //sets variable vectorsize to the size of v1
{
vectorsize = v1.size();
cout << "The size of the vector is: " << vecotrsize <<endl;
}
void NumberSet::output() //outputs entire vector
{
for (unsigned int i=0; i < v1.size( ); i++)
{
cout << v1[i] << " ";
}
cout << endl;
}
File: SSassg1.h
#ifndef SSASSG1_H
#define SSASSG1_H
class NumberSet
{
public:
void add (int A); //adds an element to the set if not already present
void remove (int A); //removes an element from the set if present
void clear(); //clears the set if there are elements present
int size(); //returns the number of elements in the set
void output(); //outputs all elements properly
NumberSet(int array1[]); //transfers the data from an array to a vector
NumberSet( ); //default constructor
private:
vector<int> v1;
int vectorsize;
}
#endif //SSASSG1_H
Also, here is the makefile used to run the program:
File: SSmakefile1.cc
##TARGET: Dependencies
## Instructions
SumnerApp.exe: SumnerApp.o SSassg1.o
g++ SumnerApp.o SSassg1.o -o SumnerApp.exe
SumnerApp.o: SumnerApp.cpp SSassg1.h
g++ -c SomeApp.cpp
SSassg1.o: SSassg1.cpp SSassg1.h
g++ -c SSassg1.cpp
clean:
rm -f SumnerApp.o SSassg1.o SumnerApp.exe
I realize this is a lot, but I would really appreciate any tips anyone can give me.
Thanks a lot!