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

B142L Coursework assignment 1

Academic year 2009/10



COURSEWORK DUE DATE

9pm January 15, 2010



COURSEWORK DESCRIPTION

Students are required to submit their coursework online using their individual coursework pages on the Mosaic website.

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 accepts 8 input characters and displays the ASCII value for all input characters.



QUESTION 2
[5 Marks]

In the same program as question 1, add a do/while loop to iterate through all 8 input characters and display messages that the character is uppercase, lowercase or neither.



QUESTION 3
[5 Marks]

In the same program as question 1, add a function that accepts a character input and returns the character that is 2 ahead. For example, if the input character was D, then the function would return F. As an example of wrap around at the end of the alphabet, if the input character was z, then the function would return b. If the input character was not a letter, then the function would return A.



QUESTION 4
[5 Marks]

In the same program as question 1, add a for loop to iterate through all 8 input characters and display the result of calling the function written for question 3.
#include <stdio.h>
#include <ctype.h>

char convertLetter( char );

int main( void )
{
   char letters[ 8 ];
   int counter;
   
   /* accept input characters and store them in an array */
   for ( counter = 0; counter < 8; counter++ )
   {
      /* Requirement for question 1 */   
      scanf( "%c", &letters[ counter] );
      printf( "%d is the ASCII value of %c\n", letters[ counter ], letters[ counter ] );
   }

   /* Required output for question 2 */
   counter = 0;
   do
   {
      if ( isupper( letters[ counter ] ) ) printf( "%c is uppercase\n", letters[ counter ] );
      else if ( islower( letters[ counter ] ) ) printf( "%c is lowercase\n", letters[ counter ] );
      else printf( "%c not a letter\n", letters[ counter ] );
   } while( ++counter < 8 );

   /* Required output for question 4 */   
   for ( counter = 0; counter < 8; counter++ )
      printf( "%c is converted to %c\n", letters[ counter ], convertLetter( letters[ counter ] ) );
}

/* Requirement for question 3 */   
char convertLetter( char input )
{
   if ( ( input >= 65 ) && ( input <= 90 ) ) /* uppercase letters */
   {
      input = input + 2; /* move ahead by 2 characters */
      if ( input > 90 ) input = input - 26; /* wrap around at Z */
   }
   else if ( ( input >= 97 ) && ( input <= 122 ) ) /* lowercase letters */
   {
      input = input + 2; /* move ahead by 2 characters */
      if ( input > 122 ) input = input - 26; /* wrap around at Z */
   }
   else input = 'A'; /* input is not a letter */
   
   return input;
}