CSC161 2011S Imperative Problem Solving

Exam 1: Linux and C Basics

Assigned: Wednesday, 9 February 2011
Due: 11 p.m., Wednesday, 16 February 2011

Preliminaries

Exam Format

This is a take-home examination. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date.

There are four problems on this examination. You must answer all of them. The problems are not necessarily of equal difficulty. Each problem may include subproblems. Four correct or mostly-correct solutions will earn you an A. Three correct or mostly-correct solutions will earn you a B. Two correct or mostly-correct solutions will earn you a C. One correct or mostly-correct solution will earn you a D. Zero correct or mostly-correct solutions will earn you an F. Failure to attempt the exam will earn you a 0. Partially-correct solutions may or may not earn you a partial grade, at the discretion of the grader.

Read the entire exam before you begin.

I expect that someone who has mastered the material and works at a moderate rate should have little trouble completing the exam in a reasonable amount of time. In particular, this exam is likely to take you about five hours, depending on how well you've learned the topics and how fast you work. You should not work more than eight hours on this exam. Eight hours of work, documented attempts on all problems, and a signed statement that There's more to life than computer science will earn you at least a C on this exam.

I would also appreciate it if you would write down the amount of time each problem takes. Doing so may earn you a modicum of extra credit.

Academic Honesty

This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than as restricted by that limitation, you should feel free to use all reasonable resources available to you.

As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately. If you use code that you wrote for a previous lab or homework, cite that lab or homework and the other members of your group. If you use code that you found on the course Web site, be sure to cite that code. You need not cite the code provided in the body of the examination.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web. And, in case it's not clear, you may not ask others (in person, via email, via IM, by posting a please help message, or in any other way) to put answers on the Web.

Because different students may be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say This is among the hardest exams I have ever taken. If you don't start it early, you will have no chance of finishing the exam. You may also summarize these policies. You may not tell other students which problems you've finished. You may not tell other students how long you've spent on the exam.

You must include both of the following statements on the cover sheet of the examination.

  1. I have neither received nor given inappropriate assistance on this examination.
  2. I am not aware of any other students who have given or received inappropriate assistance on this examination.

Please sign and date each statement. Note that the statements must be true; if you are unable to sign either statement, please talk to me at your earliest convenience. You need not reveal the particulars of the dishonesty, simply that it happened. Note also that inappropriate assistance is assistance from (or to) anyone other than Professor Rebelsky (that's me).

Presenting Your Work

You must present your exam to me in two forms: both physically and electronically. That is, you must write all of your answers using the computer, print them out, number the pages, put your name on the top of every page, and hand me the printed copy. You must also submit an electronic copy of your exam. You should create the electronic version by making a tarball of any relevant code and submitting that tarball on Pioneerweb. Failure to name and number the printed pages will lead to a penalty. Failure to turn in both versions may lead to a much worse penalty. Failure to turn in a legible version of the exam is also likely to lead to a penalty.

If your answer relies on provided files but you have not modified the files, you should include them in the tarball but should not include them in the printed copy of the exam.

In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code. You should do your best to format that code to GNU coding standards.

Unless I explicitly tell you to document a procedure, you need not document that procedure. If I do tell you to document a procedure, you should document it with six-P style documentation. If I do not tell you to document a procedure, you may still document that procedure and may use whatever form you find most appropriate.

Just as you should be careful and precise when you write code and documentation, so should you be careful and precise when you write prose. Please check your spelling and grammar. Since I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit to five points.

I may give partial credit for partially correct answers. I am best able to give such partial credit if you include a clear set of work that shows how you derived your answer. You ensure the best possible grade for yourself by clearly indicating what part of your answer is work and what part is your final answer.

Getting Help

I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445).

I will also reserve time at the start of each class before the exam is due to discuss any general questions you have on the exam.

Problems

Problem 01: Selecting Students

Topics: Linux commands, Pipes

Suppose Professor L. stores information for his classes in the following form:

FirstName:LastName:Assignment:Grade:Notes

For example,

Jane:Smith:HW1:95
John:Doe:HW1:90
Joe:Smythe:HW2:100
Joe:Smythe:HW1:105
Jane:Smith:HW2:80
Jen:Doe:HW1:85
Jen:Doe:HW2:100
Jess:Smith:HW1:60
Jess:Smith:HW2:85
Jack:Smith:HW1:50
Jack:Smith:HW2:85
Jerry:Jones:HW1:80
Jo:Brown:HW1:84
Jeff:Doe:HW1:86
Jeff:Smith:HW1:87
Jeff:Jacobs:HW1:82
Jan:Jacobs:HW1:91

Write a Linux command (a pipe between various programs) that prints out the records for the five highest grades on HW1, sorted alphabetically by name (e.g., John Doe comes before Jane Smith but after Jen Doe).

You can assume that no two students have the same grade on the assignment.

Problem 02: Counting Numbers

Topics: Input processing, Numbers, Histograms, Loops

Write a C program, numbers, that extracts the numbers from input and prints them out. For example, if the input was

The 411 on 3.1415 is
that 11x11 is 121.

then the output should be

411
3.1415
11
11
121

Note: You should use getchar to read values, and not scanf.

Problem 03: Simple Coding

Topics: Strings, Characters, Output, Arithmetic Operations

In our initial explorations of output, we built a simple coding program by adding 1 to each character. Let's develop a more complex version of that program. Instead of using a single number, we'll use a code word. To encode an input, you add characters from the code word. E.g.,

   This is the text to be encoded
 + WordWordWordWordWordWordWordWo
 --------------------------------

So, the first letter of output will be the result of adding 'T' and 'W'. The second will be the result of adding 'h' and 'o'. The third will be the result of adding 'i' and 'r'. And so on and so forth.

Of course, this strategy is a bit harder in C, since we need to deal with the limitations of ASCII representation. In particular, only characters with values between 32 (space) and 126 (tilde) are printable. So, one has to play a bit with the computation using modulo and other operations.

a. Write a C program, encode, that encodes its input using a hard-coded code word. (You can use the code word of your choice.)

b. Write a C program, decode, that decodes its input using the same hard-coded code word.

Problem 04: Drawing Diamonds

Topics: Output, Loops, Error Checking

Write a concise C program, diamond, that reads in an integer and prints out a diamond made out of asterisks, such as the following.

   *
  ***
 *****
*******
 *****
  ***
   *

The input integer represents the size of the edges. For example, the diamond given above has sides of size 4.

Your program should generate an appropriate error message if the user does not enter a valid side length.

Questions

Here you will find answers to questions of general interest. Please check here before emailing me your questions!

Problem 1

Any hints? [2011.02.12]
You'll want to sort multiple times.

Problem 2

Do we have to support signs at the front of numbers, like +3 or -235? [2011.02.09]
No.
Do we have to handle scientific notation, like 3e4? [2011.02.09]
No.
Do we have to handle both integers and real numbers? [2011.02.09]
Yes.
Can we assume that everything in the text read by the function is either a proper word (all letters, no numbers) or a proper number (all numbers, except for possibly a single decimal point)? [2011.02.10]
No. You could have inputs like foo123bar or 123a45 or 123.4elephants or even the PITN My favorite number is 32. What's yours?
Can we print out extra blank lines between numbers? [2011.02.10]
No.
Do you have any hints? [2011.02.10]
Look at the word count program.
Do we have to handle fractions like 5/4? [2011.02.12]
No. You can treat those as two separate integers separated by a slash.
Can we print the decimal point in My favorite number is 32.? [2011.02.14]
No.
Is atoi a good way to handle this problem? [2011.02.14]
It's not how I'd handle it. You don't actually need to figure out what the numbers are, you just need to figure out whether or not the current character is in a number.
Why did you mention histograms in the topic? [2011.02.14]
Because I think of the problem as being based on your histograms problem.

Problem 3

Can you give me a hint? [2011.02.12]
You shold store the keyword and its length in variables. You can keep a counter variable (e.g., i) to keep track of where you are in the keyword, incrementing and moding it at each step.
In problem 3, do we need to add the characters or can we do other operations with them, like substracting? If all of my classmates used the same encoding word, would their codes all produce the same results, or are there many different answers? [2011.02.16]
In an ideal world, everyone would produce the same encoding. However, there are a few different design decisions that can be made about order of operatios. So, I'd expect a few different answers (maybe 3), but not 30.

Problem 4

Can we use functions or macros? [2011.02.09]
Functions yes. Macros no.
Is it okay if when the user enters a real number, we ignore the part after the decimal? E.g., if the user enters 4.1, can we just read that as 4? [2011.02.10]
Yes, it's fine to ignore the decimal point and part after.
What should we do if the user enters a non-digit character? [2011.02.10]
You should print an error message.

Miscellaneous

Can we get extra credit for errors in the Q&A section or the errata section?
Certainly not.
What does PITN stand for? [2011.02.10]
Pain in the neck.
So when you call someone a pocket bread, you're really calling them a ...? [2011.02.10]
Yes.
Was that supposed to be funny? [2011.02.10]
Yes.
When testing code, do you want us to use a script to show it works? [2011.02.10]
Yes.
Can we use libraries other than stdio.h? [2011.02.13]
Certainly.
The P'web drop box did not seem to be available on Tuesday night. [2011.02.16]
Sorry about that. It's available now.

Errata

Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to a point of extra credit for everyone. We usually limit such extra credit to five points. However, if I make an astoundingly large number of errors, then I will provide more extra credit.

 

History

Tuesday, 8 February 2011 [Samuel A. Rebelsky]

  • First draft.
  • General policies taken from past exams.

Wednesday, 9 February 2011 [Samuel A. Rebelsky]

  • Released.
  • Added first set of questions and answers.

Thursday, 10 February 2011 [Samuel A. Rebelsky]

  • More questions and answers.
  • Added dates to questions and answers for clarity.
  • Added first correction.

Saturday, 12 February 2011 [Samuel A. Rebelsky]

  • Another correction.
  • More questions and answers.

Sunday, 13 February 2011 [Samuel A. Rebelsky]

  • Another correction.
  • Minor reformatting of some Q&A.
  • More questions.

Monday, 14 February 2011 [Samuel A. Rebelsky]

Wednesday, 16 February 2011 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Sun Feb 20 13:46:43 2011.
The source to the document was last modified on Sun Feb 20 13:40:33 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Exams/exam.01.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu