1. (25 points)  Write a program that reads in a 5-digit integer from the user, creates an integer that is the REVERSE of the integer the user inputted, and then prints out that integer.  You can assume the input is always five digits.

Sample Run:

Enter a 5 digit number: 56789

The reverse is: 98765

Algorithm:

i.                declare and initialize variables

ii.              get user input (every entry is only a 5 digit number)

iii.            create an integer that is the reverse of user’s input:

to do so, you must loop five times, and do the following inside the loop:

a.      break off each digit

i.     (Hint – integer division by some divisor)

b.     use that digit in composing the integer for the reverse

i.     (Hint – multiply digit by some multiplier and add it to what you’ve got so far)

c.      adjust the integer representing the user’s input to get rid of the digit you just broke off

iv.             print out one integer representing the reverse.

Hints:

i.                what do you divide by (integer division) to get the left-most digit of a 5-digit number? 4-digit number, 3, 2, 1?  The primary statement can look like this:

iCurrentDigit = iUserInput / iCurrentDivisor;

ii.              you will have to multiply each digit you broke off by something different to get the reverse number

iReverseNumber = iReverseNumber + (iCurrentDigit * iCurrentMultiplier);

iii.            you will have to reduce the value of the user input each iteration so that the integer’s left-most digit is the next one you want to break off.  The way you do that inside the loop is as follows:

iUserInput = iUserInput – (iCurrentDigit * iCurrentDivisor );

iv.             Of course, you have to adjust the iCurrentDivisor and iCurrentMultiplier as appropriate so it’s at the correct value for each iteration of the loop:

a.     iCurrentDivisor starts at 10000 and every iteration gets divided by 10.

b.     iCurrentMultiplier starts at 1 and every iteration gets multiplied by 10.

PROBLEM 1 SOLUTION 1 (see solution 2 below for shorter solution)

#include <stdio.h>

int main()

{

int iInput = 0,

iCurrentDigit = 0,

iCurrentDivisor = 0,

iCurrentMultiplier = 0,

iReverseNumber = 0,

iCounter ;

/* get user input */

printf("Enter a 5 digit number: ");

scanf("%d", &iInput ) ;

/* initialize to 10000, cause we divide a 5 digit #

by that to get the leftmost digit */

iCurrentDivisor = 10000;

/* start out at 1 'cause the leftmost digit will be

our reverse's right-most digit which is the ones position */

iCurrentMultiplier = 1;

/* loop five times -- note could not even use iCounter

if use iCurrentMultiplier or iDivisor for control,

but it's clearer */

for (iCounter = 1 ; iCounter <= 5 ; iCounter++   )

{

iCurrentDigit = iInput / iCurrentDivisor ;

/* build the number by taking the digit and using the

multiplier on it e.g. if we lopped off 5 from 56789, our    first multiplier is 1 soreverse number at that point would be 0 + 5 * 1 = 5 */

iReverseNumber = iReverseNumber + ( iCurrentDigit *

iCurrentMultiplier );

/* reduce our input integer, making it one less digit */

/* e.g. if integer was 56789, subtract 50000 to get 6789 */

iInput -= ( iCurrentDigit * iCurrentDivisor ) ;

/* modify our divisor and multiplier accordingly */

iCurrentDivisor /= 10 ;

iCurrentMultiplier *= 10 ;

} /* end for */

printf ("The reverse is: %d\n", iReverseNumber );

return 0;

} /* end main */

PROBLEM 1 SOLUTION 2 (ALTERNATIVE)

#include <stdio.h>

int main()

{

int        iInput = 0,

iEditedInputValue = 0 ,

iCurrentDigit = 0,

iCurrentFactor = 0,

iCurrentMultiplier = 0,

iReverseNumber = 0,

i ;

printf("Enter a 5 digit number: ");

scanf("%d", &iInput ) ;

/* don't like to mess with user input so store it some

place else so we can edit it */

iEditedInputValue = iInput ;

/* loop five times, handling more in control header */

/* NOTE: could leave out i stuff cause 10000/10 will be 1 in

5 iterations anyway (so could have middle condition of

iCurrentFactor >= 1 instead of i <= 5) */

for (       i = 1, iCurrentFactor = 10000, iCurrentMultiplier = 1 ;

i <= 5 ;

i++, iCurrentFactor /= 10, iCurrentMultiplier *= 10 )

{

iCurrentDigit = iEditedInputValue / iCurrentFactor ;

iReverseNumber = iReverseNumber + ( iCurrentDigit *

iCurrentMultiplier );

iEditedInputValue -= ( iCurrentDigit * iCurrentFactor ) ;

} /* end for i = */

/* print out answer */

printf ("The reverse is: %d\n", iReverseNumber );

return 0;

} /* end main */

2. (25 Points) Given a dimension specified by the user, print out a square box with a diagonal line starting from the upper left corner down to the lower right corner.  Print out an “X” for the borders of the square and an “X” for the diagonal line.  Every other place must be printed with a dot (.).  For example:

Sample Run # 1:

Enter dimension: 5

XXXXX

XX..X

X.X.X

X..XX

XXXXX

Sample Run # 2:

Enter dimension: 7

XXXXXXX

XX....X

X.X...X

X..X..X

X...X.X

X....XX

XXXXXXX

Hints

i.                 You will need at least a nested loop:

a.      You can solve it with a nested loop (i.e. one loop within a loop) depending on how you decide what to print and where

b.     Or you can use one  loop for the top line, a nested loop for the middle (you still have to decide what and where to print), and a loop for the bottom line

ii.               Think about under what circumstances are you printing an “X” ? A dot?  What are the row and column numbers where you’re printing?

iii.             You don’t need getchar, putchar, or char variables to do this

PROBLEM 2 SOLUTION

#include <stdio.h>

#define ROW_FIRST 1

#define COL_FIRST 1

int main()

{

int        iDimension = 0,

iRowLast = 0, iColLast = 0,

iRowIndex = 0, iColIndex = 0;

printf ("Enter dimension: " );

scanf("%d", &iDimension );

iRowLast = iDimension;

iColLast = iDimension;

for (iRowIndex = ROW_FIRST; iRowIndex <= iDimension; iRowIndex++ )

{

for (iColIndex = COL_FIRST; iColIndex <= iDimension; iColIndex++)

{

/* if we’re in first row, first col, last row, last col,

or where row equals col put an X, else put a dot */

if (       iRowIndex == ROW_FIRST || iColIndex == COL_FIRST ||

iRowIndex == iRowLast || iColIndex == iColLast ||

iRowIndex == iColIndex )

printf("X");

else

printf(".");

} /* end for iColindex */

printf("\n");

} /* end for iRowIndex */

} /* end main */

3. (20 points) What output does the following code fragment produce?

int i, j, result ;

for (i = 1; i <= 4; i++)

{

for (j = 10; j >= 6; j--)

{

result = i * j – 1 ;

printf( “%d”, result );

} /* end for j */

} /* end for i */

if ( result % 2 == 0 )

printf(“-”) ;

else

printf(“+”) ;

printf(“\n”);

You must go through the inner loop (j from 10 down to 6) for i = 1, then i = 2, then i = 3, i = 4;

and since there is a printf in the loop, the output of iResult should be displayed each time.  Make a table for each step and write the values of the variables and the output

 Time/Step i j iResult Output T0 (start) 0 0 n/a n/a T1 1 10 9 9 T2 1 9 8 8 T3 1 8 7 7 T4 1 7 6 6 T5 1 6 5 5 T6 2 10 19 19 T7 2 9 17 17 T8 2 8 15 15 etc. AND SO ON etc.

Note that the if else is NOT in the loop so only one + should be printed.

4. (15 points) There are at least 5 errors in the code below.  Comments, formatting or lack thereof are not any of the errors. Identify the line numbers and the errors.

1     #include <stdio.h>

2    int main()

3    {

4         int iInput == 0 ;

5         printf( "Please enter a number\n");

6         scanf ("%d", iInput );

7         while ( iInput > 0 );

8         {

9              if (iInput = 5)

10                  printf("Input is five\n" );

11                  printf("Input is 5\n");

12             else

13                  printf( "Input is not five or 5\n" );

14        } /* end while iInput */

15   } /* end program */

5 from the following lines were accepted for full credit:

line 4 should be gets = not equals ==

line 6 missing & preceding iInput

line 7 semicolon after while loop condition

line 9 should be equals == not gets =

lines 10 and 11 should be enclosed within brackets { }

line 14 iInput is never updated so once in it’s infinite loop

line 15 no return 0 ;

5. (15 points)

i.                 What is the value of result after the following statements?

result = 3;

result += result * 5 – 2 + (7 % 3);  Answer:_____17

ii.               If a = 5 and b = 7, does the following expression in parentheses evaluate to true or false?

( a >= 6 || b < 8 )            Answer: ______TRUE

iii.             Why should you NOT directly use floats in equality comparisons? (one sentence answer only):

because computer may store float differently than it appears making equality comparisons faulty (e.g. 2.3 may be stored as 2.29999999 once and 2.29999998 another time. e.g..3 will not be seen as equal to 2.3)

____________________________________________________________

iv.             What is a “magic number”? (one sentence answer only):

a number that appears in the body of your code (which should be replaced by a constant)

____________________________________________________________

v.               Given the following code fragment, what is the final value of result?

int result = 70 ;

result /= result * 2 + 3 ;

Answer:______0 [ 70/143 in integer division is 0 ]