{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CSE250 HW1 Solution

CSE250 HW1 Solution - CSE 250 Spring 2011 Homework 1 Due...

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

CSE 250 Spring 2011 Homework 1 Due Date: Feb 14, Monday, by 2:05pm Total Points: 24 1. (3 points) Trace the following program segments: double x = 3.1416; double y =2.71828; double z; double* px = &x; double& ry=y; *px = 6.28; z = ry; px = &z; ry = *px; cout << "x= " << x << endl; cout << "z= " << z << endl; cout << "y= " << y << endl; cout << "ry= " << ry << endl; cout << "*px= " << *px << endl; What will be the output? (You may run the program to see the output. But make sure you understand why.) Solution. x= 6.28 z= 2.71828 y= 2.71828 ry= 2.71828 *px= 2.71828 x is originally equal to 3.1416, but the pointer px is assigned to the address of x , and when * px (the thing px points to) is set to 6.28, this changes the value at the address of x . The reference variable ry is set to refer to y which holds the value of 2.71828, so ry also equals 2.71828. Then z is set equal to the variable that ry refers to (i.e. z = y , so z = 2.71828 now). The pointer px is then set to point to the address of z . The next line then sets the variable refered to by ry to be equal to the variable pointed to by px (i.e. y = z ). Since y is already equal to 2.71828, its value (and that of its reference variable ry ) does not change. 2. (2 points) Consider the following program segment: { int temp = a; a=b; b=temp; } int main() { int x=-5, y=5; 1

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

View Full Document
swap (x,y); cout << "x = " << x << " y = " << y << endl;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 4

CSE250 HW1 Solution - CSE 250 Spring 2011 Homework 1 Due...

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

View Full Document
Ask a homework question - tutors are online