Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Alex Mardikian
- CS 250
- 01 September 2011
- Excercise 01 - Vector and Arrays
- */
- #include <cstdlib> // for rand
- #include <ctime> // for rand
- #include <iostream>
- #include <vector>
- #include <Windows.h> // for sleep to prevent rand seed time from being the same
- using namespace std;
- void fillVector(vector<int>& v, int size);
- // fillVector - Either randomly fills a vector or has user input values
- // @param vector<int>& - The vector to fill
- // @param int - Predefined amount of elements to randomly fill vector with
- void printVector(const vector<int>& v);
- // printVector - Prints the vector contents to the console
- // @param const vector<int>& - Vector to print to console
- bool equals(const vector<int>& v1, const vector<int>& v2);
- // equals - Checks if two vectors are equal
- // @param const vector<int>& - The first vector to compare
- // @param const vector<int>& - The second vector to compare
- // @return bool - True if they are equal, else false
- void fillArray(int *a, int size);
- // fillArray - Eitehr randomly fills an array or has user input values
- // @param int* - Pointer to the array to fill
- // @param int - Size of the array
- void printArray(const int *a, int size);
- // printArray - Prints the array contents to the console
- // @param const int* - Pointer to the array to print
- // @int - size of the array to print
- void replaceElement(int *a, int size);
- // replaceElement - Asks the user to input a value to replace the largest
- // value in the array with
- // @param int* - The array to replace the largest value of
- // @param int - Size of the array
- int main( )
- {
- // Predefine size for array and vectors if using random fill
- cout << "Enter the size of the array (and random filled vectors): ";
- int size = 0;
- cin >> size;
- cout << endl;
- // Create both vectors and array
- vector<int> v1, v2;
- int *a = new int[size];
- // Print out to console the options for filling vectors and array
- cout << "Vector 1: ";
- fillVector(v1, size);
- cout << "Vector 2: ";
- fillVector(v2, size);
- cout << "Array: ";
- fillArray(a, size);
- // Print the contents of both vectors to the console
- cout << "\nVector 1 contents: ";
- printVector(v1);
- cout << "Vector 2 contents: ";
- printVector(v2);
- // Check if both vectors are equal and print to console
- if (equals(v1, v2))
- cout << "\nVector 1 and vector 2 are equal." << endl;
- else
- cout << "\nVector 1 and vector 2 are not equal." << endl;
- // Print to console array contents
- cout << "\nArray contents: ";
- printArray(a, size);
- // Have user input a new value to replace the largest value in the array with
- replaceElement(a, size);
- // Print the the midified array to the console
- cout << "\nModified Array: ";
- printArray(a, size);
- // Delete and set pointer to NULL
- delete [] a;
- a = NULL;
- cout << endl;
- system("Pause");
- return 0;
- }
- void fillVector(vector<int>& v, int size)
- {
- v.clear();
- // Got lazy with testing so having the option for random fill made the check if equals quick to test
- cout << "1 for manual fill, 2 for random fill: ";
- int option;
- cin >> option;
- if (option == 1)
- {
- cout << "\nEnter integer values to fill the vector(-1 to exit): ";
- int value;
- cin >> value;
- while (value != -1)
- {
- v.push_back(value);
- cin >> value;
- }
- cout << endl;
- }
- else
- {
- Sleep(1000); // Fixes problem where the vecotrs or array would generate the same values based on time seed
- srand((unsigned)time(0));
- for(int i = 0; i < size; i++)
- v.push_back((rand() % (10 * size)) + 1);
- }
- }
- void printVector(const vector<int>& v)
- {
- for(unsigned int i = 0; i < v.size(); i++)
- cout << v[i] << " ";
- cout << endl;
- }
- bool equals(const vector<int>& v1, const vector<int>& v2)
- {
- return (v1==v2)?true:false;
- }
- void fillArray(int *a, int size)
- {
- // Too lazy to enter values every time the program ran
- cout << "1 for manual fill, 2 for random fill: ";
- int option;
- cin >> option;
- if (option == 1)
- {
- cout << "\nEnter integer values to fill the array(-1 to exit): ";
- int value;
- cin >> value;
- int idx = 0;
- while (value != -1)
- {
- a[idx] = value;
- idx++;
- cin >> value;
- }
- }
- else
- {
- Sleep(1000); // Fixes problem where the vecotrs or array would generate the same values based on time seed
- srand((unsigned)time(0));
- for(int i = 0; i < size; i++)
- a[i] = (rand() % (10 * size)) + 1;
- }
- }
- void printArray(const int *a, int size)
- {
- for(int i = 0; i < size; i++)
- cout << a[i] << " ";
- cout << endl;
- }
- void replaceElement(int *a, int size)
- {
- cout << "\nEnter a value to replace the largest in the array: ";
- int newElement;
- cin >> newElement;
- int idx = 0;
- for(int i = 1; i < size; i++)
- if (a[idx] < a[i])
- idx = i;
- a[idx] = newElement;
- }
Add Comment
Please, Sign In to add comment