Homework 1

Due Thursday, 2 October 2008 by 11:59 PM


Question 1

Work the output of the following program fragment by hand; ie, without using your compiler.


   printf( "/*\t\v\t\t\a\t\a\nn\nnn**\n\t*\t*n\n*\n*/" );
   printf( " " /* " \n" */ " " );
   

Question 2

Although the following program will compile properly, it is poorly formatted. Format it according to convention.


   #include <stdio.h>
   int 
   main
   (
   void     )
   {printf(
   "I  am a sloppyline ofcode");return 
   0;
   }
   

Question 3

The following program is poorly formatted and contains syntax errors. Format the source neatly and correct any errors, bringing the program to completion.


#include <stdio.h>

int main( void )
{ pintf  ( "\033[31m My Color is ? \033[m\n" ); pintf  
( "\q" )
Pintf  ( "\033[42m yellow and blue make... \033[m" )
Printf ( "\033[43m much red; much green; 
little blue... \033[m" );   
return ( -1 );
}
   

Question 4

Convert 12510 to base 2 (binary), base 8 (octal), and base 16 (hexadecimal). Do the same for 12810, 102210, and 1310. Show your work.


Question 5

Check your answers to question 4 by writing a C program that makes the conversion for you.


Question 6

Convert 7F16 to base 2 (binary). Perform the same conversions for abc16, 7f7a6316, and cbcf16.

Hint: convert to decimal as an intermediary step.


Question 7

Explain the meanings of syntax error, logic errors, and runtime errors. Is there overlap between any two or three?


Question 8

Write a program that inputs three different integers from the keyboard, then prints the sum, the average, the product, the smallest and the largest of these numbers. Use only the single–selection form of the if statement you learned in chapter 2 of C How To Program. The screen dialog should appear as follows:

Input three different integers: 13 27 14
   Sum is 54
   Average is 18
   Product is 4914
   Smallest is 13
   Largest is 27
   

Question 9

Using only the programming techniques that you've learned in chapters two, write an application that calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting values in table format as shown below.


      number  square  cube
      0       0       0
      1       1       1
      2       4       8
      3       9       27
      4       16      64
      5       25      125
      6       36      216
      7       49      343
      8       64      512
      9       81      729
      10      100     1000
   

Question 10

Write a program that reads a 5–digit number and determines whether or not it is a palindrome. For example, 78987 is a palindrome, but 78988 is not.

Hint: “pick” off the numbers using the modulo and division operators.