|
//file: p002.cpp //Author: AOU #include <iostream.h> int const MAX_SIZE = 20; //Problem: // List of values (duplicates allowed) kept in order // with several operations: // initialize, insert, delete, modify, search, ... //Example: // {}, {50}, {25,50}, {25,36,50}, {25,36,50,76}, // {25,36,50,76} //Algorithms: //////////////////////////////////////////////////////// //void initialize(int a[], int &n) //////////////////////////////////////////////////////// /* Description: Initializes the list of values by setting n=0, and a[0..MAX_SIZE-1] = 0; Algorithm: n =0 for i=0 to MAX_SIZE-1 a[i] = 0; */ void initialize(int a[], int &n) { n = 0; for (int i=0; i<=MAX_SIZE-1; i++) a[i] = 0; } //////////////////////////////////////////////////////// //bool insert(int a[], int &n, int x) //////////////////////////////////////////////////////// /* Description: inserts x into the list and maintains the order Example: {}, {50}, {25,50}, {25,36,50}, {25,36,50,76}, {25,36,50,76} Algorithm: Case0: list is full Case1: x goes to empty list Case2: x < all the values in the list Case3: x > all the values in the list Case4: x goes between two values Case5: x is duplicate */ bool insert(int a[], int &n, int x) { if (n >= MAX_SIZE) return false; if (n == 0) { a[0] = x; n = 1; return true; } } //////////////////////////////////////////////////////// //void display(int a[], int n) //////////////////////////////////////////////////////// /* Description: Algorithm: */ void display(int a[], int n) { for (int i=0; i<=MAX_SIZE-1; i++) cout << a[i] << ' '; cout << endl; } void main(void) { int myArray[MAX_SIZE]; int m; display(myArray, m); initialize(myArray, m); display(myArray, m); } |