You are on page 1of 3

1a)

int main()
{
int arr[3] = { 5, 10, 15 };
int* ptr = arr;
*ptr = 30;
// set arr[0] to 30
// should increment pointer, not what the pointer is pointing
// to, need parenthesis
*(ptr + 1) = 20;
// set arr[1] to 20
ptr += 2;
// ptr is not an array; must dereference pointer using *
*ptr = 10;
// set arr[2] to 10
// wouldve cout arr[2], arr[1], arr[0] which is
// the opposite of what is desired
while (ptr >= arr)
{
ptr--;
cout << *(arr +(arr-(ptr-1))) << endl;
// print values
}
}
b) The findMax function never changes the pointer ptr since ptr is passed to
the function by value, not reference. As a result, ptr is an uninitiated ptr.
To fix this, simply change the function prototype to
void findMax(int arr[], int n, int*& pToMax)
c) the main may not work because you are passing a pointer to a pointer,
changing the pointer to 5^3, and then print out the value that is at address
5^3. To fix this, change ptr to an int and pass the address of the ptr to the
function, and cout ptr.
int main()
{
int ptr;
computeCube(5, &ptr);
cout << "Five cubed is " << ptr << endl;
}
d) str1 and str2 are pointers to the 0 position of the array (starting points
of arrays). Therefore, this function should dereference both pointers before
comparing them.
// return true if two C strings are equal
bool strequal(const char str1[], const char str2[])
{
while (*str1 != 0 && *str2 != 0)
{
if (*str1 != *str2) // compare corresponding characters
return false;
str1++;
// advance to the next character
str2++;
}
return *str1 == *str2;
// both ended at same time?
}

e) The problem with this program is that the array that ptr points to only
exists in the getPtrToArray function. After the function ends, the memory is
not held for that array anymore even though the pointer is pointing to the
first element of what was previously the array, which is now unallocated
memory.
2 a)
b)
c)
d)
e)
f)
g)
h)
i)
j)

double* cat;
double mouse[5];
cat = mouse + 4;
*cat =42;
*(mouse + 3) = 17;
cat -= 3;
cat[1] = 25;
cat[0] = 54;
bool b = (*cat == *(cat + 1));
bool d = ( cat == mouse);

3a)
double mean(const double* scores, int numScores)
{
const double* ptr = scores;
double tot = 0;
int count = 0;
while ((ptr + count) != scores + numScores)
{
tot += *(ptr + count);
count++;
}
return tot/numScores;
}
3b)
// This function searches through str for the character chr.
// If the chr is found, it returns a pointer into str where
// the character was first found, otherwise nullptr (not found).
const char* findTheChar(const char* str, char chr)
{
for (int k = 0; *(str + k) != 0; k++)
if (*(str + k) == chr)
return (str + k);
return nullptr;
}
3c)
// This function searches through str for the character chr.
// If the chr is found, it returns a pointer into str where
// the character was first found, otherwise nullptr (not found).
const char* findTheChar(const char* str, char chr)
{
for (str ; *str != 0; str++)
if (*str == chr)
return str;
return nullptr;
}

4)

int main()
{
int array[6] = { 5, 3, 4, 17, 22, 19 };
// compares 4 to 5, returns array[0] position
int* ptr = maxwell(array, &array[2]);
// array [0] = -1
*ptr = -1;
// ptr now points to array[2]
ptr += 2;
// array[3] = 9;
ptr[1] = 9;
// array[1] = 79;
*(array+1) = 79;
//prints out &array[5] - &array[3] = 5-2 = 3
cout << &array[5] - ptr << endl;
// array [0], array[1] stay the same, only pointers swap
swap1(&array[0], &array[1]);
// array[0] = 4, array[2] = -1
swap2(array, &array[2]);

//prints out (new line after each one) array[0] = 4, array[1]= 79,
array[2] = -1, array[3] = 9, array[4] = 22, array[5] = 19
for (int i = 0; i < 6; i++)
cout << array[i] << endl;
}
3
4
79
-1
9
22
19

Program prints out


(cout #1)
(cout #2)
(cout #2)
(cout #2)
(cout #2)
(cout #2)
(cout #2)

5)
void removeS (char* ptr)
{
char* tmpptr = ptr;
for (ptr; *ptr != \0; ptr++)
{
if (((*ptr) == s) || ((*ptr) == S))
continue;
*tmpptr = *ptr;
tmpptr++;
}
*tmpptr = \0;
}

You might also like