Sunteți pe pagina 1din 3

Homework 1

CS 1323, Fall 2016


Name (5 points):
1. (5 points; 1 point each part) Declare a variable with a well-chosen
type and identifier for each of the data elements below.
a) The amount, in ounces, of suntan lotion that you applied to your
skin over the Labor Day weekend.
A: double numSuntanLotion
b) Whether or not you went swimming over the weekend.
A: Boolean wentSwim
c) The number of hot dogs eaten at a fair over the weekend.
A: int numHotDogs
d) The number of hours you slept on Sunday.
A: double numHoursSleep
e) The name of the best song that was played at a party on Labor
Day.
A: char bestSong
2. (12 points; 2 points each part) Perform the given operations on the
data below. Pay careful attention to whether the result is an int (like
3) or a double (like 3.0). It is best to show individual operations and
promotions to get partial credit.
a. 3 * 8
A: 24
b. 53 % 8
A: 5
c. 15 / 6
A: 2
d. 15.0 / 6
A: 2.5
e. 4 * 9 / 5 1.0
A: 6.2
f. 4 * 9.0 / 5 1

A: 36.0/5-1 = 7.2-1 = 6.2


3. (8 points) The code below is trying to create a right rotation of
data (so (1, 2, 3) would become (3, 1, 2) for example), but is not
correct. Draw a memory diagram to trace the execution of the
code below to show what is going wrong. You do not need to fix
the code.
int first = 6;
int second = 1;
int third = 2;
int temp = first;
second = first;
third = second;
first = temp;
Identifier
first
second
third
temp
second
third

Address
100
101
102
103
101
102

Contents
6
1
2
6
6
6

4. (20 points; 2 points each) What does the variable number contain
at the end of each of these computations? If the code is not legal,
say so. If you encounter a decimal number with many places, you
may show only 3 (even though this isnt really what the computer
does). Show your work to receive partial credit.
You can check that you've done these correctly by writing a little
computer program, but make sure that you do them by hand first to
understand the critical ideas of precedence and promotion. There will
be questions like this on the first midterm, and you won't have a
computer or calculator available (on an exam, Ill make sure the
numbers are easy to manipulate).
int iCount = 10;
double dCount = 4.9;
int iSize = 3;
double dSize = 9.2;
a. int number = iCount + iSize;
A: int number = 10 + 3 = 13

b. int number = (int) dCount + iCount;


A: int number = 4 + 10 = 14
c. double number = (int) dSize - dCount;
A: double number = 9 4.9 = 4.1
d. int number = iCount % iSize;
A: int number = 10%3 = 1
e. double number = iCount / iSize;
A: double number = 10 / 3 = 3.333333 3.0
f. int number = iSize * iSize * iSize / iCount % iSize;
A: int number = 3*3*3/10%3 = 2
g. int number = dSize + iCount - dCount + -dSize;
A: not legal
h. double number = iSize - iCount + iSize / dCount;
A: double number = 3 10 + 3/4.9 = -6.387755102 -6.388
i. double number = ((iCount - iSize) + iCount) - (dCount *
iSize)/dSize;
A: double number = ((10 3) + 10) (4.9*3)/9.2 = (7+10)
14.7/9.2 = 17 1.597826087 = 15.40217391 15.402
j. int number = iCount + iSize / (int) dCount;
A: int number = 10 + 3 / 4 = 10

S-ar putea să vă placă și