What’s the Point? Solution

$35.00

Description

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.

    1. 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:

    1. int main()

    2. {

    1. int arr[3] = { 5, 10, 15 };

    2. int* ptr = arr;

f.

*ptr

=

30;

//

set

arr[0]

to

30

g.

20;

h.

*ptr

+

1 =

//

set

arr[1]

to

20

  1. ptr += 2;

j.

ptr[0] = 10;

// set arr[2] to 10

k.

  1. while (ptr >= arr)

  2. {

n.

ptr–;

// print values

o.

cout << *ptr << endl;

  1. }

  2. }

  1. 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.

  1. void findMax(int arr[], int n, int* pToMax)

  1. {

  2. if (n <= 0)

v.

return;

// no items, no maximum!

w.

  1. pToMax = arr;

y.

  1. for (int i = 1; i < n; i++)

  1. {

bb.

cc.

if (arr[i] > *pToMax)

pToMax = arr + i;

  1. }

  2. }

ff.

  1. int main()

  2. {

  1. int nums[4] = { 5, 3, 15, 6 };

  2. int* ptr;

kk.

  1. findMax(nums, 4, ptr);

  2. cout << “The maximum is at address ” << ptr << endl;

  1. cout << “It’s at position ” << ptr – nums << endl;

  2. cout << “Its value is ” << *ptr << endl;

  3. }

  1. 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.

int main()

ww.

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.

  1. // return true if two C strings are equal

  2. bool strequal(const char str1[], const char str2[])

  1. {

  2. while (str1 != 0 && str2 != 0)

  3. {

iii.

if (str1 != str2)

// compare corresponding

characters

return false;

jjj.

kkk.

str1++;

// advance to the next

character

str2++;

lll.

  1. }

nnn. return str1 == str2; // both ended at same time?

  1. }

ppp.

  1. int main()

  1. {

  2. char a[15] = “Noor”;

  3. char b[15] = “Noah”;

  1. if (strequal(a,b))

www. cout << “They’re the same person!\n”;

  1. }

  1. 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.)

  1. #include <iostream>

  1. using namespace std;

bbbb.

  1. int* getPtrToArray(int& m)

  1. {

  2. int anArray[100];

  3. for (int j = 0; j < 100; j++)

gggg. anArray[j] = 100-j;

  1. m = 100;

  1. return anArray;

  2. }

kkkk.

  1. void f()

  2. {

  1. int junk[100];

  2. for (int k = 0; k < 100; k++)

pppp. junk[k] = 123400000 + k;

  1. }

rrrr.

  1. int main()

  2. {

  3. int n;

  4. int* ptr = getPtrToArray(n);

  5. f();

  1. for (int i = 0; i < 3; i++)

yyyy. cout << ptr[i] << ‘ ‘;

  1. for (int i = n-3; i < n; i++)

  1. cout << ptr[i] << ‘ ‘;

  2. cout << endl;

  1. }

  1. 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).

  1. Declare a pointer variable named cat that can point to a variable of type double.

  1. Declare mouse to be a 5-element array of doubles.

  1. Make the cat variable point to the last element of mouse.

  1. Make the double pointed to by cat equal to 42, using the * operator.

  1. 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.

  1. Move the cat pointer back by three doubles.

  1. 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.

  1. Without using the * operator, but using square brackets, set the double pointed to by cat to have the value 54.

  1. 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.

  1. 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.

  1. 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.

  1. double mean(const double* scores, int numScores)

  1. {

  2. const double* ptr = scores;

  3. double tot = 0;

  4. while (ptr != scores + numScores)

  5. {

h.

i.

tot += *ptr;

ptr++;

  1. }

  2. return tot/numScores;

  3. }

  1. 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.

  1. // This function searches through str for the character chr.

  2. // If the chr is found, it returns a pointer into str where

  1. // the character was first found, otherwise nullptr (not found).

q.

  1. const char* findTheChar(const char str[], char chr)

  2. {

  3. for (int k = 0; str[k] != 0; k++)

u. if (str[k] == chr)

v. return &str[k];

w.

  1. return nullptr;

  2. }

  1. 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.

  1. 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.

  1. #include <iostream>

  2. using namespace std;

7.

  1. int* maxwell(int* a, int* b)

  2. {

  1. if (*a > *b)

11. return a;

  1. else

13. return b;

  1. }

  1. void swap1(int* a, int* b)

  2. {

  3. int* temp = a;

  4. a = b;

  1. b = temp;

  2. }

22.

  1. void swap2(int* a, int* b)

  2. {

  1. int temp = *a;

  2. *a = *b;

  3. *b = temp;

  4. }

29.

  1. int main()

  2. {

  3. int array[6] = { 5, 3, 4, 17, 22, 19 };

  1. int* ptr = maxwell(array, &array[2]);

  1. *ptr = -1;

  2. ptr += 2;

  3. ptr[1] = 9;

  4. *(array+1) = 79;

39.

  1. cout << &array[5] – ptr << endl;

  1. swap1(&array[0], &array[1]);

  1. swap2(array, &array[2]);

44.

  1. for (int i = 0; i < 6; i++)

46. cout << array[i] << endl;

  1. }

  1. 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.