V22.0002.002 - Pappas

Name

Departmental Final Exam     FALL 2000

The following table is for the instructors use only
 Question Points Score 1 10 2 5 3 15 4 15 5 15 6 20 7 20 Total 100

All work must be done on the exam.
Be sure to write your name on each page of the exam.

V22.0002.002 - Pappas

Name

1. (10 points) Reminder: in C the value 0 is false, everything else is true.
 i. How many times will the following for loop execute ? for(ind = -1; ind < 7; ind *= 2); ii. How many times will the following while loop execute ? int ind = 32; while( ind \% 3 ) ind /= 3; iii. What is the value of the following expression (true/false) ? ((0 && 0) || (1 && (0 || 1))) iv. Given: int t = 4; What is the value of the following expression (true/false) ? ((t = 5) || (t = 6)) v. Given: int f = 43, g = 5; What will be the output of the following printf statement ? printf("%d\n", (f % g) + (f / g));

2. (5 points) What does this code produce?

```   int main()
{
int i = 0, j = 0 ;
for ( i = 4  ; i >= j ; i-- )
for ( j = 0;  j <= 3; j++ )
printf(``%d'', i + j );
printf(``\n'');
return 0;
}```

3. (15 points) Write a program that reads an integer N from the keyboard and then computes and displays the sum of the numbers from N to (2 * N) if N is nonnegative, or the sum of the numbers from (2 * N) to N if N is negative. The starting and ending points are included in the sum.

Sample run:

```   Enter number : 5
Sum : 45```
Sample run:
```   Enter number : -4
Sum : -30```
Notes and hints:
• You should have only 1 loop to compute the sum; use the sign of N to determine the start point and the end point.

4. (15 points) A palindrome is a word, verse, or sentence that reads the same backward or forward. For example, "able was I ere I saw elba" is a palindrome, as are "abba", and "a".

Write a function called IsPalindrome that takes one string parameter called str and returns a 1 if str is a palindrome or 0 if it's not a palindrome. The parameter str represents a string ending in a NULL termination character and you do not know in advance the string's length.

• Do not to write a whole program.
• Do not read in a string from the keyboard; the string is passed to the function.
• Do not use any standard library function.
Hints:
• Since the string passed to the function can be any length, you should count the characters in the string (excluding the NULL termination character) before doing any comparisons.
• With a palindrome, if you look at the beginning and end of the string and work your way one at a time from both ends towards the middle, the character from either end is the same at each step.

5. (15 points) Write a program that reads a positive integer N from the keyboard and outputs all of the prime numbers from 1 up to N. Write and use the function:
int IsPrime(int num);
which returns 1 if num is prime, 0 if it is not.

The main function will prompt for the number N and then it will test all of the numbers from 1 to N inclusive calling IsPrime for each number. If the number is prime it will print it.

Note: A prime number is a number divisible only by itself and 1.

Sample run:

```   Enter number : 9
Prime numbers: 1 2 3 5 7```

6. (20 points) Write a program that reads a date as a long integer in the format MMDDYYYY. The program will then print out the date in the format <Month Name> <day>, <year>.

Sample run:

```   Enter date: 12192000
December 19, 2000.```
In the input the month and the day will always be given as two digits, including leading zero. So the 20th of May, 2000 will be entered as 05202000 and the 3rd of December, 2000 will be entered as 12032000. Don't worry about writing down all of the months, write code that can handle just the first couple and you can put a comment like /* Remaining months the same way */.

7. (20 points) Write a program that reads an integer N greater than 0 from the keyboard and then produces a series of numbers according to the follwing rules:

• If the current value of N is even, the next value of N will be equal to (N/2)
• If the current value of N is odd, the next value of N will be equal to (3 * N + 1)
• The series stops when N becomes equal to 1.

Write and use a function called Next which takes one integer parameter and returns the next value in the series.

Sample run:

```   Enter starting number : 3
Series : 3 10 5 16 8 4 2 1```
Notes and hints:
• In the output you can display the values one next to each other with a space between them. Don't worry about "neat" format on the screen (but don't forget a newline at the end of the output !).
• You can assume that the input from the user will be correct (that is, an integer > 0).
• The main function will "drive" the whole program with a loop. What kind of loop will you need ? What will determine when this loop ends ?
• How do you determine if a number is even or odd ?