hw - 1 a The first bug is that there are no parentheses...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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. #include <iostream> #include <cmath> using namespace std ;
Image of page 1

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

View Full Document Right Arrow Icon
void hypotenuse( double leg1, double leg2, double * resultPtr) { *resultPtr = sqrt (leg1*leg1 + leg2*leg2); //changes the result pointer } int main() {
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern