Main Contents Page | CTEC1401 Programming in C |
Quick Reference |
Week 20: Sorting | Week 21: Structures |
Week 22: Consolidation |
Before the lab session read CFTTP Chapter 13 and study the solved exercises very carefully.
Attempt the following unsolved exercises at the end of Chapter 13:
Read the following program carefully and try to understand exactly what it does. Note particularly
#include <stdio.h> typedef struct { int suit; int rank; } CARD; /* Notice the use of #define below. For example, #define clubs 1 This tells the C compiler that whenever it sees the token "clubs" in the program text it should be replaced by the symbol 1. This means that in your program you can refer to special values by using meaningful names for them instead of the actual numbers. For example the switch statements can be written case clubs: which is more readable and more flexible than case 1 : (meaning clubs) Also note that the #define (like #include) does not have a semicolon ";" at the end. */ #define clubs 1 #define diamonds 2 #define hearts 3 #define spades 4 #define jack 11 #define queen 12 #define king 13 #define ace 14 /* Ace is higher than a king */ void printCard( CARD card ) { printf("("); switch (card.rank) { case ace : printf("A "); break; case king : printf("K "); break; case queen : printf("Q "); break; case jack : printf("J "); break; case 10 : printf("10 ");break; default : printf("%d ",card.rank); } switch (card.suit) { case clubs : printf("C"); break; case diamonds : printf("D"); break; case hearts : printf("H"); break; case spades : printf("S"); break; } printf(")"); } main() { CARD c; int s,r; for (s=clubs; s<=spades; s++) { c.suit = s; for (r=2;r<=ace;r++) { c.rank = r; printCard(c); } printf("\n"); } }
CARD deck[52];and then write code to fill the array with the 52 playing cards in the following order: two of clubs, two of diamonds, two of hearts, two of spades, three of clubs, three of diamonds, etc. up to ace of clubs, ace of diamonds, ace of hearts, ace of spades. Once the deck has been so prepared you should print it out (in order from 0 to 51) to check that the cards are all in the right places.
int sameRank( CARD c1, CARD c2)that returns true (1) if the input cards are of the same rank and false (0) otherwise. For example, the two of hearts and the two of clubs are of the same rank (i.e. 2) so the answer should be true. Test your function by writing code to compare all the adjacent cards in the deck you have already created. That is, compare deck[0] and deck[1]; then deck[1] and deck[2]; etc. up to deck[50] and deck[51]. Make sure you print out the result of each test.
Make a copy of your cards program as joker.c. Then modify the program so that our deck of cards contains two jokers (wild cards). You will need to
Once you have made these modifications you should check that your program is working correctly.
A sports results table (e.g. a football or hockey league table) has the following format
_______Home______ _______Away______ W D L F A W D L F A Leicester_City 7 0 0 21 0 7 0 0 21 0 Derby_Forest 6 1 0 14 5 4 1 2 13 8 Nottingham_Wednesday 5 1 1 13 5 6 0 1 17 3 Wimbledon_FC 4 0 3 10 7 3 3 1 9 9 Manchester_Wanderers 3 2 2 7 6 3 1 3 6 8 Wakefield_Albion 2 4 1 3 8 2 4 1 4 9 Upton_Villa 1 2 4 1 12 1 3 3 3 13 Birmingham_Rangers 0 0 7 0 22 0 0 7 0 17 where W = Won D = Drawn L = Lost F = Goals For A = Goals AgainstWrite a program league.c that
struct
type to hold the results for a
single team.league
that will contain the
all the results for a league containing eight teams.Leicester_City Derby_Forest 2 0 Leicester_City Nottingham_Wednesday 3 2 Leicester_City Wimbledon_FC 2 1 Leicester_City Manchester_Wanderers 4 3 Leicester_City Wakefield_Albion 2 0 Leicester_City Upton_Villa 3 0 Leicester_City Birmingham_Rangers 5 4 Derby_Forest Leicester_City 1 2 Derby_Forest Nottingham_Wednesday 4 2 Derby_Forest Wimbledon_FC 3 0 Derby_Forest Manchester_Wanderers 0 1 Derby_Forest Wakefield_Albion 2 0 Derby_Forest Upton_Villa 1 1 Derby_Forest Birmingham_Rangers 2 2 Nottingham_Wednesday Leicester_City 0 1 Nottingham_Wednesday Derby_Forest 2 2 Nottingham_Wednesday Wimbledon_FC 0 3 Nottingham_Wednesday Manchester_Wanderers 1 1 Nottingham_Wednesday Wakefield_Albion 1 2 Nottingham_Wednesday Upton_Villa 3 2 Nottingham_Wednesday Birmingham_Rangers 1 3 Wimbledon_FC Leicester_City 2 5 Wimbledon_FC Derby_Forest 3 3 Wimbledon_FC Nottingham_Wednesday 3 1 Wimbledon_FC Manchester_Wanderers 2 2 Wimbledon_FC Wakefield_Albion 4 1 Wimbledon_FC Upton_Villa 1 0 Wimbledon_FC Birmingham_Rangers 1 2 Manchester_Wanderers Leicester_City 2 3 Manchester_Wanderers Derby_Forest 0 0 Manchester_Wanderers Nottingham_Wednesday 1 1 Manchester_Wanderers Wimbledon_FC 1 3 Manchester_Wanderers Wakefield_Albion 2 4 Manchester_Wanderers Upton_Villa 3 3 Manchester_Wanderers Birmingham_Rangers 2 2 Wakefield_Albion Leicester_City 0 1 Wakefield_Albion Derby_Forest 2 1 Wakefield_Albion Nottingham_Wednesday 4 3 Wakefield_Albion Wimbledon_FC 2 3 Wakefield_Albion Manchester_Wanderers 5 0 Wakefield_Albion Upton_Villa 2 1 Wakefield_Albion Birmingham_Rangers 2 4 Upton_Villa Leicester_City 0 3 Upton_Villa Derby_Forest 3 3 Upton_Villa Nottingham_Wednesday 3 1 Upton_Villa Wimbledon_FC 2 6 Upton_Villa Manchester_Wanderers 3 2 Upton_Villa Wakefield_Albion 2 1 Upton_Villa Birmingham_Rangers 2 3 Birmingham_Rangers Leicester_City 2 3 Birmingham_Rangers Derby_Forest 4 2 Birmingham_Rangers Nottingham_Wednesday 2 3 Birmingham_Rangers Wimbledon_FC 1 1 Birmingham_Rangers Manchester_Wanderers 1 1 Birmingham_Rangers Wakefield_Albion 0 3 Birmingham_Rangers Upton_Villa 0 2Then you can run your program and input the data from this file by entering:
./league < teamdata.txt