Home     Mosaic     CLIP     Octave     R Project     Processing     Contact  
Search
<http://cnfolio.com/B142LCW20090515>
Introduction to Computing – B142L

B142L referral assessment for Coursework 2

Academic year 2008/9



Coursework Title:  C arrays, strings and functions
Coursework Due Date:  May 15, 2009
Lecturer:  C Nguyen



COURSEWORK DESCRIPTION

This is the referral assessment for Coursework 2. This assessment has a total of 20 marks. Students must obtain a mark of 8 or higher for this referral assessment in order to increase their Coursework 2 mark to 40%.

Students are required to submit their programs online using the Mosaic website. Add the programs to the top of your coursework page.

Each question will be marked according to the following criteria:
MarksCriteria
5Program uses an effective design to answer the question, compiles without errors and includes informative comments.
4Program answers the question, compiles without errors and includes informative comments.
3Program answers the question and compiles without errors.
2Program is relevant to the question, but does not compile.
1Program is incorrect or not relevant to the question.




COURSEWORK QUESTIONS

QUESTION 1

Write a C program that uses an array to store the following values and a loop to display any value which is greater than 50.   [5 Marks]
20, 40.2, 22, 42.5, 36, 55.78, 39.4, 61, 62, 73.11

int main( void )
{
   double numbers[] = { 20, 40.2, 22, 42.5, 36, 55.78, 39.4, 61, 62, 73.11 };
   int index;
   
   for ( index = 0; index < 10; index++ )
   {
      if ( numbers[ index ] > 50 )
         printf( "%.2lf ", numbers[ index ] );
   }
}



QUESTION 2

Define a function using the following prototype. Then, write a C program that asks for input as US dollars and uses the function to display the output as GBP sterling. The current exchange rate is 1 GBP to 1.5207 USD.   [5 Marks]
double gbp_to_usd( double );

double gbp_to_usd( double );

int main( void )
{
   double usd = 0.0;
   scanf( "%lf", &usd );
   printf( "%.2lf usd is equivalent to %.2lf gbp", usd, gbp_to_usd( usd ) );
}

double gbp_to_usd( double usd )
{
   return ( usd / 1.5207 );
}



QUESTION 3

Write a C program that asks for 5 input text strings and displays to output the largest string length value.   [5 Marks]
#include <string.h>

int main( void )
{
   char text[ 256 ] = { '\0' };
   int max_length = 0;
   int counter;
   
   for ( counter = 0; counter < 5; counter++ )
   {
      scanf( "%s", text );
      if ( strlen( text ) > max_length )
         max_length = strlen( text );
   }
   
   printf( "The largest string length is %d", max_length );
}



QUESTION 4

Write a C program that asks for an input text string, replaces all instances of the character m with the character w, and then displays the text string.   [5 Marks]
#include <string.h>

int main( void )
{
   char text[ 256 ] = { '\0' };
   int index;
   
   scanf( "%s", text );
   for ( index = 0; index < strlen( text ); index++ )
   {
      if ( text[ index ] == 'm' )
         text[ index ] = 'w';
   }

   printf( "%s", text );
}




C COMPILER AND REFERENCES

Brief reference of C language and functions.
Alphabetical index of functions in the standard C libraries.