7. What is the main difference between a vector
object and an array object?
between a vector and a
list object?
14. Give the value of v.size() after executing each of the following statements:
vector<int> v(25); //___________15. Given the declarations
v.push_back(-4); //___________
v.resize(40); //___________
m = v.back(); //___________
v.pop_back(); //___________
int arr[] = {4, 6, 2, 9, 3, 8, 7, 1};what is the list of elements in v after the execution of the statement v.resize(5)?
int arrSize = sizeof(arr)/sizeof(int);
vector<int> v(arr, arr+arrSize);
16. Trace the following code, and give the output:
int arr[] = {4, -6, 22, 7, 13, 8};Chapter 5 (pages 268 to 273)
int arrSize = sizeof(arr)/sizeof(int);
vector<int> v(arr, arr+arrSize);while (!v.empty())
{
cout<< v.back() <<" ";
v.pop_back();
}
int * a = new int(35);(a) a is an array of 35 integers.
Written Exercises
15. Assume the declaration
int m = 35, *intPtr;(a) Assign intPtr a value so that it points to m.
double x, *dblptr;
17. Repeat the following declaration for each part, (a)-(d). Fill in the value for *p and *q.
int a = 15, b = 25, *p = &a, *q = &b;(a) *p = *q + 2 *p = ______
19. The upcoming declaration includes integer and pointer variables. For
parts (b)-(e), repeat the
declaration and use the results from part (a).
int x = 6, y = 5, *px, *py;
(a) Write two assignment statements that set px and
py to point at integers x and y, respectively.
(b) Write an output statement using px that prints the
contents of x.
(c) Write a statement using py that assigns 20 as the
contents if y.
(d) What is the resulting value of *py + *px?
(e) After executing *py = *px, are &x and py
equal?
20. The following statement declares both an array and a pointer:
int arr[] = {9, 2, -3, 5, -1}, *px;
(a) Give a
statement that sets px to point at array arr.
(b) Give a cout statement using px that outputs the
value 9 from the array.
(c) Give a cout statement using px that outputs the
value -3 from the array.
(d) Give a statement that sets px to point at element
arr[1]. After executing this statement,
(i)
what is the value of *(px + 2)?
(ii)
what is the value of *px + 2?
21. Trace the following function:
template <typename T>
T *f(T *arr, int n)
{
T *cum = new T[n];
T *p = cum, *end = cum + n;
*p = *arr;
p++;
arr++;
while (p < end)
{
*p =
*(p-1) + *arr;
arr++;
p++;
}
return cum;
}
(a) int arr[] = {3,
5, 7, -9, 6}, *p;
p = f(arr,
sizeof(arr)/sizeof(int));
What are the contents of the dynamic array p after f() has been called?
(b) string str[] =
{"C", "+", "+", " Programs"}, *p;
p = f(str,
sizeof(str)/sizeof(string));
What are the contents of p after f() has been called?
22. What does p point to after the execution of each of the following
declarations?
(a) int *p = new int(20);
(b) int *p = new int[25];
(c) Give the delete statements that
would deallocate the dynamic memory for parts (a) and (b).
Chapter 6:
Pages 319 to 321:
# 13, 14, 16, 17, 18, 19, 20, 21, and 23.
13. Consider the C++ array
int arr[] = {-15, 5, 35, -19, -12, 17, -4};14. Use list<char> objects chList and revList and their member functions:
int arrSize = sizeof(arr) / sizeof(int);(a) Declare the list object intList that holds the integers from the array arr.
(b) Declare the iterator iter for an integer list.
(c) Initialize the iterator iter to the beginning of the list intList.
(d) Assume iter is set to the start of the list.iter++;(e) Use the iterator to write a code segment that scans the list elements and replaces
iter++; // what value does iter point at?
cout << *iter; // what is the output?
iter = intList.end();
iter--;
iter--; // what value does iter point at?
cout << *iter; // what is the output?
iter = intList.end();
iter++; // what value does iter point at?
cout << *iter; // what is the output?
each negative value by the corresponding positive number.
list<char> chList, revList;16. Assume the following declarationss:
char ch;(a) Give the status of chList after each of the following instructions:
chList.push_front('t'); _____________________(b) Write a code segment with a while loop that deletes the characters from chList
chList.push_front('a'); _____________________
chList.push_back('j'); _____________________
chList.pop_front(); _____________________
and store them in revList, but in reverse order. Use the member functions pop_front(),
push_front(), and empty().
list<string> strList;Also assume that the list has the following contents:
list<string>::iterator strIter;
vector list begin insertWhat are the contents of the list after each statement sequence?
(b) strIter = strList.end();
strList.insert(strIter,
"switch");
(c) Assume that
*strIter is "begin"
strIter++;
strList.erase(strIter);
17. Assume that strList is a list object that stores a sequence of strings
and that strIter is an iterator for the object.
Write a code segment that scans the elements in the
list and outputs only those strings
whose length is greater than four elements.
18.Use list<char> objects chList and newList and their member functions:
list<char> chList, newList;Trace the following code:
while (chList.size() != 0)(a) Assume that chList has the three characters in the string "C++".
{
newList.push_front(chList.front());
newList.push_back(chList.front());
chList.pop_front();
}
19. Trace the following code, and display the resulting elements in the list:
int arr[] = {1, 2, 3, 4};20. Trace function inList, which inserts new items into a list:
int arrSize = sizeof(arr) / sizeof(int);
list<int> intList(arr, arr+arrSize);
list<int>::iterator iter = intList.begin();
int i;for(i=1; i <= arrSize; i++)
intList.insert(iter++, i);
template <typename T>(a) Assume that the list<int> object intL is originally empty. What are the elements in the
void inList(list<T>& aList, const T& item)
{
list<T>::iterator iter;iter = aList.begin();
while (iter != aList.end())
{
if (item == *iter)
return;
iter++;
}
aList.push_back(item);
}
21. Implement the function rmOrderedDuplicates(), which removes duplicate values from an ordered list:
template <typename T>For instance, assume that the integer list intList contains the values{2 2 7 8 8 8 15 20 20}.
void rmOrderedDuplicates(list<T>& aList):
23. Implement, according to the instructions in parts (a) and (b), the
function count(), which takes item
as argument and returns the number of times item
occurs in a list.
template <typename T>(a) Implement the function by scanning the list and maintaining a count of the number of occurrences of item.
int count(const list<T>& aList, const T& item);