#include #include #include using namespace std; #include "tvector.h" void print (const tvector & v) { int i; for (i=0; i < v.size(); i++) { cout << v[i] << endl; } } int max (const tvector & v) //pre: vector v is not empty //post: return max of elements in v { int i, max_so_far = INT_MIN; for (i=0; i < v.size(); i++) { if (v[i] > max_so_far) { max_so_far = v[i]; } } return max_so_far; } bool issorted (const tvector & v) //post: returns true if the array is acsending sorted { bool s = true; // initially assume that array is sorted //in the function try to break this assumption int i =1; while (i < v.size() && s == true) //check until the end of array or //until a counterexample is found { if (v[i-1] > v[i]) // if not sorted s = false; // counterexample is found i++; } return s; } void insert(tvector & a, int newnum) // NOT const vector // pre: a[0] <= ... <= a[a.size()-1], a is sorted // post: newnum inserted into a, a still sorted { int count = a.size(); // size before insertion a.push_back(newnum); // increase size – newnum is inserted at // the end but this is not important int loc = count; // start searching insertion loc from end while (0 < loc && newnum < a[loc-1]) { a[loc] = a[loc-1]; loc--; // shift right until the proper insertion cell } a[loc] = newnum; //insert } void remove(tvector & a, int pos) // pre: a is sorted // post: original a[pos] removed, a is still sorted { int lastIndex = a.size()-1; int k; for(k=pos; k < lastIndex; k++) { a[k] = a[k+1]; } //shift all elements on the right of pos one cell left a.pop_back(); //remove the last element of the array } int main() { tvector nums; int num; cout << "fill the array with integers" << endl; while (cin >> num) { nums.push_back(num); } cout << "array content" << endl; cout << "-------------" << endl; print (nums); cout << "maximum array element is " << max(nums) << endl; if (issorted(nums)) cout << "array is sorted" << endl; else cout << "array is not sorted" << endl; cout << "enter the element to insert: "; cin.clear(); cin >> num; insert(nums, num); cout << "after insertion" << endl; cout << "---------------" << endl; print(nums); cout << "delete element at location: "; cin >> num; remove(nums, num); cout << "after deletion" << endl; cout << "---------------" << endl; print(nums); return 0; }