Home   MosaicPresentation format
<http://cnfolio.com/B142LCW20100430>
Introduction to Computing – B142L

B142L Coursework assignment 2

Academic year 2009/10



COURSEWORK DUE DATE

9pm April 30, 2010



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

QUESTION 1
[5 Marks]

Write a C program that uses a stack or a queue to store individual input characters. The data structure size must be 10. The program must:
  • Accept 10 input characters
  • Store them into the data structure
  • Stop accepting input and end the program



QUESTION 2
[5 Marks]

In the same program as question 1, add source code to identify sequences of 3 consecutive input characters that are the same. The program must display the number of sequences that were found. If the input does not contain any matching sequences, then display zero sequences.

For example, if the input characters are:
RRRedTag9?

Then, the program must display the following output message:
Found 1 sequence




QUESTION 3
[5 Marks]

In the same program as question 1, add source code to calculate and display the sum of points for the 10 input characters stored in the data structure using the following rules:
  • Any number is worth 4 points,  e.g. character 9 is worth 4 points and character 0 is worth 4 points
  • Any uppercase letter is worth 3 points,  e.g. character W is worth 3 points
  • Any lowercase letter is worth 2 points,  e.g. character b is worth 2 points
  • Any other character is worth 1 point,  e.g. character ? is worth 1 point

For example, if the input characters are:
RRRedTag9?

Then, the program must display the following output message:
Total points is 25




QUESTION 4
[5 Marks]

In the same program as question 1, add source code to identify and display 3 characters stored in the data structure which have the highest points value. If there are more than 3 characters with the highest points value, then the program may select any of them. The order of the characters when displayed is not important.

For example, if the input characters are:
RRRedTag9?

Then, the program output could be similar to the following. Your program may display the output characters in a different order.
Highest input characters are 9 T R

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX_STACK_SIZE  10

struct stack
{
   int  currentStackSize;
   char currentStackItems[ MAX_STACK_SIZE ];
};

struct stack letters;  /* Stack currently in use by program */

void push( char )/* Adds a character to the top of the stack */
char pop()/* Remove a character from the top of the stack */
char top()/* Look at the character currently at the top of the stack */
int  size()/* Count the characters currently in the stack */

int main( void )
{
   char token, sequencesPattern, topToken1, topToken2, topToken3;
   int counter, tokenPoints, totalPoints, sequencesCounter, sequencesFound;
   int topToken1Points, topToken2Points, topToken3Points;

   /* Required for Question 1 */
   for ( counter = 0; counter < MAX_STACK_SIZE; counter++ )
   {
      scanf( "%c", &token );
      push( token );
   }

   for ( counter = 0; counter < MAX_STACK_SIZE; counter++ )
   {
      token = pop();

      /* Required for Question 2 */
      if ( ( sequencesPattern == token ) && ( sequencesCounter > 0 ) )
         sequencesCounter++;
      else
      {
         /* Reset each time a non-matching character is found */
         sequencesPattern = token;
         sequencesCounter = 1;
      }

      /* Reset sequence counter variables when a sequence is found */
      if ( sequencesCounter == 3 )
      {
         sequencesFound++;
         sequencesCounter = 0;
      }

      /* Required for Question 3 */
      if ( isdigit( token ) ) tokenPoints = 4;
      else if ( isupper( token ) ) tokenPoints = 3;
      else if ( islower( token ) ) tokenPoints = 2;
      else tokenPoints = 1;

      /* Add token points to total points */
      totalPoints += tokenPoints;

      /* Required for Question 4 */
      if ( tokenPoints > topToken1Points )
      {
         topToken1Points = tokenPoints;
         topToken1 = token;
      }
      else if ( tokenPoints > topToken2Points )
      {
         topToken2Points = tokenPoints;
         topToken2 = token;
      }
      else if ( tokenPoints > topToken3Points )
      {
         topToken3Points = tokenPoints;
         topToken3 = token;
      }
   }

   printf( "Found %d sequence\n", sequencesFound );
   printf( "Total points is %d\n", totalPoints );
   printf( "Highest input characters are %c %c %c", topToken1, topToken2, topToken3 );
}

void push( char input )
{
   if ( letters.currentStackSize >= MAX_STACK_SIZE ) return;

   letters.currentStackItems[ letters.currentStackSize ] = input;
   letters.currentStackSize++;
}

char pop()
{
   if ( letters.currentStackSize < 1 ) return ' ';

   letters.currentStackSize--;
   return letters.currentStackItems[ letters.currentStackSize ];
}

char top()
{
   if ( letters.currentStackSize < 1 ) return ' ';

   return letters.currentStackItems[ letters.currentStackSize - 1 ];
}

int size()
{
   return letters.currentStackSize;
}