OLLSCOIL NA hÉIREANN

THE NATIONAL UNIVERSITY OF IRELAND

COLÁISTE NA hOLLSCOILE, CORCAIGH

UNIVERSITY COLLEGE, CORK

Summer Examination 2007

Computer Science

CS1061 C Programming

Prof. S. Craw

Prof. G. Provan

Mr. A. P. O'Riordan

Time: 90 minutes

Answer any three of four Questions

All questions equal marks (Total = 100 marks)

Question 1.

(a)

Write a single C line to accomplish each of the following: [5 marks]

(i).  Display the value 3.145 with two digits of precision.

(ii).  Output the new line character.

(iii).  Read a double from the keyboard and store the value entered in the float variable speed.

(iv).  Assign w the average of the values x, y and z.

(v).  Use the preprocessor to define ARR_SIZE as 100.

(b).

Given the following declarations:

int i = 5;

float f = 3.0;

What is the value of each of the following? [5 marks]

(i).  i + f

(ii).  i++

(iii).  i / f

(iv).  (int) f / i

(v).  f % 2

(c).

Give the type and value of each of the following expressions. (e.g. 10/3 is an integer with the value 3.) [8 marks]

(i).  2 * 3

(ii).  '?'

(iii).  -- 3

(iv).  (1 > 2) || (3 < 4)

(d).

Consider a binary search procedure, which searches an integer array for a given key, assuming the elements of the array are in ascending order. Correct the four deliberate mistakes in the code below. [8 marks]

bool *binarySearch(int size int arr[], int searchKey){

/* returns true if searchKey in arr */

int i = 0, j, mid;

j = size - 1;

while (i < j){

mid = (i + j)/3;

if (searchKey <= arr[m])

j = mid;

else;

i = mid + 1;

}

return arr[i] != searchKey;

}

(e).

Give short written answers to the following: [7 marks]

(i).  What is a compiler?

(ii).  How does a while loop differ from a for loop?

(iii).  Contrast an array with a struct.

(iv).  What is a stream and how is it declared?

Question 2.

(a).

Write a function to calculate the growth of an investment over a ten-year period. Your program should prompt the user for the value of the principal invested and the interest rate, in percent. It should print out the value of the investment at each anniversary, like this:

Principal: 1000.00, interest rate: 4.99%

After 1 year the value of the investment will be 1049.90

After 2 years the value of the investment will be 1102.29

… [17 marks]

(b).

Write a function to read in a line of text from the standard input using fgetc() and display it backwards. You can declare and use an array to store the text.

You may not use the scanf() family of functions or gets(). [16 marks]

Question 3.

(a).

Write a function that will calculate if a given year is a leap year. You can use the prototype below as a guide.

int isLeapYear(unsigned int year);

A leap year is defined as any year that is divisible by 4 but not by 100 or is divisible by 400. [17 marks]

(b).

Write a function which keeps a running average on a sequence of floats. Read in the numbers from the keyboard.

For example, the sequence of numbers 3.0, 4.0, 9.0, 2.0, … produces the running average 3.0, 3.5, 4.0, … i.e. 3.0 / 1 = 3.0, (3.0 + 4.0) / 2 = 3.5, (3.0 + 4.0 + 9.0) / 3 = 4.0, etc.

The sequence should converge. [16 marks]

Question 4.

Write a complete program to count the number of lines in a sequential file, “Essay.txt”.

The program should open the pre-existent text file for reading (checking if the open operation was successful), count the number of lines in the file (use a variable called numLines) and close the file afterwards.

If the file is empty a message should be displayed it that effect.

Remember to include the relevant header files. Declare any variables you feel are necessary.

No marks are given for comments. [33 marks]

Page 3 of 4