// Author: Keith Shomper // Date: 10/3/05 // Purpose: To demonstrate array access and modification #include #include using namespace std; int main() { const int SIZE = 10; string s; int insertionPoint; int insertionValue; int a[SIZE]; // Initialize the random number generator srand(time(0)); // Build an arbirary array of SIZE floats each element being between 1 and 100 for (int i = 0; i < SIZE; i++) { a[i] = rand() % 100 + 1; } // Print out the contents of array 'a' cout << "The array 'a' has the values: " << endl << '\t'; for (int i = 0; i < SIZE; ++i) { cout << a[i] << " "; } cout << endl << endl << "Press enter to continue .... "; getline(cin, s, '\n'); cout << "Into which position would you like to insert a value: "; cin >> insertionPoint; cout >> "What integer value would you like to insert: "; cin >> insertionValue; // To insert into position j we need to move the j+1 to SIZE elements to make space for (int j = SIZE-1; j > insertionPoint; --j) { a[j] = a[j-1]; a[j-1] = 0; // not really required, but it let's us see what's happening better // Print out the contents of array 'a' cout << "The array 'a' has the values: " << endl << '\t'; for (int i = 0; i < SIZE; ++i) { cout << a[i] << " "; } cout << endl << endl << "Press enter to continue .... "; getline(cin, s, '\n'); } // Insert the new element a[insertionPoint] = insertionValue; // Print out the contents of array 'a' cout << "The array 'a' has the values: " << endl << '\t'; for (int i = 0; i < SIZE; ++i) { cout << a[i] << " "; } cout << endl << endl << "Press enter to continue .... "; getline(cin, s, '\n'); return 0; }