15-122: Principles of Imperative Computation, Spring 2024 Written Homework 1 Due On
15-122: Principles of Imperative Computation, Spring 2024 Written Homework 1 Due On
Written Homework 1
Due on Gradescope: Monday 15th January, 2024 by 9pm
Name:
Andrew ID:
Section:
This written homework is the first of two homeworks that will introduce you to the way
we reason about C0 code in 15-122. It also makes sure that you have a good understand-
ing of key course policies.
Preparing your Submission You can prepare your submission with any PDF editor that
you like. Here are a few that prior-semester students recommended:
• Kami, Adobe Acrobat Online, or DocHub, some web-based PDF editors that work
from anywhere.
• Acrobat Pro, installed on all non-CS cluster machines, works on many platforms.
• iAnnotate works on any iOS and Android mobile device.
There are many more — use whatever works best for you. If you’d rather not edit a PDF,
you can always print this homework, write your answers neatly by hand, and scan it into
a PDF file — we don’t recommend this option, though.
Please do not add, remove or reorder pages.
Caution Recent versions of Preview on Mac are buggy: annotations get occasionally
deleted for no reason. Do not use Preview as a PDF editor.
Submitting your Work Once you are done, submit this assignment on Gradescope. Al-
ways check it was correctly uploaded. You have unlimited submissions.
If you haven’t yet enrolled in Gradescope for this class, please do so by completing the
setup lab (see Diderot or the course web page).
Question: 1 2 3 4 5 Total
Points: 2.5 2 3 3.5 4 15
Score:
15-122 Written Homework 1 Page 1 of 11
1. Policies
Read the collaboration policy on the course website. For every statement in each
scenario below, mark whether it is OK or not OK according to the collaboration
policy. Take each action to be independent from others.
0.5pts 1.1 Alex and Alice are on Zoom while completing a written homework.
not
OK OK
⃝ ⃝ Alex has already partially completed the homework and keeps it in
view on his computer while discussing it with Alice.
⃝ ⃝ Alice screenshares the blank homework writeup so they can both ref-
erence it.
⃝ ⃝ Alice asks Alex to check her work on Task 2 by reading her answer out
loud.
⃝ ⃝ Alex takes notes while brainstorming approaches and uses them to
complete the homework later.
⃝ ⃝ Alice draws a diagram on the whiteboard to explain this loop invariant
proof.
⃝ ⃝ Alex is working in a busy public area. Halfway through the home-
work, he leaves his computer unlocked to go to the bathroom.
0.5pts 1.2 Mihir and Sophia are studying for a midterm together.
not
OK OK
⃝ ⃝ Sophia screenshares the review slides so that the two of them can fig-
ure out a confusing concept from lecture.
⃝ ⃝ Mihir draws out an array on a shared whiteboard to better understand
a sorting problem in the practice midterm.
⃝ ⃝ Mihir asks a student from a previous iteration of the course for the
midterm from that semester so that he could use the exam as practice.
⃝ ⃝ Mihir asks Sophia how she did a question on a previous, already
graded written assignment.
⃝ ⃝ Sophia looks up code for a previous programming assignment on
GitHub to figure out how to complete a particularly challenging task.
⃝ ⃝ Mihir takes notes during their study session and uses them when cre-
ating his allowed exam notes.
1pt 1.3 Jackson asks Amanda for advice about the current programming homework.
not
OK OK
⃝ ⃝ Jackson forgets how to transfer files to AFS and Amanda tells him the
correct scp command.
⃝ ⃝ Amanda teaches Jackson some vim shortcuts.
⃝ ⃝ Jackson screenshares his code and asks Amanda to help him figure out
a bug.
⃝ ⃝ Amanda screenshares the blank homework writeup so they figure out
what Task 2 is asking them to do.
⃝ ⃝ Jackson writes out his pseudocode onto a shared whiteboard.
⃝ ⃝ Amanda says: “Just use two for loops, one for each array. You then
create a variable outside of both loops and just compare values to find
the overall maximum”.
⃝ ⃝ Amanda says: “The writeup said that we want to find the maximum
from these two arrays. If you’re stuck, I found it helpful to look at the
lecture notes on sorting.”
⃝ ⃝ Jackson asks Amanda to go over his code after both of them have made
their final submission, even though grades have not been released.
Amanda agrees and walks him through her answers.
⃝ ⃝ Jackson runs ChatGPT to implement task 2 and asks Amanda for help
fixing the resulting code.
The next 3 tasks are ungraded but will help you think about the consequences of
various academic integrity infringements. For each statement give your best guess at
the right answer. Make sure to come back to it and check the actual correct answers.
0pts 1.5 MOSS is the software service we use to check for plagiarized code.
1. Robbie is stuck on the last two tasks of the programming homework. Fara
sends him her code for them and he changes all variable names before sub-
mitting. Will MOSS detect this?
2. Robbie finds code for the current programming homework on GitHub, mod-
ifies it and then submits it. Will MOSS detect this?
3. Robbie asks Kevin, a student who completed the course three semesters
ago, for his code for the programming homework. Robbie makes cosmetic
changes and submits. Will MOSS detect this?
4. Robbie retakes the course and discovers that the next programming home-
work is the same as when he took it the first time around. He doesn’t look
at his old solution, but he is worried that his new code may be too similar to
his old code. Will MOSS detect a similarity?
2. Iliano is very busy this semester and decides to use ChatGPT in his 15-122
assignments to free up time. How are his changes of passing 15-122?
3. Anne made it through 15-122 in great part thanks to ChatGPT. How is she
likely to do in his next CS course?
⃝ nothing
⃝ he gets reported and nothing else
⃝ he gets reported and is given a symbolic zero on that assignment
⃝ he gets reported and his course grade is lowered by one letter grade
⃝ he gets suspended
3. Iliano was reported for an academic integrity violation in a history course
last semester and is being reported again this semester for copying code.
What will happen to him?
I have read the statements above and reviewed the course policy for cheating and plagiarism.
I agree to the clause in paragraph 6.
By signing below, I commit to abiding by these policies in this course.
Andrew ID
Signature Date
15-122 Written Homework 1 Page 6 of 11
2. Running C0 Programs
Assume we have the files num.c0 and num-test.c0. The file num.c0 contains a func-
tion num that takes an integer argument and returns an integer. The file num-test.c0
contains this main function (and nothing else):
int main() {
int x = num(15124);
return x;
}
How to run this program? Check out a relevant page in the C0 Tutorial at https:
//bitbucket.org/c0-lang/docs/wiki/Tutorial and answer the following ques-
tions.
1pt 2.1 From the command line, show how to display the value returned by num(15124)
using the C0 compiler.
1pt 2.2 From the command line, show how to display the value returned by num(15124)
using the C0 interpreter.
x= y= x=
x= y= x= y=
x= y= x= y=
a′ = , b′ = , and c′ =
a′ = , b′ = , and c′ =
a′ = , b′ = , and c′ =
a′ = , b′ = , and c′ =
Note that we always say “assuming (something) < n,” because if that were not
the case the loop wouldn’t run, and it wouldn’t make any sense to be talking
about the values of the primed variables.
a′ = , b′ = , and c′ =
a′ = b′ = , and c′ = ,
1.5pts 4.3 Consider this loop:
while (a > 0 && b > 0) {
if (a > b) {
a = a-b;
} else {
b = b-a;
}
}
a′ = and b′ =
a′ = and b′ =
a′ = and b′ =
• If a > 0 and b > 0, one of the two cases above will always be the case.
Therefore, we can conclude which of the following about the values stored
in a and b after an arbitrary iteration of the loop? (Check all that apply)
a′ ≥ 0 and b′ ≥ 0
a′ > 0 and b′ ≥ 0
a′ ≥ 0 and b′ > 0
a′ > 0 and b′ > 0
1pt 5.1 Complete the specification function below with the simple mathematical formula
in C0 that gives the square of the number v.
1 int SQUARE(int v)
2 //@requires 0 <= v && v < 2024;
3 {
4 return ;
5 }
10 //@ensures ;
11 {
12 int v = val;
13 int sum = 0;
14 while (v > 0)
15 //@loop_invariant 0 <= v;
16 //@loop_invariant v <= 2024;
17 // Additional loop invariant will go here
18 {
19 sum += 2*v - 1;
20 v--;
21 }
22 //@assert v == ;
23 return sum;
24 }
Note: in the real world we wouldn’t have an efficient closed-form solution used as a
specification function for an inefficient loop-based solution. We usually use the slow,
simple version as the specification function for the fast one!
0.5pts 5.2 Had we not introduced the local variable v on line 12 and used val instead,
compute_square would not compile. Explain why.
1.5pts 5.3 Using SQUARE everywhere possible, give a suitable extra invariant that would
allow us to prove the function correct. (Consider creating a table with values that
change during the loop.)
17 //@loop_invariant ;
To which line numbers would we point to support the assertion you completed
on line 22?
Plug the value you wrote on line 22 for v into your loop invariant for line 17 and
show that it simplifies to the postcondition on line 10 (if it doesn’t, you will want
to look for a different loop invariant). This proves that the loop invariants and the
negation of the loop guard imply the postcondition. Show your work.
1pt 5.4 Termination arguments for loops (in this class) have the following form:
During an arbitrary iteration of the loop, the expression ... gets strictly
larger / smaller , but this expression can’t get larger / smaller than ...
on which the loop guard is false.
Fill the blanks and circle either “larger” or “smaller” (in two places) to justify
that the loop in compute_square terminates.