Sei sulla pagina 1di 4

Olivia Leu

ID: 904304022
5/24/16
Homework 6
1.a.
Bugs:
*ptr + 1 = 20; // The asterisk doesnt dereference ptr+1, just dereferences ptr and cant
// initialize the sum of the dereferenced pointer and 1 to 20.
ptr--; // Decrements ptr before printing it out, which isnt what we want.
Fixed version of the program:
int main()
{
int arr[3] = { 5, 10, 15 };
int* ptr = arr;
*ptr = 10;
*(ptr + 1) = 20;
ptr += 2;
ptr[0] = 30;
while (ptr >= arr)
{
cout << << *ptr;
ptr--;
}
cout << endl;
}
1.b.
This function wont work the way we want it to because ptr is uninitialized when it is
used as a parameter in the findDisorder function. Since pointers are not passed in by reference,
the computer makes a copy of the pointer to use in the function, which causes an error since ptr
is uninitialized.
Fixed findDisorder function:
void findDisorder(int arr[], int n, int*& p)
{
for (int k = 1; k < n; k++)
{
if (arr[k] < arr[k-1])
{
p = arr + k;
return;

}
}
p = nullptr;
}
1.c.
The program doesnt work the way we want it to because again the pointer p is not
initialized, so it causes an error when the hypotenuse function tries to create a copy of p to use in
its function.
Fixed main function:
int main()
{
double p;
hypotenuse(1.5, 2.0, &p);
cout << The hypotenuse is << p << endl;
}
1.d.
The match function has a problem in its implementation because str1 and str2 refer to the
pointers and the addresses they are pointing to, and they point to different arrays in memory so
str1 will not equal str2. So the function will return false even when the string arguments have
exactly the same text. The return statement should also be an if statement regarding str1 == str2.
Fixed match function:
bool match(const char str1[], const char str2[])
{
while (*str1 != 0 && *str2 != 0)
{
if (*str1 != *str2)
return false;
str1++;
str2++;
}
if (str1 == str2) return true;
}
1.e.
The computeSquares function returns a pointer to an array that is created within the
function so the array only has local scope, which can lead to undefined behavior later on when
you refer to that array using the returned pointer in main.
2.
a.
string* fp;
b.
string fish[5];
c.
fp = &fish[4];
d.
*fp = yellowtail;
e.
*(fish+3) = salmon;
f.
fp -= 3;

g.
h.
i.
j.

fp[1] = perch;
fp[0] = eel;
bool d = (fp == fish);
bool b = (*fp == *(fp+1));

3.a.
double computeAverage(const double* scores, int nScores)
{
const double* ptr = scores;
double tot = 0;
for (int i = 0; i < nScores; i++)
{
tot+=*(ptr + i);
}
return tot/nScores;
}
3.b.
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;
}
3.c.
char* findTheChar(char* str, char chr)
{
while (*str != 0)
{
if (*str == chr)
return str;
str++;
}
return nullptr;
}
4.
Output:
diff=1 // ptr is initialized to &array[2] since *array > *(array[2])
4
// ptr[1] = 9 so array[3] is set to 9
79
// ptr is then incremented by 2, so now points to array[4]
5
// *ptr = -1 so array[4] is set to -1
9
// *(array+1) = 79 so array[1] is set to 79
-1
// So &array[5] ptr is &array[5] - &array[4] which is equal to 1
19
// So diff=1 is output to console

// array is now {5, 79, 4, 9, -1, 19}


// &array[0] and &array[1] are swapped so in memory, array[0] is stored where
// array[1] was (their addresses were swapped, but array[0] and array[1] still point
// to the same elements as before)
// array and &array[2] are swapped (array now points to address of array[2])
// so array points to 3rd element and array[2] points to 1st element
// for loop then prints out array[0],,array[5]
// So it prints out 3rd, 2nd, 1st, 4th, then 5th element
5.
void deleteG (char* ptr)
{
char* checkptr = ptr;
while (*ptr != 0)
{
if (*ptr == G || *ptr == g)
{
for (checkptr = ptr + 1; *checkptr != 0; checkptr++)
*(checkptr 1) = *checkptr;
*(checkptr 1) = 0;
}
ptr++;
}
}

Potrebbero piacerti anche