$30
CS 31
Project 6
What's the Point?
This project is designed to help you master pointers. To that end, you'll get the most
out of it by working through the problems by hand. Only after that should you resort
to running the programs (and stepping through them with the debugger) to check your
understanding. Remember, on the final exam you'll have to be able to do problems
like this by hand.
This "project" is more like a homework. There are five problems. In problems that ask
you to change code, make the few changes necessary to fix the code without changing
its overall approach. For example, don't fix the program in problem 1a by changing it
to
int main()
{
cout << "30\n20\n10" << endl;
}
1. The subparts to this problem involve errors in the use of pointers.
a. This program is supposed to write 30 20 10, one per line. Find all of the
bugs and show a fixed version of the program:
b. int main()
c. {
d. int arr[3] = { 5, 10, 15 };
e. int* ptr = arr;
f.
g. *ptr = 30; // set arr[0] to 30
h. *ptr + 1 = 20; // set arr[1] to 20
i. ptr += 2;
j. ptr[0] = 10; // set arr[2] to 10
k.
l. while (ptr = arr)
m. {
n. ptr--;
o. cout << *ptr << endl; // print values
p. }
q. }
r. The findMax function is supposed to find the maximum item in an array
and set the pToMax parameter to point to that item so that the caller knows
that item's location. Explain why this function won't do that, and show a
way to fix it. Your fix must be to the function only; you must not change
the main routine below in any way, yet as a result of your fixing the
function, the main routine below must work correctly. s. void findMax(int arr[], int n, int* pToMax)
t. {
u. if (n <= 0)
v. return; // no items, no maximum!
w.
x. pToMax = arr;
y.
z. for (int i = 1; i < n; i++)
aa. {
bb. if (arr[i] *pToMax)
cc. pToMax = arr + i;
dd. }
ee. }
ff.
gg. int main()
hh. {
ii. int nums[4] = { 5, 3, 15, 6 };
jj. int* ptr;
kk.
ll. findMax(nums, 4, ptr);
mm. cout << "The maximum is at address " << ptr << endl;
nn. cout << "It's at position " << ptr - nums << endl;
oo. cout << "Its value is " << *ptr << endl;
pp. }
qq.The computeCube function is correct, but the main function has a problem.
Explain why it may not work and show a way to fix it. Your fix must be
to the main function only; you must not change computeCube in any way. rr. void computeCube(int n, int* ncubed)
ss. {
tt. *ncubed = n * n * n;
uu. }
vv.
ww. int main()
xx. {
yy. int* ptr;
zz. computeCube(5, ptr);
aaa. cout << "Five cubed is " << *ptr << endl;
bbb. }
ccc. The strequal function is supposed to return true if and only if its
two C string arguments have exactly same text. Explain what the
problems with the implementation of the function are, and show a way to
fix them.
ddd. // return true if two C strings are equal
eee. bool strequal(const char str1[], const char str2[])
fff. {
ggg. while (str1 != 0 && str2 != 0)
hhh. {
iii. if (str1 != str2) // compare corresponding
characters
jjj. return false;
kkk. str1++; // advance to the next
character
lll. str2++;
mmm. }
nnn. return str1 == str2; // both ended at same time?
ooo. }
ppp.
qqq. int main()
rrr. {
sss. char a[15] = "Noor";
ttt. char b[15] = "Noah";
uuu.
vvv. if (strequal(a,b))
www. cout << "They're the same person!\n";
xxx. }
yyy. This program is supposed to write 100 99 98 3 2 1, but it
probably does not. What is the program doing that is incorrect? (We're
not asking you explain why the incorrect action leads to the particular
outcome it does, and we're not asking you to propose a fix to the
problem.) zzz. #include <iostream
aaaa. using namespace std;
bbbb.
cccc. int* getPtrToArray(int& m)
dddd. {
eeee. int anArray[100];
ffff. for (int j = 0; j < 100; j++)
gggg. anArray[j] = 100-j;
hhhh. m = 100;
iiii. return anArray;
jjjj. }
kkkk.
llll. void f()
mmmm. {
nnnn. int junk[100];
oooo. for (int k = 0; k < 100; k++)
pppp. junk[k] = 123400000 + k;
qqqq. }
rrrr.
ssss. int main()
tttt. {
uuuu. int n;
vvvv. int* ptr = getPtrToArray(n);
wwww. f();
xxxx. for (int i = 0; i < 3; i++)
yyyy. cout << ptr[i] << ' ';
zzzz. for (int i = n-3; i < n; i++)
aaaaa. cout << ptr[i] << ' ';
bbbbb. cout << endl;
ccccc. }
2. For each of the following parts, write a single C++ statement that performs the
indicated task. For each part, assume that all previous statements have been
executed (e.g., when doing part e, assume the statements you wrote for parts a
through d have been executed).
a. Declare a pointer variable named cat that can point to a variable of type
double.
b. Declare mouse to be a 5-element array of doubles.
c. Make the cat variable point to the last element of mouse.
d. Make the double pointed to by cat equal to 42, using the * operator.
e. Without using the cat pointer, and without using square brackets, set the
fourth element (i.e., the one at position 3) of the mouse array to have the
value 25.
f. Move the cat pointer back by three doubles.
g. Using square brackets, but without using the name mouse, set the third
element (i.e., the one at position 2) of the mouse array to have the value
17.
h. Without using the * operator, but using square brackets, set the double
pointed to by cat to have the value 54.
i. Using the * operator in the initialization expression, declare a bool
variable named b and initialize it to true if the double pointed to bycat is
equal to the double immediately following the double pointed to by cat,
and false otherwise.
j. Using the == operator in the initialization expression, declare a bool
variable named d and initialize it to true if cat points to the double at the
start of the mouse array, and false otherwise.
3.
a. Rewrite the following function so that it returns the same result, but does
not increment the variable ptr. Your new program must not use any
square brackets, but must use an integer variable to visit each double in
the array. You may eliminate any unneeded variable.
b. double mean(const double* scores, int numScores)
c. {
d. const double* ptr = scores;
e. double tot = 0;
f. while (ptr != scores + numScores)
g. {
h. tot += *ptr;
i. ptr++;
j. }
k. return tot/numScores;
l. }
m. Rewrite the following function so that it does not use any square
brackets (not even in the parameter declarations) but does use the integer
variable k. Do not use any of the <cstring functions such
as strlen, strcpy, etc. n. // This function searches through str for the character chr.
o. // If the chr is found, it returns a pointer into str where
p. // the character was first found, otherwise nullptr (not
found).
q.
r. const char* findTheChar(const char str[], char chr)
s. {
t. for (int k = 0; str[k] != 0; k++)
u. if (str[k] == chr)
v. return &str[k];
w.
x. return nullptr;
y. }
z. Now rewrite the function shown in part b so that it uses neither square
brackets nor any integer variables. Your new function must not use any
local variables other than the parameters. Do not use any of
the <cstring functions such as strlen, strcpy, etc.
4. What does the following program print and why? Be sure to explain why each
line of output prints the way it does to get full credit.
5. #include <iostream
6. using namespace std;
7.
8. int* maxwell(int* a, int* b)
9. {
10. if (*a *b)
11. return a;
12. else
13. return b;
14. }
15.
16. void swap1(int* a, int* b)
17. {
18. int* temp = a;
19. a = b;
20. b = temp;
21. }
22.
23. void swap2(int* a, int* b)
24. {
25. int temp = *a;
26. *a = *b;
27. *b = temp;
28. }
29.
30. int main()
31. {
32. int array[6] = { 5, 3, 4, 17, 22, 19 };
33.
34. int* ptr = maxwell(array, &array[2]);
35. *ptr = -1;
36. ptr += 2;
37. ptr[1] = 9;
38. *(array+1) = 79;
39.
40. cout << &array[5] - ptr << endl;
41.
42. swap1(&array[0], &array[1]);
43. swap2(array, &array[2]);
44.
45. for (int i = 0; i < 6; i++)
46. cout << array[i] << endl;
47. }
48.Write a function named removeS that accepts one character pointer as a
parameter and returns no value. The parameter is a C string. This function must
remove all of the upper and lower case 's' letters from the string. The resulting
string must be a valid C string.
Your function must declare no more than one local variable in addition to the
parameter; that additional variable must be of a pointer type. Your function
must not use any square brackets and must not use any of
the <cstring functions such as strlen, strcpy, etc.
int main()
{
char msg[50] = "She'll be a massless princess.";
removeS(msg);
cout << msg; // prints he'll be a male prince.
}
Prepare your solutions to these homework problems as a single Word document
named hw.doc or hw.docx, or a plain text file named hw.txt. Put that file in a zip file,
and follow the turn in Project 6 link to turn in the zip file.