CSE250 HW1 Solution

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
swap (x,y); cout << "x = " << x << " y = " << y << endl;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 12/11/2011.

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 Right Arrow Icon
Ask a homework question - tutors are online