BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security & BSc. (Hons.) Software Engineering Cohort: BIS/05/FT BCNS/05/FT BSE/05/FT Examinations for 2005-2006 / Semester 2 MODULE: STRUCTURED PROGRAMMING MODULE CODE: PRG103 Duration: 2 Hours and 15 Minutes Instructions to Candidates: 1. Answer all questions. 2. Programs can be written in C or C++ language. 3. Questions may be answered in any order but your answers must show the question number clearly. 4. Always start a new question on a fresh page. 5. All questions carry equal marks. 6. Total marks 100. This question paper contains 4 questions and 6 pages. Page 1 of 6
QUESTION 1: (25 MARKS) ANSWER ALL QUESTIONS (a) Given a function prototype as shown below. void swap(int *, int *); Write a C/C++ program to pass two variable pointers to that function to swap two integer numbers. (7 marks) (b) Write a C/C++ function that receives two integer parameters and returns an integer. The value returned should be 0 if the parameter values are the same, -1 if the first value is less than the second and 1 if the second value is greater than the first. (5 marks) (c) The following explains how the computation of commission is performed in a company. (i) The price of one book is Rs.200. (iii) Salesmen receive a commission of 20% on each book they sell. Salesmen receiving more than Rs.400 commission receive a bonus of Rs.100. Write a function calculate that will take as parameter the number of books sold by a salesman and will return the commission to be received by the salesman. (13 marks) Page 2 of 6
QUESTION 2: (25 MARKS) (a) Describe the two C functions fprintf() and fscanf(). Using appropriate examples, write the full syntax for each function. (b) Write a C/C++ program that will perform the following: (i) Accept as input 3 book titles and 3 book prices Write the input data into a text file (book.txt) (12 marks) (c) The file info.txt contains information such as name of students and their corresponding ages (see preview below). Write a C/C++ program that will allow a user to read the first student s details. The output should be as shown below. (12 marks) Page 3 of 6
QUESTION 3: (25 MARKS) (a) What is a queue? List two applications of a queue. (4 marks) (b) Give two examples of the use of queues in the real world. (1 mark) (c) What is a Priority queue? (2 marks) (d) Given the following structures and type definition: struct que { int item; struct que *next; } ; struct queu { struct que *front, *rear; }; typedef struct queu queueq; Using C/C++ write functions for the following operations. (i) empty - has one argument of type pointer to queueq, checks if the queue is empty and returns a boolean or integer value. initialise - has one argument of type pointer to queueq, initialises the queue to null and the return type is void. (iii) insert has two argument, one of type pointer to queueq, insert the integer at the rear of the queue and the second of type integer and the return type is void. (Continued) Page 4 of 6
Question 3 : (continued) (iv) remove has one argument of type pointer to queueq, deletes the item at the front and the return type is an integer. (e) Suppose you have an empty linked list (i.e. pointing to NULL) and the following statements are executed: additem(25); additem(50); additem(60); Draw the resulting linked list if the list was implemented using (i) a stack a queue. (Please turn over) Page 5 of 6
QUESTION 4: (25 MARKS) (a) Describe a linked list. (b) Explain the three different types of linked list with the help of diagrams. (c) Give two advantages and two disadvantages of pointer based implementation of a list over array based implementation? (4 marks) (d) Explain how the following operations are performed in a single linked list. You should explain the algorithms using diagrams. (i) Insert a new node at the front of the list. Delete a node located between two nodes of the list. ***END OF QUESTION PAPER*** Page 6 of 6