V22.0002.002 - Pappas

Name



 
 



Departmental Final Exam     FALL 2000



 
 
 
 
 
 

The following table is for the instructors use only
Question Points Score
10 
 
 
15 
 
15 
 
15 
 
20 
 
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;
   }
ANSWER: 4567


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:

Solution


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.

Hints:

Solution


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

Solution



 

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 */.


Solution


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:


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:

Solution