An Introduction to C: HW 1

The written portion of this assignment should be written in a text file — not a Word® file or a rich text file, but a basic ASCII text file, regardless of OS.

For the programming section, each question should be answered in its own file. So, since there are four programming questions, and the first one is answered in three separate files, you will submit six .c files.

This assignment is due Thursday, 10 March 2011, by 11:59 PM. Read the HW submission instructions for directions on how to submit your homework electronically.

Written

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). Show your work.

Question 5

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

Programming

Program 1

Check your answers to question 4 from the written section above by writing three C programs that make the conversion for you. You should notice a pattern by the time you code the third program.

To lessen your burden, set the LSB to the left and the MSB to the right.

Program 2

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

Program 3

Using only the programming techniques that you've learned in chapters two from the text, 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. Use only one looping construct.

   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

Program 4

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.