B142L Coursework assignment 2 – PRACTICE SAMPLE WITH SOLUTIONS
Academic year 2009/10
COURSEWORK DUE DATE
PRACTICE SAMPLE
COURSEWORK DESCRIPTION
Students are required to submit their coursework online using their individual coursework pages on theMosaic website.
Coursework submissions MUST satisfy the following requirements:
There will be at least one saved version per question answered.
The answer to all questions must be written as one single program.
Answers must not be copied from any printed sources, electronic sources or other people.
Answer all questions. This assessment has a total of 20 marks. Each question will be marked according to the following criteria:
Marks
Criteria
5
Source code uses an effective design to answer the question, compiles without errors and includes informative comments.
4
Source code answers the question, compiles without errors and includes informative comments.
3
Source code answers the question and compiles without errors.
2
Source code is relevant to the question, but does not compile.
1
Source code is incorrect or not relevant to the question.
COURSEWORK QUESTIONS – PRACTICE SAMPLE
QUESTION 1
[5 Marks]
Write a C program that uses a stack or a queue to store integer numbers that are called during a bingo game. The size of the data structure should be equal to 15.
QUESTION 2
[5 Marks]
In the same program as question 1, add source code to implement these features:
Positive input integers should be stored in the data structure
If the input number is zero, then remove the first available element from the data structure
If the input number is any negative value, then stop reading standard input and display all numbers currently in the data structure
QUESTION 3
[5 Marks]
In the same program as question 1, add source code to display an output message, "Bingo!", when there are 5 numbers of the same value stored in the data structure. Stop the program after displaying the winning output message.
QUESTION 4
[5 Marks]
In the same program as question 1, add source code to remove all numbers stored in the data structure and restart the Bingo game logic when the input value is equal to negative one.
struct queue { int currentQueueSize; int queueFrontIndex; int queueRearIndex; int currentQueueItems[ MAX_QUEUE_CAPACITY ]; };
struct queue numbers; /* Queue currently in use by program */
void enqueue(int); /* Adds a number to the rear of the queue */ int dequeue(); /* Removes a number from the front of the queue */ int front(); /* Look at the number currently at the front of the queue */ int size(); /* Count the items currently in the queue */
int main(void) { int input, bingoNumber, bingoCounter;
do { if( scanf("%d", &input ) < 1)break;
/* Check for reset command before any other processing */ if( input == RESET_VALUE ) { /* Empty queue */ while( size() > 0) dequeue();
/* The while loop repeats as long as the value is positive, zero or -1 */ }while( input >= -1);
/* Displays all numbers in the queue, except when the winning condition is found */ while(( bingoCounter != BINGO_SEQUENCE_LENGTH ) && ( size() > 0)) printf("%d ", dequeue()); }
void enqueue(int item ) { /* Do not add if the queue is full */ if( numbers.currentQueueSize >= MAX_QUEUE_CAPACITY )return;
/* Add new item and increment queue size counter */
numbers.currentQueueItems[ numbers.queueRearIndex] = item;
numbers.currentQueueSize++;
/* Move the rear index to the right and wraparound if end of the array is reached */
numbers.queueRearIndex++; if( numbers.queueRearIndex >= MAX_QUEUE_CAPACITY ) numbers.queueRearIndex = numbers.queueRearIndex - MAX_QUEUE_CAPACITY; }
int dequeue() { int item;
/* Nothing to return if the queue is empty */ if( numbers.currentQueueSize < 1)return item;
/* Return the item at the front position and decrement the queue size counter */
item = numbers.currentQueueItems[ numbers.queueFrontIndex];
numbers.currentQueueSize--;
/* Move the front index to the right and wraparound if the end of the array is reached */
numbers.queueFrontIndex++; if( numbers.queueFrontIndex >= MAX_QUEUE_CAPACITY ) numbers.queueFrontIndex = numbers.queueFrontIndex - MAX_QUEUE_CAPACITY; return item; }
int front() { int item;
/* Return the front item only if there is at least one item in the queue */ if( numbers.currentQueueSize > 0)
item = numbers.currentQueueItems[ numbers.queueFrontIndex];