﻿

Homework 6 (due 5/21) CSC 241

We talked about exceptions (Section 4.4) and started discussing programming patterns more systematically, including various uses of loops (Section 5.1), nested loops (Section 5.2). We saw how nested loops can be used in processing image data (appendices C4 and C5 in the electronic edition of the book). We will see some more examples of nested loops next week (Section 5.3) and then move on to the while loop (Section 5.4-5.6), and some new data structures, including dictionaries (Chapter 6).

In class I mentioned:

Submission: The homework is due by midnight (I will not accept late homeworks). You can submit your homework through d2l into the drop-box for this homework. Please prepare your homework as a single file containing all answers (e.g. doc, docx, or pdf, not a zip file).

When submitting programs, include the programs, together with screenshots of test-runs of your program (make sure screenshots are sized so the text is legible; resize and/or crop the images).

1. (Reading Assignment) Read Sections 4.4, 5.1, 5.2, 5.3, and C.4, C.5 of the textbook.

2. (Multipliation Table, 10pt) Write a function mult(m,n) that prints an mxn multiplication table, that is, there should be m rows and n columns, with the entry in cell (i,j) being i*j, where i ranges from 1 to m, and j from 1 to n. You can assume that 3 spaces are enough to display each number (you can work with '{:3d}'.format(x) to display number x so it uses 3 spaces). In the following output I also added a header row/initial column, and separating lines. You can ignore that, but if you have time you can implement the headers and lines for extra credit (+5pt EC). 3. (Nested Loops, 15pt) a) [7pt] Write a program A(n) that produces the letter in an nxn square grid as ASCII art (black pixels are asterisks, white pixels are blanks). Test with both even and odd n (at least one of them will look a bit strange depending on whether you work with n//2 or (n-1)//2 for the middle bar; either one is fine). b) [8pt] Write a program R(n) that produces the letter in an nxn square grid as ASCII art (black pixels are asterisks, white pixels are blanks). Test with both even and odd n (at least one of them will look a bit strange depending on whether you work with n//2 or (n-1)//2 for the middle bar; either one is fine). Note: if you have trouble with the diagonal part, you can hand in P(n) for partial credit (that's R without the diagonal part). 4. (Thumbnailing, 15pt). Implement a function half(name) which takes a picture with name name, and halves the picture in size (so half the width and half the height). Working with the Meidner.jpg picture we saw in class, I applied the half function twice to get half_Meidner.jpg and half_half_Meidner.jpg.  Hint: think of creating the half-sized picture pixel by pixel. In the half-size picture, where does the pixel (x,y) come from in the original picture?

Marcus Schaefer
Last updated: May 15th, 2019.