Introduction to Computing – B142L
B142L Computing practical – Using control structures
Academic year
2009/10
Learning outcomes
Evaluate source code and write C programs that use control structures .
Apply logic and math to the design of control structures .
Practice using pseudo code and D structures to document control structures.
Practice using the scanf() function to obtain input.
Exercise 1 : Obtain input values
Write a C program to obtain and display input values of the following data types:
character
integer
float
double
#include <stdio.h>
int main
( void )
{
char code;
int number;
float value;
double money;
scanf
( "%c %d %f %lf" , &code, &number, &value, &money
) ;
printf ( "%c %d %.2f %.2lf" , code, number, value, money
) ;
}
There are detailed formatting rules to specify the data type when obtaining input:
An important learning outcome of the unit is the ability to read and use technical documentation . The 4 web links in the above section are required to complete this practical worksheet.
Exercise 2 : Handling of spaces
Read the reference information in the section above and determine how spaces are handled when writing C programs that use input values. Write sample programs to test your understanding.
#include <stdio.h>
int main
( void )
{
int code1;
char code2;
scanf
( "%d%c" , &code1, &code2
) ;
printf ( "Found [%d] and [%c] \n " , code1, code2
) ;
scanf
( "%d %c" , &code1, &code2
) ;
printf ( "Found [%d] and [%c] \n " , code1, code2
) ;
scanf
( "%d %c" , &code1, &code2
) ;
printf ( "Found [%d] and [%c] \n " , code1, code2
) ;
scanf
( "%d" , &code1
) ;
scanf
( "%c" , &code2
) ;
printf ( "Found [%d] and [%c] \n " , code1, code2
) ;
}
Exercise 3 : Vending machine selection by number
Let's consider a vending machine that has sells 10 different sweets and candy:
Mars
Snickers
Bounty
Peanut M&M
Chocolate M&M
Aero Bubbles
Fruit Pastilles
Wine Gums
Polo mints
Haribo Gold Bears
Write a C program that display a message corresponding to the candy selected by the user.
Following is a sample output display when the
user input is number 4
You selected Peanut M&M
#include <stdio.h>
int main
( void )
{
int candy;
scanf
( "%d" , &candy
) ;
if ( candy ==
1 ) printf ( "You selected Mars" ) ;
if ( candy ==
2 ) printf ( "You selected Snickers" ) ;
if ( candy ==
3 ) printf ( "You selected Bounty" ) ;
if ( candy ==
4 ) printf ( "You selected Peanut M&M" ) ;
if ( candy ==
5 ) printf ( "You selected Chocolate M&M" ) ;
if ( candy ==
6 ) printf ( "You selected Aero Bubbles" ) ;
if ( candy ==
7 ) printf ( "You selected Fruit Pastilles" ) ;
if ( candy ==
8 ) printf ( "You selected Wine Gums" ) ;
if ( candy ==
9 ) printf ( "You selected Polo mints" ) ;
if ( candy ==
10 ) printf ( "You selected Haribo Gold Bears" ) ;
}
Exercise 4 : Vending machine selection by character
Let's change the list of sweets and candy to characters:
Mars
Snickers
Bounty
Peanut M&M
Chocolate M&M
Aero Bubbles
Fruit Pastilles
Wine Gums
Polo mints
Haribo Gold Bears
Write a C program that display a message corresponding to the candy selected by the user.
Following is a sample output display when the
user input is character c
You selected Bounty
#include <stdio.h>
int main
( void )
{
char candy;
scanf
( "%c" , &candy
) ;
if ( candy ==
'a' ) printf ( "You selected Mars" ) ;
if ( candy ==
'b' ) printf ( "You selected Snickers" ) ;
if ( candy ==
'c' ) printf ( "You selected Bounty" ) ;
if ( candy ==
'd' ) printf ( "You selected Peanut M&M" ) ;
if ( candy ==
'e' ) printf ( "You selected Chocolate M&M" ) ;
if ( candy ==
'f' ) printf ( "You selected Aero Bubbles" ) ;
if ( candy ==
'g' ) printf ( "You selected Fruit Pastilles" ) ;
if ( candy ==
'h' ) printf ( "You selected Wine Gums" ) ;
if ( candy ==
'i' ) printf ( "You selected Polo mints" ) ;
if ( candy ==
'j' ) printf ( "You selected Haribo Gold Bears" ) ;
}
Exercise 5 : Handling invalid input values
Add code to your programs for Exercise 3 and 4 to display an informative error message when the user provides invalid input values , e.g. less than 1 or the character z.
#include <stdio.h>
int main
( void )
{
char candy;
scanf
( "%c" , &candy
) ;
if ( candy ==
'a' ) printf ( "You selected Mars" ) ;
else if ( candy ==
'b' ) printf ( "You selected Snickers" ) ;
else if ( candy ==
'c' ) printf ( "You selected Bounty" ) ;
else if ( candy ==
'd' ) printf ( "You selected Peanut M&M" ) ;
else if ( candy ==
'e' ) printf ( "You selected Chocolate M&M" ) ;
else if ( candy ==
'f' ) printf ( "You selected Aero Bubbles" ) ;
else if ( candy ==
'g' ) printf ( "You selected Fruit Pastilles" ) ;
else if ( candy ==
'h' ) printf ( "You selected Wine Gums" ) ;
else if ( candy ==
'i' ) printf ( "You selected Polo mints" ) ;
else if ( candy ==
'j' ) printf ( "You selected Haribo Gold Bears" ) ;
else printf ( "Invalid candy selection" ) ;
}
Exercise 6 : Vending machine cost
Let's add a cost to each sweet sold by the vending machine:
£0.55 Mars
£0.55 Snickers
£0.55 Bounty
£0.85 Peanut M&M
£0.85 Chocolate M&M
£0.65 Aero Bubbles
£0.55 Fruit Pastilles
£0.55 Wine Gums
£0.45 Polo mints
£0.95 Haribo Gold Bears
Write a C program that accepts
2 input values :
candy selection
amount of money
The program should display a message
whether there is enough money for the user selection .
Following is a sample output display when the
user inputs are character c and amount 1.20
You selected Bounty and there is enough money
Following is a sample output display when the
user inputs are character h and amount 0.40
You selected Wine Gums, but, do not have enough money
#include <stdio.h>
int main
( void )
{
char candy;
float money, price;
scanf
( "%c %f" , &candy, &money
) ;
if ( candy ==
'a' )
{
printf ( "You selected Mars" ) ;
price =
0.55 ;
}
else if ( candy ==
'b' )
{
printf ( "You selected Snickers" ) ;
price =
0.55 ;
}
else if ( candy ==
'c' )
{
printf ( "You selected Bounty" ) ;
price =
0.55 ;
}
else if ( candy ==
'd' )
{
printf ( "You selected Peanut M&M" ) ;
price =
0.85 ;
}
else if ( candy ==
'e' )
{
printf ( "You selected Chocolate M&M" ) ;
price =
0.85 ;
}
else if ( candy ==
'f' )
{
printf ( "You selected Aero Bubbles" ) ;
price =
0.65 ;
}
else if ( candy ==
'g' )
{
printf ( "You selected Fruit Pastilles" ) ;
price =
0.55 ;
}
else if ( candy ==
'h' )
{
printf ( "You selected Wine Gums" ) ;
price =
0.55 ;
}
else if ( candy ==
'i' )
{
printf ( "You selected Polo mints" ) ;
price =
0.45 ;
}
else if ( candy ==
'j' )
{
printf ( "You selected Haribo Gold Bears" ) ;
price =
0.95 ;
}
else
{
printf ( "Invalid candy selection" ) ;
price =
0.0 ;
}
if ( price >
0 )
{
if ( money >= price
) printf ( " and there is enough money" ) ;
else printf ( ", but, do not have enough money" ) ;
}
}
Write pseudo code and D structures when designing control structures. They are easier to communicate and revise than source code.
Exercise 7 : Vending machine multiple purchases
Using the same list and prices from Exercise 6,
write a C program that accepts
2 input values :
candy selection
amount of money
The program should display a message about
the quantity of sweets that can be purchased by the user .
Following is a sample output display when the
user inputs are character c and amount 2.00
You selected Bounty and have enough money to buy 3 bars
Following is a sample output display when the
user inputs are character d and amount 4.50
You selected Peanut M&M and have enough money to buy 5 bags
Following is a sample output display when the
user inputs are character h and amount 0.40
You selected Wine Gums, but, do not have enough money
#include <stdio.h>
int main
( void )
{
char candy;
float money, price;
int quantity;
scanf
( "%c %f" , &candy, &money
) ;
if ( candy ==
'a' )
{
printf ( "You selected Mars" ) ;
price =
0.55 ;
}
else if ( candy ==
'b' )
{
printf ( "You selected Snickers" ) ;
price =
0.55 ;
}
else if ( candy ==
'c' )
{
printf ( "You selected Bounty" ) ;
price =
0.55 ;
}
else if ( candy ==
'd' )
{
printf ( "You selected Peanut M&M" ) ;
price =
0.85 ;
}
else if ( candy ==
'e' )
{
printf ( "You selected Chocolate M&M" ) ;
price =
0.85 ;
}
else if ( candy ==
'f' )
{
printf ( "You selected Aero Bubbles" ) ;
price =
0.65 ;
}
else if ( candy ==
'g' )
{
printf ( "You selected Fruit Pastilles" ) ;
price =
0.55 ;
}
else if ( candy ==
'h' )
{
printf ( "You selected Wine Gums" ) ;
price =
0.55 ;
}
else if ( candy ==
'i' )
{
printf ( "You selected Polo mints" ) ;
price =
0.45 ;
}
else if ( candy ==
'j' )
{
printf ( "You selected Haribo Gold Bears" ) ;
price =
0.95 ;
}
else
{
printf ( "Invalid candy selection" ) ;
price =
0.0 ;
}
if ( price >
0 )
{
if ( money >= price
)
{
quantity = money / price;
/* Force down conversion */
printf ( " and have enough money to buy %d bars" , quantity
) ;
}
else printf ( ", but, do not have enough money" ) ;
}
}
Exercise 8 : Vending machine multiple selections
Using the same list and prices from Exercise 6,
write a C program that accepts
3 input values :
first candy selection
second candy selection
amount of money
The program should display a message about
whether the user has enough money to purchase both selections of candy .
Following is a sample output display when the
user inputs are characters c, d and amount 2.00
You have enough money to buy Bounty and Peanut M&M
Following is a sample output display when the
user inputs are characters c, h and amount 1.00
You do not have enough money to buy Bounty and Wine Gums
#include <stdio.h>
int main
( void )
{
char candy, candy1, candy2;
float money, totalCost =
0 ;
scanf
( "%c %c %f" , &candy1, &candy2, &money
) ;
candy = candy1;
while ( candy
)
{
if ( candy ==
'a' ) totalCost +=
0.55 ;
else if ( candy ==
'b' ) totalCost +=
0.55 ;
else if ( candy ==
'c' ) totalCost +=
0.55 ;
else if ( candy ==
'd' ) totalCost +=
0.85 ;
else if ( candy ==
'e' ) totalCost +=
0.85 ;
else if ( candy ==
'f' ) totalCost +=
0.65 ;
else if ( candy ==
'g' ) totalCost +=
0.55 ;
else if ( candy ==
'h' ) totalCost +=
0.55 ;
else if ( candy ==
'i' ) totalCost +=
0.45 ;
else if ( candy ==
'j' ) totalCost +=
0.95 ;
else totalCost =
0 ;
if ( candy == candy1
) candy = candy2;
else break ;
/* exit while loop */
}
if ( money >= totalCost
) printf ( "You have enough money to buy " ) ;
else printf ( "You do not have enough money to buy " ) ;
candy = candy1;
while ( candy
)
{
if ( candy ==
'a' ) printf ( "Mars" ) ;
else if ( candy ==
'b' ) printf ( "Snickers" ) ;
else if ( candy ==
'c' ) printf ( "Bounty" ) ;
else if ( candy ==
'd' ) printf ( "Peanut M&M" ) ;
else if ( candy ==
'e' ) printf ( "Chocolate M&M" ) ;
else if ( candy ==
'f' ) printf ( "Aero Bubbles" ) ;
else if ( candy ==
'g' ) printf ( "Fruit Pastilles" ) ;
else if ( candy ==
'h' ) printf ( "Wine Gums" ) ;
else if ( candy ==
'i' ) printf ( "Polo mints" ) ;
else if ( candy ==
'j' ) printf ( "Haribo Gold Bears" ) ;
if ( candy == candy1
)
{
printf ( " and " ) ;
candy = candy2;
}
else
break ;
/* exit while loop */
}
}
Exercise 9 : Price check
Using the same list and prices from Exercise 6,
write a C program that accepts
an input amount and then use a
switch statement to display
the list of all candy that has the same or lower price than the input amount .
Following is a sample output display when the
user input is 0.50
You have enough money to buy one of the following sweets: Polo mints
Following is a sample output display when the
user input is 0.55
You have enough money to buy one of the following sweets: Mars, Snickers, Bounty, Fruit Pastilles, Wine Gums, Polo mints
Following is a sample output display when the
user input is 0.90
You have enough money to buy one of the following sweets: Mars, Snickers, Bounty, Peanut M&M, Chocolate M&M, Aero Bubbles, Fruit Pastilles, Wine Gums, Polo mints
#include <stdio.h>
int main
( void )
{
float money;
int pennies;
scanf
( "%f" , &money
) ;
pennies = money *
100 ;
printf ( "You have enough money to buy one of the following sweets: " ) ;
if ( pennies >
95 ) pennies =
95 ;
/* The most expensive candy */
switch ( pennies
)
{
case 95 :
printf ( "Haribo Gold Bears, " ) ;
case 94 :
case 93 :
case 92 :
case 91 :
case 90 :
case 89 :
case 88 :
case 87 :
case 86 :
case 85 :
printf ( "Peanut M&M, Chocolate M&M, " ) ;
case 84 :
case 83 :
case 82 :
case 81 :
case 80 :
case 79 :
case 78 :
case 77 :
case 76 :
case 75 :
case 74 :
case 73 :
case 72 :
case 71 :
case 70 :
case 69 :
case 68 :
case 67 :
case 66 :
case 65 :
printf ( "Aero Bubbles, " ) ;
case 64 :
case 63 :
case 62 :
case 61 :
case 60 :
case 59 :
case 58 :
case 57 :
case 56 :
case 55 :
printf ( "Mars, Snickers, Bounty, Fruit Pastilles, Wine Gums, " ) ;
case 54 :
case 53 :
case 52 :
case 51 :
case 50 :
case 49 :
case 48 :
case 47 :
case 46 :
case 45 :
printf ( "Polo mints" ) ;
}
}
Exercise 10 : Vending machine price calculator
Using the same list and prices from Exercise 6,
write a C program that accepts
5 input values :
candy selection 1
candy selection 2
candy selection 3
candy selection 4
candy selection 5
The program must use a
for loop to display a message about
the amount of money required to purchase one of each candy selected by the user.
Following is a sample output display when the
user inputs are characters c, d, e, f, h
The price is £3.45 for Bounty, Peanut M&M, Chocolate M&M, Aero Bubbles, Wine Gums
Following is a sample output display when the
user inputs are characters d, e, f, h, i
The price is £3.35 for Peanut M&M, Chocolate M&M, Aero Bubbles, Wine Gums, Polo Mints
#include <stdio.h>
int main
( void )
{
char candy, candy1, candy2, candy3, candy4, candy5;
float totalCost =
0 ;
int counter;
scanf
( "%c %c %c %c %c" , &candy1, &candy2, &candy3, &candy4, &candy5
) ;
for ( counter =
1 ; counter <=
5 ; counter++
)
{
switch ( counter
)
{
case 1 :
candy = candy1;
break ;
case 2 :
candy = candy2;
break ;
case 3 :
candy = candy3;
break ;
case 4 :
candy = candy4;
break ;
case 5 :
candy = candy5;
}
if ( candy ==
'a' ) totalCost +=
0.55 ;
else if ( candy ==
'b' ) totalCost +=
0.55 ;
else if ( candy ==
'c' ) totalCost +=
0.55 ;
else if ( candy ==
'd' ) totalCost +=
0.85 ;
else if ( candy ==
'e' ) totalCost +=
0.85 ;
else if ( candy ==
'f' ) totalCost +=
0.65 ;
else if ( candy ==
'g' ) totalCost +=
0.55 ;
else if ( candy ==
'h' ) totalCost +=
0.55 ;
else if ( candy ==
'i' ) totalCost +=
0.45 ;
else if ( candy ==
'j' ) totalCost +=
0.95 ;
}
printf ( "The price is £%.2f for " , totalCost
) ;
for ( counter =
1 ; counter <=
5 ; counter++
)
{
switch ( counter
)
{
case 1 :
candy = candy1;
break ;
case 2 :
candy = candy2;
break ;
case 3 :
candy = candy3;
break ;
case 4 :
candy = candy4;
break ;
case 5 :
candy = candy5;
}
if ( counter !=
1 ) printf ( ", " ) ;
if ( candy ==
'a' ) printf ( "Mars" ) ;
else if ( candy ==
'b' ) printf ( "Snickers" ) ;
else if ( candy ==
'c' ) printf ( "Bounty" ) ;
else if ( candy ==
'd' ) printf ( "Peanut M&M" ) ;
else if ( candy ==
'e' ) printf ( "Chocolate M&M" ) ;
else if ( candy ==
'f' ) printf ( "Aero Bubbles" ) ;
else if ( candy ==
'g' ) printf ( "Fruit Pastilles" ) ;
else if ( candy ==
'h' ) printf ( "Wine Gums" ) ;
else if ( candy ==
'i' ) printf ( "Polo mints" ) ;
else if ( candy ==
'j' ) printf ( "Haribo Gold Bears" ) ;
}
}
Use a dry run table to manually calculate the variables involved with iterations and loops. This will help to find logic errors.
Exercise 11 : Vending machine price calculator
Using the same list, prices and requirements from Exercise 10, write a C program that uses a while loop to display a message about the amount of money required to purchase one of each candy selected by the user.
#include <stdio.h>
int main
( void )
{
char candy, candy1, candy2, candy3, candy4, candy5;
float totalCost =
0 ;
int counter;
scanf
( "%c %c %c %c %c" , &candy1, &candy2, &candy3, &candy4, &candy5
) ;
counter =
1 ;
while ( counter <=
5 )
{
switch ( counter++
)
{
case 1 :
candy = candy1;
break ;
case 2 :
candy = candy2;
break ;
case 3 :
candy = candy3;
break ;
case 4 :
candy = candy4;
break ;
case 5 :
candy = candy5;
}
if ( candy ==
'a' ) totalCost +=
0.55 ;
else if ( candy ==
'b' ) totalCost +=
0.55 ;
else if ( candy ==
'c' ) totalCost +=
0.55 ;
else if ( candy ==
'd' ) totalCost +=
0.85 ;
else if ( candy ==
'e' ) totalCost +=
0.85 ;
else if ( candy ==
'f' ) totalCost +=
0.65 ;
else if ( candy ==
'g' ) totalCost +=
0.55 ;
else if ( candy ==
'h' ) totalCost +=
0.55 ;
else if ( candy ==
'i' ) totalCost +=
0.45 ;
else if ( candy ==
'j' ) totalCost +=
0.95 ;
}
printf ( "The price is £%.2f for " , totalCost
) ;
counter =
1 ;
while ( counter <=
5 )
{
if ( counter !=
1 ) printf ( ", " ) ;
switch ( counter++
)
{
case 1 :
candy = candy1;
break ;
case 2 :
candy = candy2;
break ;
case 3 :
candy = candy3;
break ;
case 4 :
candy = candy4;
break ;
case 5 :
candy = candy5;
}
if ( candy ==
'a' ) printf ( "Mars" ) ;
else if ( candy ==
'b' ) printf ( "Snickers" ) ;
else if ( candy ==
'c' ) printf ( "Bounty" ) ;
else if ( candy ==
'd' ) printf ( "Peanut M&M" ) ;
else if ( candy ==
'e' ) printf ( "Chocolate M&M" ) ;
else if ( candy ==
'f' ) printf ( "Aero Bubbles" ) ;
else if ( candy ==
'g' ) printf ( "Fruit Pastilles" ) ;
else if ( candy ==
'h' ) printf ( "Wine Gums" ) ;
else if ( candy ==
'i' ) printf ( "Polo mints" ) ;
else if ( candy ==
'j' ) printf ( "Haribo Gold Bears" ) ;
}
}
Exercise 12 : Vending machine price calculator
Using the same list, prices and requirements from Exercise 10, write a C program that uses a do/while loop to display a message about the amount of money required to purchase one of each candy selected by the user.
#include <stdio.h>
int main
( void )
{
char candy, candy1, candy2, candy3, candy4, candy5;
float totalCost =
0 ;
int counter;
scanf
( "%c %c %c %c %c" , &candy1, &candy2, &candy3, &candy4, &candy5
) ;
counter =
1 ;
do
{
switch ( counter++
)
{
case 1 :
candy = candy1;
break ;
case 2 :
candy = candy2;
break ;
case 3 :
candy = candy3;
break ;
case 4 :
candy = candy4;
break ;
case 5 :
candy = candy5;
}
if ( candy ==
'a' ) totalCost +=
0.55 ;
else if ( candy ==
'b' ) totalCost +=
0.55 ;
else if ( candy ==
'c' ) totalCost +=
0.55 ;
else if ( candy ==
'd' ) totalCost +=
0.85 ;
else if ( candy ==
'e' ) totalCost +=
0.85 ;
else if ( candy ==
'f' ) totalCost +=
0.65 ;
else if ( candy ==
'g' ) totalCost +=
0.55 ;
else if ( candy ==
'h' ) totalCost +=
0.55 ;
else if ( candy ==
'i' ) totalCost +=
0.45 ;
else if ( candy ==
'j' ) totalCost +=
0.95 ;
} while ( counter <=
5 ) ;
printf ( "The price is £%.2f for " , totalCost
) ;
counter =
1 ;
do
{
if ( counter !=
1 ) printf ( ", " ) ;
switch ( counter++
)
{
case 1 :
candy = candy1;
break ;
case 2 :
candy = candy2;
break ;
case 3 :
candy = candy3;
break ;
case 4 :
candy = candy4;
break ;
case 5 :
candy = candy5;
}
if ( candy ==
'a' ) printf ( "Mars" ) ;
else if ( candy ==
'b' ) printf ( "Snickers" ) ;
else if ( candy ==
'c' ) printf ( "Bounty" ) ;
else if ( candy ==
'd' ) printf ( "Peanut M&M" ) ;
else if ( candy ==
'e' ) printf ( "Chocolate M&M" ) ;
else if ( candy ==
'f' ) printf ( "Aero Bubbles" ) ;
else if ( candy ==
'g' ) printf ( "Fruit Pastilles" ) ;
else if ( candy ==
'h' ) printf ( "Wine Gums" ) ;
else if ( candy ==
'i' ) printf ( "Polo mints" ) ;
else if ( candy ==
'j' ) printf ( "Haribo Gold Bears" ) ;
} while ( counter <=
5 ) ;
}
Exercise 13 : Mixed vending selection
Using the same list and prices from Exercise 6,
write a C program that accepts
an input amount and then uses a
sentinel controlled while loop to display a
list of all candy that can be bought for the input amount and the
amount of change remaining .
Following is a sample output display when the
user input is 1.70
You have enough money to buy Mars, Snickers, Bounty with change of £0.05
Following is a sample output display when the
user input is 4.00
You have enough money to buy Mars, Snickers, Bounty, Peanut M&M, Chocolate M&M, Aero Bubbles with change of £0.00
#include <stdio.h>
int main
( void )
{
char candy;
float money;
int pennies;
scanf
( "%f" , &money
) ;
pennies = money *
100 ;
/* convert pounds to pennies */
if ( pennies >=
45 ) /* Need at least 45 pennies to buy any candy */
{
printf ( "You have enough money to buy " ) ;
candy =
'a' ;
while ( ( pennies >=
45 ) &&
( candy <=
'j' ) ) /* pennies is sentinel variable */
{
switch ( candy++
)
{
case 'a' :
if ( pennies >=
55 )
{
printf ( "Mars, " ) ;
pennies = pennies -
55 ;
}
break ;
case 'b' :
if ( pennies >=
55 )
{
printf ( "Snickers, " ) ;
pennies = pennies -
55 ;
}
break ;
case 'c' :
if ( pennies >=
55 )
{
printf ( "Bounty, " ) ;
pennies = pennies -
55 ;
}
break ;
case 'd' :
if ( pennies >=
85 )
{
printf ( "Peanut M&M, " ) ;
pennies = pennies -
85 ;
}
break ;
case 'e' :
if ( pennies >=
85 )
{
printf ( "Chocolate M&M, " ) ;
pennies = pennies -
85 ;
}
break ;
case 'f' :
if ( pennies >=
65 )
{
printf ( "Aero Bubbles, " ) ;
pennies = pennies -
65 ;
}
break ;
case 'g' :
if ( pennies >=
55 )
{
printf ( "Fruit Pastilles, " ) ;
pennies = pennies -
55 ;
}
break ;
case 'h' :
if ( pennies >=
55 )
{
printf ( "Wine Gums, " ) ;
pennies = pennies -
55 ;
}
break ;
case 'i' :
if ( pennies >=
45 )
{
printf ( "Polo mints, " ) ;
pennies = pennies -
45 ;
}
break ;
case 'j' :
if ( pennies >=
95 )
{
printf ( "Haribo Gold Bears " ) ;
pennies = pennies -
95 ;
}
break ;
}
}
money = pennies /
100.0 ;
/* convert pennies back to pounds */
printf ( "with change of £%.2f" , money
) ;
}
else
printf ( "You do not have enough money to buy candy" ) ;
}
Exercise 14 : Vending machine price calculator
Using the same requirements from Exercise 13, write a C program that uses a sentinel controlled for loop to display a list of all candy that can be bought for the input amount and the amount of change remaining .
#include <stdio.h>
int main
( void )
{
char candy;
float money;
int pennies;
scanf
( "%f" , &money
) ;
pennies = money *
100 ;
if ( pennies >=
45 ) /* Need at least 45 pennies to buy any candy */
{
printf ( "You have enough money to buy " ) ;
for ( candy =
'a' ;
( ( pennies >=
45 ) &&
( candy <=
'j' ) ) ; candy++
) /* pennies is sentinel variable */
{
switch ( candy
)
{
case 'a' :
if ( pennies >=
55 )
{
printf ( "Mars, " ) ;
pennies = pennies -
55 ;
}
break ;
case 'b' :
if ( pennies >=
55 )
{
printf ( "Snickers, " ) ;
pennies = pennies -
55 ;
}
break ;
case 'c' :
if ( pennies >=
55 )
{
printf ( "Bounty, " ) ;
pennies = pennies -
55 ;
}
break ;
case 'd' :
if ( pennies >=
85 )
{
printf ( "Peanut M&M, " ) ;
pennies = pennies -
85 ;
}
break ;
case 'e' :
if ( pennies >=
85 )
{
printf ( "Chocolate M&M, " ) ;
pennies = pennies -
85 ;
}
break ;
case 'f' :
if ( pennies >=
65 )
{
printf ( "Aero Bubbles, " ) ;
pennies = pennies -
65 ;
}
break ;
case 'g' :
if ( pennies >=
55 )
{
printf ( "Fruit Pastilles, " ) ;
pennies = pennies -
55 ;
}
break ;
case 'h' :
if ( pennies >=
55 )
{
printf ( "Wine Gums, " ) ;
pennies = pennies -
55 ;
}
break ;
case 'i' :
if ( pennies >=
45 )
{
printf ( "Polo mints, " ) ;
pennies = pennies -
45 ;
}
break ;
case 'j' :
if ( pennies >=
95 )
{
printf ( "Haribo Gold Bears " ) ;
pennies = pennies -
95 ;
}
break ;
}
}
money = pennies /
100.0 ;
printf ( "with change of £%.2f" , money
) ;
}
else
printf ( "You do not have enough money to buy candy" ) ;
}
Optional challenge problem : Tax calculator
Write a C program that accepts as
input a specific amount of income and displays as
output total amount of income tax owed to the UK government.
The income tax rates and information are available from the HM Revenue & Customs website:
#include <stdio.h>
int main
( void )
{
double income;
int pounds;
scanf
( "%lf" , &income
) ;
pounds = income;
/* downward conversion from floating point to integer whole numbers */
if ( pounds <=
6035 ) /* personal allowance amount during 2008-09 */
{
printf ( "You do not owe any taxes" ) ;
return 1 ;
}
else
pounds = pounds -
6035 ;
if ( pounds <=
34800 ) /* income band for 20% basic rate during 2008-09 */
{
printf ( "You owe £%.2lf of taxes" , pounds *
0.20 ) ;
return 1 ;
}
else
pounds = pounds -
34800 ;
/* 40% higher rate during 2008-09 */
printf ( "You owe £%.2lf of taxes" ,
( 34800 *
0.20 ) +
( pounds *
0.40 ) ) ;
}
Optional challenge problem : Multiplication tables
Write a C program that uses iterative loops to display
multiplication tables from 1 to 10 .
Following is a sample output display of multiplication tables for 1 and 2.
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
#include <stdio.h>
int main
( void )
{
int inside, outside;
for ( outside =
1 ; outside <=
10 ; outside++
)
{
for ( inside =
1 ; inside <=
10 ; inside++
)
{
printf ( "%d x %d = %d \n " , outside, inside, outside * inside
) ;
}
}
}