//highArray.cpp
//demonstrates array class with high-level interface
#include <iostream>
#include <vector>
using namespace std;
////////////////////////////////////////////////////////////////
class HighArray
{
private:
vector<double> v; //vector v
int nElems; //number of data items
public:
//--------------------------------------------------------------
HighArray() : nElems(0) //default constructor
{ }
//--------------------------------------------------------------
HighArray(int max) : nElems(0) //1-arg constructor
{ v.resize(max); } //size the vector
//--------------------------------------------------------------
bool find(double searchKey) //find specified value
{
int j;
for(j=0; j<nElems; j++) //for each element,
if(v[j] == searchKey) //found item?
break; //exit loop before end
if(j == nElems) //gone to end?
return false; //yes, can’t find it
else
return true; //no, found it
} //end find()
//--------------------------------------------------------------
void insert(double value) //put element into array
{
v[nElems] = value; //insert it
nElems++; //increment size
}
//--------------------------------------------------------------
void insertatbegining(double value) //put element into array
{
//moving existing elements down before adding new element to array.
auto it = v.begin();
//inserting at beginning of array
it = v.insert( it, value );
nElems++;
}
//--------------------------------------------------------------
bool remove(double value) //remove element from array
{
int j;
for(j=0; j<nElems; j++) //look for it
if( value == v[j] )
break;
if(j==nElems) //can’t find it
return false;
else //found it
{
for(int k=j; k<nElems; k++) //move higher ones down
v[k] = v[k+1];
nElems--; //decrement size
return true;
}
} //end delete()
//--------------------------------------------------------------
void display() //displays array contents
{
for(int j=0; j<nElems; j++) //for each element,
cout << v[j] << " " ; //display it
cout << endl;
}
//--------------------------------------------------------------
void showmenu()
{
cout<<"Please choose one of the options listed below: \n"
<<"1. Insert new\n"
<<"2. Find \n"
<<"3. Delete \n"
<<"4. Insert from beginning \n"
<<"5. Show all results. \n";
}
//--------------------------------------------------------------
}; //end class HighArray
////////////////////////////////////////////////////////////////
int main()
{
int maxSize = 100; //array size
int number, i, item, grade;
int choice;
bool caution;
char answer;
cout<<"Welcome to your personal gradebook \n"
<<"To begin... \n";
HighArray arr(maxSize); //vector
arr.showmenu();
cin>>choice;
while (choice != 6)
{
switch (choice)
{
case 1: //insert items
cout<<"How much grades will you be entering? \n";
cin>>i;
for (int j=0; j<i; j++)
{
cout<<"Please enter the numbers below one by one, each one followed by pressing enter key \n";
cin>>number;
arr.insert(number);
}
arr.showmenu();
cin>>choice;
break;
case 2: //Find item
cout<<"Please enter a grade to locate \n";
cin>>item;
if (arr.find(item) )
{
cout<<"Found "<<item<<". \n";
}
else
{
cout<<"Can't locate "<<item<<"! \n";
};
arr.showmenu();
cin>>choice;
break;
case 3: //Delete Item
cout<<"Please enter the grade you would like to delete \n";
cin>>grade;
cout<<"Are you sure you would like to delete grade "<<grade<<"?! \n Enter y to continue \n";
cin>>answer;
if (answer == 'Y' or answer == 'y')
{
arr.remove(grade);
caution = true;
cout<<"Deletion successful = "<<caution<<". \n";
}
else
{
caution = false;
cout<<"Deletion successful = "<<caution<<". \n"
<<"Processed cancelled by user. \n";
};
arr.showmenu();
cin>>choice;
break;
case 4: //Inserting Item from beginning
cout<<"How much grades will you be entering? \n";
cin>>i;
for (int j=0; j<i; j++)
{
cout<<"Please enter the numbers below one by one, each one followed by pressing enter key \n";
cin>>number;
arr.insertatbegining(number);
}
arr.showmenu();
cin>>choice;
break;
case 5: //Displaying Items
arr.display();
cout<<"--------------------------------------------------- \n";
arr.showmenu();
cin>>choice;
break;
default: cout<<"This is not an option!! \n";
};
};
cout<<"Good bye \n";
//arr.display(); //display items again
return 0;
} //end main()