***********************************************************************
CS201 Lab 7, Winter 08
Vectors and Functions
***********************************************************************
1. Here is a function that determines whether two vectors are identical:
bool same(vector a, vector b)
{
if (a.size() != b.size())
return false;
for (int i = 0; i < a.size(); i++)
if (a[i] != b[i])
return false;
return true;
}
Here is a main() function that tests this function:
main()
{
vector a, b;
int t;
cout << "Enter elements of a, terminate with -1: ";
cin >> t;
while (t != -1) {
a.push_back(t);
cin >> t;
}
cout << "Enter elements of b, terminate with -1: ";
cin >> t;
while (t != -1) {
b.push_back(t);
cin >> t;
}
if (same(a, b) == true)
cout << "The two vectors are identical.\n";
else
cout << "The two vectors are not identical.\n";
}
Understand, type in (don't cut and paste), compile, run, and test the program.
2. Now modify same() to construct and return a vector of bools called v.
Element v[i] is set to true if a[i] == b[i] and to false if a[i] != b[i].
After the function call print out the elements of all three vectors as follows:
5 5 Equal
2 2 Equal
6 7 Nor Equal
1 1 Equal
3 3 Equal
...
What happens if a.size() != b.size()?!
Hand in a printout of the program.
3. Modify the mean and standard deviation program from the previous lab
to use a function. The header of the function should look like:
void stat(vector v, double & mean, double & sd)
Note the function has a void return type because it computes and needs to return
two values, but functions cannot return more than one value. This implies
that mean and sd (two values) must be passed by reference (use of the & sign)
so that the calling function can access the new values of mean and sd.
The main program should enter the data points until end-of-file, call stat(),
and then print the mean and sd.
Hand in a print out of the program and typescript of two different sample runs.