Home B101 B142L B202 B302 B324 M520 Mosaic Course updatesPresentation format
<http://cnfolio.com/B142LCW20100422Practice>
Introduction to Computing – B142L

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 the Mosaic website.

Coursework submissions MUST satisfy the following requirements:
  1. There will be at least one saved version per question answered.
  2. The answer to all questions must be written as one single program.
  3. 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:
MarksCriteria
5Source code uses an effective design to answer the question, compiles without errors and includes informative comments.
4Source code answers the question, compiles without errors and includes informative comments.
3Source code answers the question and compiles without errors.
2Source code is relevant to the question, but does not compile.
1Source 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:



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.
#include <stdio.h>

#define MAX_QUEUE_CAPACITY     15
#define BINGO_SEQUENCE_LENGTH  5
#define RESET_VALUE            -1

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();
   
         /* Reset bingo counter */
         bingoNumber = input;
         bingoCounter = 1;
   
         /* Go to next input number */
         continue;
      }

      /* Add positive numbers to the queue */
      if ( input > 0 )
      {
         enqueue( input );
   
         if ( bingoNumber == input )
         {
            /* Increment bingo counter */
            bingoCounter++;
         }
         else
         {
            /* Reset bingo counter */
            bingoNumber = input;
            bingoCounter = 1;
         }
      }

      /* Remove a number when the input is zero */
      if ( input == 0 )
      {
         dequeue();
   
         /* Always reset bingo counter when a number is removed */
         bingoNumber = input;
         bingoCounter = 1;
      }

      /* Check for winning condition */
      if ( bingoCounter == BINGO_SEQUENCE_LENGTH )
      {
         printf( "Bingo!" );

         /* Jump out of loop */
         break;
      }

      /* 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 ];

   return item;
}

int size()
{
   return numbers.currentQueueSize;
}