problem set 1

EN47/COMP9, Fall 2009

Out: October 15, 3:00pm
Due: October 22, 3:00pm

In this assignment, you will apply your knowledge of many computer science and programming concepts we have covered over the past weeks, including algorithm design, debugging, functions, conditionals, and iteration. While you may choose to test any code you write using g++, you should hand in your answers on paper at the start of class on October 22.

A. Problems, Algorithms, and Programs

  1. Algorithm design (7 pts). You are selling your rare coin collection on eBay to a number of customers. To estimate your shipping costs, you want to write a program to compute the combined weight of any number of coins.

    1. The output of your program will be the weight of a batch of coins. One piece of data you will need for each coin is the mass density of the material (e.g. the density of gold is 19.3 g/cc). What other data do you need?
    2. Sketch an algorithm for solving this problem. Write your algorithm in English, not C++!
  2. Programming terminology (3 pts).
    1. What is the difference between a statement and a comment?
    2. What is the difference between syntax and semantics?
    3. What is the difference between syntax errors and semantic errors? Give one example of each in C++.

B. Functions

  1. Tracing code (8 pts). It is a useful skill to be able to read someone else's code and understand what it does without actually compiling and running it. The point of this exercise is to practice reading code and to make sure that you understand the flow of execution through a program with multiple functions. Consider the program below:

         #include <iostream>
         using namespace std; 
    
         void ping () {
             cout << "." << endl;
         }
    
         void baffle () {
             cout << "wug";
             ping();
         }
    
         void zoop () {
             baffle();
             cout << "You wugga ";
             baffle();
         }
    
         int main () {
             cout << "No, I ";
             zoop();
             cout << "I ";
             baffle();
             return 0;
         }
    
    1. What is the output of this program? Be precise about where there are spaces and where there are newlines. Hint: Start by describing in words what ping and baffle do when they are called.
    2. Draw a call graph (flowchart) that shows the order in which functions are called.
    3. Suppose you wanted to define ping, baffle, and zoop elsewhere in the file, after the definition of main, for example. What changes would you need to make to the program for this to compile?
  2. Invoking functions and passing arguments (2 pts).
    1. Write the function prototype for a function named zoo that takes three parameters: an int and two strings.
    2. Write one line of code that invokes (calls) zoo, passing as arguments the age of your pet, the name of your pet, and your street address.

C. Conditionals and Iteration

  1. Triangles (4 pts). If you are given three sticks, you may or may not be able to arrange them in a triangle. For any three lengths, there is a simple geometric test to see if it is possible to form a triangle:
    "If any of the three lengths is greater than the sum of the other two, then you cannot form a triangle.
    Otherwise you can."
    Write a function called isTriangle that takes three integers as parameters and returns 1 (true) or 0 (false), depending on whether you can form a triangle from sticks of the given lengths.
  2. Buggy loops (6 pts). Consider the following program:

         #include <iostream>
         using namespace std; 
    
         void loop (int n) {
    	 int i = n;
    	 while (i > 0) {
    	     cout << i << endl;
    	     if (i%2 == 0) {
    		 i = i/2;
    	     }
    	     else {
    		 i = i + 1;
    	     }
    	 }
         }
    
         int main () {
    	 loop (10);
    	 return 0;
         }
    
    1. Draw a table that shows the value of the variables n and i during the execution of the loop function. Your table should contain two columns (one for each variable) and one row for each iteration. For each row in the table, write down the values of the variables as they would be at the line containing the cout statement.
    2. What is problematic about this program? Suggest one way to improve its behavior.
Updated October 14, 2009 by sarasu