PROJECT+6 - PROJECT 6: Sorted Arrays The last ...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: PROJECT 6: Sorted Arrays The last project is fairly easy: I need you to create your own class called SortedArray. This class is like a common array with one major difference: the array that you have must at all times be sorted. As an example, assume that you start off an array (initially empty) and you add number 2 to it. Then your array looks like this: 2 After that if you add 5 to it, you would have 2- >5 Assume now that you want to input number 3. The array now should be: 2- >3- >5 For this project, you only need to write the class. The main is given to you in the following section along with the output your program should produce. Notice that inside main we have 3 major methods (plus the constructor). The insert(int) method inserts the argument in the correct place. The remove(int) methods removes the argument if it is in the list, otherwise leaves the array as is. Last the print() method prints out the array as is at each time. Also notice that we have two overloaded operators: the + and the – operators. The + one is doing exactly the same job as the insert whule the – the same things as the remove. MAIN int main(){ SortedArray x; int inp; do{ cin >> inp; x.insert(inp); x.print(); }while(inp>=0); x+6; x.print(); do{ cin >> inp; x.remove(inp); x.print(); }while(inp>=0); x-1; x.print(); } return 0; OUTPUT Assume that the user gives in a row the following values: 1 3 2 7 5 9 7 - 1 5 7 3 - 1 Then the output should be: 1 1 3 1 2 3 1 2 3 7 1 2 3 5 7 1 2 3 5 7 9 1 2 3 5 7 7 9 1 2 3 5 6 7 7 9 1 2 3 6 7 7 9 1 2 3 6 7 9 1 2 6 7 9 2 6 7 9 ...
View Full Document

Ask a homework question - tutors are online