CompSci Homework 5
CompSci Homework 5
CompSci Homework 5
96
97
98
99
100
101
Program Output
The donations, sorted in ascending order, are:
5 5 5 5 5 5 10 10 10 10 15 25 25 100 100
The donations, in their original order, are:
5 100 5 25 10 5 25 5 5 100 10 15 10 5 10
What will be displayed if you send the expression *iptr to cout? What happens if
you send the expression ptr to cout?
3. So far you have learned three different uses for the * operator. What are they?
4. What math operations are allowed on pointers?
5. Assuming that ptr is a pointer to an int, what happens when you add 4 to ptr?
6. Look at the following array definition.
int numbers[] = { 2, 4, 6, 8, 10 };
Fill-in-the-Blank
13. Each byte in memory is assigned a unique __________.
14. The __________ operator can be used to determine a variables address.
541
542
Chapter 9 Pointers
Algorithm Workbench
24. Look at the following code.
double value = 29.7;
double *ptr = &value;
Write a cout statement that uses the ptr variable to display the contents of the value
variable.
25. Look at the following array definition.
int set[10];
Write a statement using pointer notation that stores the value 99 in set[7];
26. Write code that dynamically allocates an array of 20 integers, then uses a loop to allow
the user to enter values for each element of the array.
27. Assume that tempNumbers is a pointer that points to a dynamically allocated array.
Write code that releases the memory used by the array.
28. Look at the following function definition.
void getNumber(int &n)
{
cout << "Enter a number: ";
cin >> n;
}
In this function, the parameter n is a reference variable. Rewrite the function so that
n is a pointer.
29. Write the definition of ptr, a pointer to a constant int.
30. Write the definition of ptr, a constant pointer to an int.
True or False
31. T
32. T
33. T
34. T
35. T
36. T
When the indirection operator is used with a pointer variable, you are actually working with the value the pointer is pointing to.
37. T
38. T
When you add a value to a pointer, you are actually adding that number
times the size of the data type referenced by the pointer.
39. T
40. T
You can change the address that an array name points to.
41. T
42. T
43. T
44. T
45. T
A pointer variable that has not been initialized is called a null pointer.
46. T
47. T
In using a pointer with the delete operator, it is not necessary for the
pointer to have been previously used with the new operator.
543
544
Chapter 9 Pointers
// Free memory
Programming Challenges
1. Array Allocator
Write a function that dynamically allocates an array of integers. The function should
accept an integer argument indicating the number of elements to allocate. The function
should return a pointer to the array.
2. Test Scores #1
Write a program that dynamically allocates an array large enough to hold a userdefined number of test scores. Once all the scores are entered, the array should be
passed to a function that sorts them in ascending order. Another function should be
Programming Challenges
called that calculates the average score. The program should display the sorted list of
scores and averages with appropriate headings. Use pointer notation rather than array
notation whenever possible.
Input Validation: Do not accept negative numbers for test scores.
3. Drop Lowest Score
Modify Problem 2 above so the lowest test score is dropped. This score should not be
included in the calculation of the average.
4. Test Scores #2
Modify the program of Programming Challenge 2 to allow the user to enter name-score
pairs. For each student taking a test, the user types the students name followed by the
students integer test score. Modify the sorting function so it takes an array holding
the student names and an array holding the student test scores. When the sorted list
of scores is displayed, each students name should be displayed along with his or her
score. In stepping through the arrays, use pointers rather than array subscripts.
5. Pointer Rewrite
VideoNote
Solving the
Pointer Rewrite
Problem
The following function uses reference variables as parameters. Rewrite the function so
it uses pointers instead of reference variables, and then demonstrate the function in a
complete program.
int doSomething(int &x, int &y)
{
int temp = x;
x = y * 10;
y = temp * 10;
return x + y;
}
545
546
Chapter 9 Pointers
9. Median Function
In statistics, when a set of values is sorted in ascending or descending order, its median
is the middle value. If the set contains an even number of values, the median is the
mean, or average, of the two middle values. Write a function that accepts as arguments
the following:
A) An array of integers
B) An integer that indicates the number of elements in the array
The function should determine the median of the array. This value should be returned
as a double. (Assume the values in the array are already sorted.)
Demonstrate your pointer prowess by using pointer notation instead of array notation
in this function.
10. Reverse Array
Write a function that accepts an int array and the arrays size as arguments. The function should create a copy of the array, except that the element values should be reversed
in the copy. The function should return a pointer to the new array. Demonstrate the
function in a complete program.
11. Array Expander
Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the
unused elements of the second array with 0. The function should return a pointer to
the new array.
12. Element Shifter
Write a function that accepts an int array and the arrays size as arguments. The
function should create a new array that is one element larger than the argument array.
The first element of the new array should be set to 0. Element 0 of the argument array
should be copied to element 1 of the new array, element 1 of the argument array should
be copied to element 2 of the new array, and so forth. The function should return a
pointer to the new array.
13. Movie Statistics
Write a program that can be used to gather statistical data about the number of movies college students see in a month. The program should perform the following steps:
A) Ask the user how many students were surveyed. An array of integers with this
many elements should then be dynamically allocated.
B) Allow the user to enter the number of movies each student saw into the array.
C) Calculate and display the average, median, and mode of the values entered. (Use
the functions you wrote in Problems 8 and 9 to calculate the median and mode.)
Input Validation: Do not accept negative numbers for input.