Introduction to Computing – B142L
B142L worksheet 20: Functions
Academic year
2011/12
Objectives
- Practice designing and creating functions.
- Practice structured programming and using dry run tables to test and debug program logic.
- Practice documenting and using incremental steps in the software development life cycle to solve a problem.
Write a program that implements a simple substitution algorithm. The program should read an input string and:
- Substitute each original text character with a character that is 3 places ahead in the alphabet.
- Substitution wraps around at the end of the alphabet.
The program should create and use at least 2 user-defined functions.
The input text string is
hello would produce this sample output:
khoor
The input text string is
portsmouth would produce this sample output:
sruwvprxwk
Extend the program so that the number of places ahead for the substitution can be specified as an input value with the input string.
Example code for using the Cairo library:
#include <cairo.h>
int main ( void )
{
cairo_surface_t *surface =
cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 80);
cairo_t *cr = cairo_create (surface);
cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 32.0);
cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
cairo_move_to (cr, 10.0, 50.0);
cairo_show_text (cr, "Hello, world");
cairo_destroy (cr);
cairo_surface_write_to_png (surface, "/tmp/hello.png");
cairo_surface_destroy (surface);
return 0;
}
Example code for using the ming SWF library:
#include <stdio.h>
#include <stdlib.h>
#include <ming.h>
int main(void)
{
SWFFillStyle fill_style;
SWFShape square_definition;
SWFDisplayItem square_display_item;
SWFMovie test_movie;
Ming_init();
test_movie = newSWFMovieWithVersion(7);
SWFMovie_setDimension(test_movie, 6000, 6000);
SWFMovie_setBackground(test_movie, 0x00, 0x00, 0x00);
SWFMovie_setRate(test_movie, 12.0);
SWFMovie_setNumberOfFrames(test_movie, 120);
fill_style = newSWFSolidFillStyle(0xa5, 0xa5, 0xff, 0x80);
square_definition = newSWFShape();
SWFShape_setRightFillStyle(square_definition, fill_style);
SWFShape_drawLine(square_definition, 100.0, 0.0);
SWFShape_drawLine(square_definition, 0.0, 100.0);
SWFShape_drawLine(square_definition, -100.0, 0.0);
SWFShape_drawLine(square_definition, 0.0, -100.0);
square_display_item = SWFMovie_add(test_movie, (SWFBlock) square_definition);
SWFDisplayItem_moveTo(square_display_item, 100.00, 100.0);
Ming_setSWFCompression(9);
SWFMovie_save(test_movie, "temp.swf");
return 0;
}
Post a comment to your Mosaic coursework page to record that you've finished worksheet 20.