A8 Vectors - sheerazwalid/COMP-I GitHub Wiki
Place source code files for this assignment in a folder named vectors within your Cloud9 workspace. The files should be named as ex1.cpp for exercise 1, ex2.cpp for exercise 2, and so on.
Provide an implementation of the maximum function whose declaration is shown below. The only argument of the function is a vector v of int. The function returns the largest int that is in the vector v. Write test code that thoroughly tests the function. The test code should use assertions.
int maximum(const vector<int> & v)
Provide an implementation of the appendVector function whose declaration is shown below. Both arguments of the function are vectors of int. The function should modify vector v by appending to it all the elements of w. For example, if v = (4, 2, 5) and w = (11, 3), then v will become (4, 2, 5, 11, 3) as a result of calling the function. Hint: the vector class has a function called push_back that appends values passed into it.
void appendVector(vector<int> & v, const vector<int> & w);
Write test code that thoroughly tests the function. The test code should use assertions.
int countPrimes(const vector<Number> & v);
Write a function named countPrimes that takes a vector of Number class instances and returns the number of prime numbers in the the vector. Use the Number class you defined in the A7 Classes assignment. The declaration of countPrimes is given above.
Note: For your code to compile, the isPrime function of your Number class should be declared const.
Use the following code for your main function.
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0));
const int N = 1000;
vector<Number> v;
for (int i = 0; i < N; ++i) {
int r;
do { r = rand(); } while (r < 2); // Make sure r is not 0 or 1.
v.push_back(Number(r));
}
cout << "The percentage of primes in [2, " << INT_MAX << "] is "
<< countPrimes(v) / (double) N
<< endl;
}
Implement a function that takes an integer as its sole argument and returns a vector containing the prime factors of that number. For example, if you pass in 100, then function returns the vector (2, 2, 5, 5). The function signature should look like the following.
vector<int> factor(int n)
Write test code that thoroughly tests the function. The test code should use assertions.
Note: You solved a similar problem in the A4 Loops assignment.
Submit your work by sending the url of your Cloud9 workspace to the teaching assistant. The subject line of your email should be 201 Assignment: Vectors.
-
Any of the above problems may appear on a quiz.
-
Any of the problems from A5 Arrays formulated with vectors may appear on a quiz.
-
Any of the problems from A6 Searching and Sorting formulated with vectors may appear on a quiz.
-
Implement a function that replaces in a vector each occurrence of the number 0 with the number 1 and the number 13 with the number 7 and leaves all other values unchanged. For example, the function would convert (13, 1, 0, 26, 7) to (7, 1, 1, 26, 7). A declaration of the function is as follows.
void cleanse(vector<int> & v)
- Appending vectors
Provide an implementation of the appendVector function whose declaration is shown below. Both arguments of the function are vectors of int. The function should modify vector v by appending to it all the elements of w. For example, if v = (4, 2, 5) and w = (11, 3), then v will become (4, 2, 5, 11, 3) as a result of calling the function. Hint: the vector class has a function called push_back that appends values passed into it.
void appendVector(vector<int> & v, const vector<int> & w)
-
Write test code that tests the function you implemented in the previous problem. The test code should use assertions.
-
Vector insertion
Suppose a vector v contains a sequence of integers ordered from lowest to highest. For example, v might contain the values 3, 6, 7, 12. Write a function called insert that takes this vector v as a first argument and an integer k as the second argument. The function inserts k into the vector so that it preserves the order of elements. For example, if v were (3, 6, 7, 12) and k were 5, then after calling the function, v would be (3, 5, 6, 7, 12). In your implementation of insert, do not use the insert function of the vector class (v.insert).
-
Write test code that tests the function you implemented in the previous problem. The test code should use assertions.
-
Counting occurrences
Implement the function countOccurrences whose declaration appears below. The first argument of the function is a vector v of integers and the second argument is an integer k. The function returns the number of times k occurs in v.
int countOccurrences(const vector<int> & v, int k)
- Identical Vectors
Write a function that checks whether two vectors are identical (contain exactly the same elements in the same order). A declaration of the function is shown below. The function returns true if the two vectors are identical; otherwise it returns false.
bool areIdentical(const vector<int> & a, const vector<int> & b)
- Unlucky Vectors
Implement a function that determines if the unlucky number 13 appears in a vector. The function returns true if 13 appears in the vector at least once; otherwise it returns false. A declaration of the function is as follows.
bool isUnlucky(vector<int> & v)
- Ordered Vectors
Write a function called isStrictlyIncreasing that checks whether a vector of integers contains values that are in strictly increasing order. A declaration of the function is shown below. The function returns true if the elements are in strictly increasing order; otherwise it returns false. For example, it will return true for (-2, 4, 5, 6, 8) and it will return false for (3, 4, 6, 6, 9).
bool isStrictlyIncreasing(const vector<int> & v)
- Revealing logic errors
The function maxValue given below is supposed to return the largest value that appears in a vector; however, there is a logic error in the code. Write test code that exposes the error. Your test code should use the assert function to terminate the program.
int maxValue(vector<int> & v) {
int largest = 0;
for (int i = 0; i < len, ++i) {
If (v[i] > largest) largest = v[i];
}
return largest;
}