Homework 2

Due Thursday, 23 October 2008

Written

Question 1

Why is <stdio.h> needed to print output to the screen?


Question 2

If gcc generated an undefined symbol message, which phase(s) of the compiler caught it?


Question 3

Of the many elements that make–up gcc, which one deals with comments and whitespace? How does that component deal with them?


Question 4

What does the –c option (flag) to gcc do?


Question 5

The address of operator (&) is used to take input from the user. Explain why. (Refer to chapter 2 for the answer).


Question 6

Why have you been encouraged to use spaces to replicate tabs in source code?


Question 7

C is commonly known as a high–level language, but it's actually a mid–level language because of its low–level capabilities. Search your textbook and/or the Internet for a definition of "low–level" and summarize your findings.

Programming

Program 1

Write a for loop that prints the multiples of 50 between 0 and 500, inclusive.


Program 2

Write the while equivalent of program 1.


Program 3

Write a program that prompts the user for input in the form of an integer, adds up all the numbers between 1 and the user's input, then prints to the screen the total. For example, if I enter the number 12, the program prints 78.


Program 4

Write an application that inputs one number consisting of five digits from the user, separates that number into its individual digits, and prints each digit separated fom one another by three spaces each. (Note/Hint: You may only use math operators to separate the digits.) For example, if the user types in the number 83283, the program should print


8   3   2   8   3
   

Program 5

Write a program that prompts the user to input a number that represents seconds, then prints to the screen that number's representation in hours, minutes, and seconds. For example


$ Enter seconds:
$ 3902
$ 3902 seconds represents 1 hour, 5 minutes, and 2 seconds
   

Remember to handle negative numbers, as there is no such thing as -30 seconds.


Program 5

Write a program that prompts the user to input a number that represents seconds, then prints to the screen that number's representation in hours, minutes, and seconds. For example


$ Enter seconds:
$ 3902
$ 3902 seconds represents 1 hour, 5 minutes, and 2 seconds
   

Remember to handle negative numbers, as there is no such thing as -30 seconds.


Program 6

Write a program that prints a diamond on the screen according to user input. For example, if the user enters 5, the diamond will look like

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

And, if the user enters 8, the diamond will look like

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

You may use printf statements that print either a single asterisk (*), a single blank, or a combination of the two. Maximize your use of repetition (with nested for statements) and minimize the number of printf statements.

Hint: Printing odd and even diamonds are a bit different. Take note of the width and the height of each diamond: 5 for the first diamond and 9 (8 + 1 space) for the second. Solve the case for odd diamonds first (e.g., user enters 9, 7, 3, etc.), then move on to the even case (e.g., user enters 22, 10, 4, etc).