V22.0002.002 - Pappas
Name


 
 

Departmental Final Exam



 
 

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 = -2; ind < 7; ind *= 2);
    infinite loop 
 
 
ii.
How many times will the following while loop execute ? 
 
 
int ind = 64;
 
 
while( ind % 6 )
 
 
ind /= 6;
      3 
 
 
iii.
What is the value of the following expression (true/false) ? 
 
 
((0 && 0) && (1 && (0 || 1)))
      false 
 
 
iv.
Given: int t = 4;
 
 
What is the value of the following expression (true/false) ? 
 
 
((t = 0) || (t = 1))
      true 
 
 
v.
Given: int f = 23, g = 6; 
 
 
What will be the output of the following printf statement ?
 
 
printf("%d\n", (f / g) + (f % g));
      8 

2. (5 points) What does this code produce?
   int main()
   {
      int i = 0, j = 0 ;
      for ( i = 7; i >= j; i-- )
         for ( j = 3;  j <= 6; j++ )
            printf(``%d'', i + j );
      printf(``\n'');
      return 0;
   }
 
ANSWER: 10111213




V22.0002.002 - Pappas

Name


3. (15 points) Write a FUNCTION that computes two sums from the elements of an array of positive integers.Each element of the array contributes to one of the two sums, depending on whether the element itself is even or odd.A negative array element signals the end of the array.Once each sum is counted, print out the total sum of even numbers and the number of even numbers found and the total sum of odd numbers and the number of odd numbers found.



 

V22.0002.002 - Pappas

Name


4. (15 points) Write a FUNCTION called Shift that shifts the stored values in an array of five characters in a circular fashion.
 
Shift takes two parameters: an array of five (5) characters to manipulate and an integer parameter called iShiftCount that specifies the number of spaces to shift.The function doesn't return anything.You can assume the array is always five characters (don't worry about a NULL character).
 
For example, a call to Shift with the array ABCDE and an iShiftCount of 1 would result in the array being changed to BCDEA; a call to Shift with the array ABCDE and an iShiftCount of 2 would result in the array CDEAB

Notes and Hints:

 
SOLUTION:
void Shift( char letters[], int iShiftCount )
{
    #define FIRST_INDEX 0
    #define LAST_INDEX 4
    int iCurrentShift = 0, iCurrentCharIndex = 0 ;

    char cFirstLetter ;

    for( iCurrentShift = 1 ; iCurrentShift <= iShiftCount ; iCurrentShift++)

    {
        /* save the first as we're moving something in there */
        cFirstLetter = letters[ 0 ] ;
        for( iCurrentCharIndex = FIRST_INDEX ;
                iCurrentCharIndex <= LAST_INDEX - 1;
                    iCurrentCharIndex++)
        {
            letters[ iCurrentCharIndex ] = letters[ iCurrentCharIndex + 1];
        } /* end for icurrentcharindex */
 
        letters[ LAST_INDEX ] = cFirstLetter ;
    } /* end for icurrentshift */

} /* end function */



V22.0002.002 - Pappas

Name


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



V22.0002.002 - Pappas

Name


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



V22.0002.002 - Pappas

Name


7. (20 points) A stack is a collection of items that can be accessed one at a time from one end called the top.An item is added to the top of the stack through a "push" operation and an item is removed from the top of the stack through a "pop" operation.We can use an array to simulate a stack.
Write a program with an array of integers and two functions, push and pop, to simulate a stack.Your program must read in ten integers, push them onto a stack, print a message saying "stack filled", then pop and print each item.You must use your push and pop functions to do this.
push takes in three parameters: an array of type int, an int representing the item to push onto the stack, and the current top's index; it returns the new top's index.

pop takes in two parameters: an array of type int and an int for the top's current index; it returns the int at the top. If there is nothing left in the stack, pop will return -999 and print an error message.

Notes and hints: