Hw - 1 a The first bug is that there are no parentheses in*ptr 1 I fixed it to(ptr 1 The second bug is in to set ptr to 30 you have to use ptr

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1. a) The first bug is that there are no parentheses in *ptr + 1. I fixed it to *(ptr + 1) The second bug is in to set ptr to 30, you have to use * ptr instead of ptr[0]. The third bug is that the ptr—- comes before the next line, it’s supposed to come after. #include <iostream> using namespace std ; int main() { int arr[ 3 ] = { 5 , 10 , 15 }; int * ptr = arr; *ptr = 10 ; // set arr[0] to 10 *(ptr + 1) = 20 ; // set arr[1] to 20 ptr +=2 ; *ptr = 30 ; // set arr[2] to 30 while (ptr >= arr) { cout << *ptr << ' ' ; // print values ptr--; } cout << endl; } b) The error is that there is no ampersand when calling the pointer p. This needs to be passed by reference in order for it to work inside the main function. Correct code: 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 = NULL ; } c) It didn’t work because there was a pointer but no location to point to. I fixed it by making a double called hyp....
View Full Document

This note was uploaded on 11/10/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 5

Hw - 1 a The first bug is that there are no parentheses in*ptr 1 I fixed it to(ptr 1 The second bug is in to set ptr to 30 you have to use ptr

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online