My program works how I want it to (excuse the poor coding
myArray.h
#ifndef H_myArray
#define H_myArray
#include <iostream>
using namespace std;
class myArray
{
public:
int& operator[] (int loc);
const int& operator[] (int loc) const;
myArray(int size = 20);
myArray(int start = 0, int end = 20);
~myArray();
void showMenu();
bool isEmpty() const;
bool isFull() const;
int getMaxSize() const;
int getLength() const;
int getStartLoc() const;
void print() const;
void sortAscending();
void insertAt(const int& item, int position);
int getChoice() const;
void initialize();
void search(const int& item) const;
private:
int maxSize;
int length;
int startLocation;
int *list;
int choice;
};
#endif
the implementation .cpp file for the class myArray
#include "myArray.h"
#include <iostream>
#include <cassert>
#include <string>
using namespace std;
//overload the [] operator to accomodate for start position
int& myArray::operator[] (int loc)
{
assert(loc >= startLocation && loc < (maxSize + startLocation));
loc = loc - startLocation;
return list[loc];
}
//overload [] for constant arrays
const int& myArray::operator[] (int loc) const
{
assert(loc >= startLocation && loc < (maxSize + startLocation));
loc = loc - startLocation;
return list[loc];
}
myArray::myArray(int size)
{
startLocation = 0;
maxSize = size;
length = 0;
if(maxSize == 0)
list = NULL;
else
{
list = new int[maxSize];
initialize();
}
}
myArray::myArray(int start, int end)
{
startLocation = start;
maxSize = (end - startLocation);
length = 0;
if(maxSize == 0)
list = NULL;
else
{
list = new int[maxSize];
initialize();
}
}
myArray::~myArray()
{
delete [] list;
}
//display a menu and return the option selected
void myArray::showMenu()
{
string input;
bool valid = false;
choice = 0;
cout << "1: Insert an item into the list" << endl;
cout << "2: Print the contents of the list" << endl;
cout << "3: Search for an item" << endl;
cout << "4: Sort the list in ascending order" << endl;
cout << "5: Exit" << endl;
cout << endl << ">> ";
getline(cin, input);
while(!valid)
{
if(input == "1")
{
choice = 1;
valid = true;
}
else if(input == "2")
{
choice = 2;
valid = true;
}
else if(input == "3")
{
choice = 3;
valid = true;
}
else if(input == "4")
{
choice = 4;
valid = true;
}
else if(input == "5")
{
choice = 5;
valid = true;
}
else
{
cout << endl << "Invalid choice, please try again >> ";
getline(cin, input);
}
}
cout << string(50, '\n'); //clear the screen
}
//return true if the list is empty, false if not
bool myArray::isEmpty() const
{
return (length == 0);
}
//return true if the list is full, false if not
bool myArray::isFull() const
{
return (length == maxSize);
}
int myArray::getMaxSize() const
{
return maxSize;
}
int myArray::getLength() const
{
return length;
}
int myArray::getStartLoc() const
{
return startLocation;
}
void myArray::print() const
{
for(int i = startLocation; i < (maxSize + startLocation); i++)
{
cout << list[i] << " ";
}
cout << endl;
}
//sort the list in ascending order (lowest to highest) using bubble sort
void myArray::sortAscending()
{
int temp;
int iteration;
int index;
for(iteration = 1; iteration < maxSize; iteration++)
{
for(index = startLocation; index < (maxSize + startLocation) - iteration; index++)
{
if(list[index] > list[index + 1])
{
temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
}
}
}
}
void myArray::insertAt(const int& item, int position)
{
assert(position >= startLocation && position < (maxSize + startLocation));
list[position] = item;
length++;
}
int myArray::getChoice() const
{
return choice;
}
void myArray::initialize()
{
for(int i = startLocation; i < (maxSize + startLocation); i++)
insertAt(0, i);
}
void myArray::search(const int& item) const
{
bool found = false;
int index;
for(int i = startLocation; i < (maxSize + startLocation); i++)
{
if(list[i] == item)
{
found = true;
index = i;
}
}
if(found == false)
cout << item << " was not found" << endl;
else
cout << item << " was found in the list " << endl;
}
main
/* Author: hehe
Date: January 30, 2013
Course: CS218a
Assignment: Programming Exercise Week 4
Purpose: Create and manipulate a dynamic array
with a custom starting index
*/
#include <iostream>
#include <string>
#include "myArray.h"
using namespace std;
int main()
{
string input;
while(input != "N" && input != "n" && input != "y" && input != "Y")
{
cout << "Create a list? [Y/N] >> ";
getline(cin, input);
}
if(input != "N" && input != "n")
{
int start;
int end;
int choice = 0;
cout << endl;
cout << "Begin the array index starting at >> ";
cin >> start;
cout << "End the array at >> ";
cin >> end;
cout << string(50, '\n'); //clear the screen
cin.ignore();
//create the object with necessary parameters
myArray arrayObj(start, end);
//cycle the menu and options
while(choice != 5)
{
arrayObj.showMenu();
switch(arrayObj.getChoice())
{
case 1:
int item;
int pos;
cout << "What number do you want to insert? >> ";
cin >> item;
cout << "At which index do you want to insert " << item << "? >> ";
cin >> pos;
cout << string(50, '\n');
arrayObj.insertAt(item, pos);
cout << item << " was inserted at index " << pos;
cout << string(3, '\n');
cin.ignore();
break;
case 2:
cout << "Contents of the list:" << endl;
arrayObj.print();
cout << string(3, '\n');
break;
case 3:
int num;
cout << "Which number do you want to search for? >> ";
cin >> num;
cout << string(50, '\n');
arrayObj.search(num);
cout << string(3, '\n');
cin.ignore();
break;
case 4:
arrayObj.sortAscending();
cout << "List sorted" << endl;
cout << string(3, '\n');
break;
case 5:
cout << "Have a nice day :)/>" << endl;
break;
}//end switch
choice = arrayObj.getChoice();
}//end while
}//end if
cout << endl;
system("pause");
return 0;
}

New Topic/Question
Reply




MultiQuote




|