Assignment #1

Due date: 10AM one week after the assignment was handed out

Recommended Reading: Chapters 1, 2, 3.7, 4.1-4.4,4.6 [IACU]


Purpose

There are two main purposes for this assignment:
  1. to introduce the tools needed to program in C, getting used to the C compiler and debugger.
  2. to introduce the basic C programming language constructs:
  3. Try to keep your files organized. For example create a directory for this class in your homedirectory, create a subdirectory for assignment 1.

Programming (90 points)

  1. Exercise (30 points)

  2. In the first lecture I gave you an example of a C program that computes and prints the square of an integer.
    It had a function "square" that returns an integer and a main function that called this function.

    a) Type this program and save it under the filename "square1.c". Add the necessary "#include" statements. Make sure to include some comments. Compile it and test it (run it) on different inputs.

    b) Change your program ("square2.c") so it reads the value of the integer (y) from the user using the scanf function.

    c) Rewrite the program ("square3.c") in the following way: Use the function square that takes an integer and returns its square. Use this function from within main() to produce a table that lists all the squares of the integers from a user specified upper limit down to 1. Your program output should look roughly like this:

    % square
    Please enter upper limit: 5

    square(5) = 25
    square(4) = 16
    square(3) = 9
    square(2) = 4
    square(1) = 1

  3. Exercise (20 points)

  4. Write a program avg.c that computes the average of three integer numbers provided by the user. When you run your program to test it, the example run should look like this:

    % a.out
    Enter number 1: 1
    Enter number 2: 2
    Enter number 3: 5
    Average of the numbers is 2.666667

    Hint: See Section 2.10.1 in [IACU] and remember that the average should not be computed using integer arithmetic

  5. Exercise (40 points)

  6. Write a program that does a "Metric to Customary" length conversion for you. The program should take as input a certain number of centimeters (as float) and produces the correct output in the units feet and inches (as float).

    1. Here the program ("met2cust1.c") should do the conversion for one user specified input, which can be any float number (the legth in cm). When you produce the correct output make sure that you handle the following cases correctly:

      • If the length is smaller than 1 foot then print only the correct number of inches (i.e. just "11.345 inches" and not "0 feet and 11.345 inches")
      • Correctly handle the case of feet vs. foot when printing the output (i.e. "1 foot and 2.345 inches" but "3 feet and 2.345 inches").

      • You may disregard the similar case of inches vs. inch. Simply always use inches instead (only for exactly 2.54 cm you would get into trouble anyway).
    2. Modify your program ("met2cust2.c") so that instead of just one conversion you produce a table of conversions. List the correct conversions for every 5 centimeters, from 5 cm up to a user specified limit.
      You should run your program using input and output files: "% met2cust < inp.file > out.file".
      (CVN students: note that you should start DOS prompt from the directory where your executable is).
      For an input file containing the number "65", here is an example output file:

      5 cm are 1.9685 inches
      10 cm are 3.93701 inches
      15 cm are 5.90551 inches
      20 cm are 7.87402 inches
      25 cm are 9.84252 inches
      30 cm are 11.811 inches
      35 cm are 1 foot and 1.77953 inches
      40 cm are 1 foot and 3.74803 inches
      45 cm are 1 foot and 5.71654 inches
      50 cm are 1 foot and 7.68504 inches
      55 cm are 1 foot and 9.65354 inches
      60 cm are 1 foot and 11.622 inches
      65 cm are 2 feet and 1.59055 inches

    Hint 1: The conversion formula from centimeters to inches is:

    inch = cm / 2.54

    Hint 2: When declaring the variables use float instead of int whenever you think that floating point numbers are involved.

    Hint 3: To print a floating point number use %g in the printf() statement (instead of which is used with integers). So, for example

    printf("The value of pi is approx. %g\n", 3.1415);

    will print the value of pi. You may also use %f instead of %g, but %g gives a marginally nicer default formatting. We will discuss formatting issues at a later time.
     


Written (10 points)

  1. Exercise (10 points)
  2. Which of the following statements prints out the values 1 to 10 on seperate lines?
    Rewrite the for loop as a while loop, so that it will generate the same output.

    1. for (int count = 1; count <= 10; count= count + 1)
    	printf("%d\n",count);
    
    2. for (int count = 1; count < 10; count++)
    	printf("%d\n",count);
    
    3. for (int count = 1; count <= 9; count = count + 1)
    	printf("%d ",count);
    
    4. for (int count = 1; count <> 10; count++)
    	printf("%d\n",count);