Guide to Programming Tasks Booklet
-
Upload
bluhbluhllyitkdhfc -
Category
Documents
-
view
227 -
download
0
Transcript of Guide to Programming Tasks Booklet
-
7/27/2019 Guide to Programming Tasks Booklet
1/86
Computing at AGS
An Introduction to Computer Programming
Core Programming Concepts
PROGRAMMING TASKS BOOKLET
NAME: ___________________________________________
Throughout the booklet you will create a program to answer each task.
Write answers down next to the word answer and proe using a procedure you haewritten.
!ou will be asked to check the syntax o" the language.
!ou will declare ariables appropriately.
!ou are allowed to use the #nternet to research some o" the terminology.
1 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
2/86
Task 1 Writing your first program
$pen %isual &tudio and "amiliarise yoursel" with the enironment.
'reate a new console application and sae it in your exercises "older with anappropriate name.
Write a program which displays the text ()ello World* on screen. #t shouldpause so that the message can be iewable by the user.
Add additional lines o" code so that a story is displayed on the screen+ i.e. alittle ditty:
(There was a young man "rom A,&*
()is tie was always a mess*
()e died in the pool+ which was in school*
(and now he-s wet+ what a "ool*
ully comment your code so it is understandable what is going on.
2 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
3/86
Task 2 Practical Problems with Numbers
1. write a program to calculate / 0 1 2 3
2. run program
/. Write out the statements where they are missing.
staticvoidMain(string[] args){
int a 4 56
7eclare the data type "or the "ollowing: b 4 8
c 4 a 0 b
a 4 c 2 a
'onsole.Write9inec;
Insert statement here
b 4 b 2 bInsert statement here
'onsole..
%ariable Name or
-
7/27/2019 Guide to Programming Tasks Booklet
4/86
3. #n each o" the "ollowing+ ealuate the expression where l 4/+ w 41 and h 43)#NT: @$7MA& or @#7MA&;
'alculation 'alculate the Answermanually
or 7oes the programyou-e written agreewith your manualcalculation.
l 2 w; 0 h
5B yes 0 w; 2 h 3B !esl 2 w 0 h; 53 yesl 2 w F h / Nol 0 w 2 h 53 No1 0 h 2 1 5G no
8.
a; 'reate a trace table+ suggesting the alue o" each ariable a"ter each lineo" code is executed. $utput a"ter each statement.
&tatement %alue o"%ariable
x 4 / /
y 4 B B
y 4 1 2 x G
x 4 y 0 8 55
'onsole.'lear ;6
'onsole.Write9inex 0 3;6 58
y 4 y 05
b; 'reate a procedure "or the "ollowing:
HH think o" declaring your ariables.
balance+ interest+ with7raw
balance 4 5BB
interest 4 .B8
with7raw 4 /8
balance 4 balance 0 interest 2 balance
balance 4 balance I with7raw
4 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
5/86
G. Jsing your knowledge o" syntax and declaring ariables. or each o" the"ollowing decide upon the output o" the subIprocedure:
A) Amount as integer
Amount 4 5B
'onsole.Write9ineAmount03;
Answer!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
B) 'onsole.Write9ine5 0 ( ( 0 / 0 ( ( 0 1;6
'onsole.writeline3 0 ( ( 0 8 0 ( ( 0 G;6
'onsole.Write9ine 0 ( ( 0 D0 ( ( 0 C;6
Answer !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
C) Number$ne 4 36
Total 4 8 2 3
console.writelineNumber$ne 0 Total;6
Answer !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
D) owed as integer+ tax as integer
tax 4 /B
owed 4 /BB
owed 4 owed 0 tax
'onsole.Write9ine(!ou owe K* 0 owed;
Answer !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
E) L# as 7$J@9E
L# 4 1.5358
'onsole.Write9ineL#;6
Answer !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""
| P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
6/86
! | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
7/86
Task 3 "eclaring an# Working with Number $ariables
Write a subroutine which per"orms each o" the "ollowing. !ou can use the sameprogram+ and replace the code each time.Write down your output.
A. 'ompany Lro"it 'alculator
- 'reate a ariable to store the reenue o" CD38G
- 'reate a ariable to store costs o" 38BBB
- 'reate a pro"it ariable which stores the result o" reenueIcosts
- 7isplay the alue o" pro"it in the console.
@. &tock Lurchasing &ystem
- 'reate a ariable to store cost per share at /8.G/8
- 'reate a ariable to store the number o" shares at 3BB
- 'reate a total cost ariable which stores the result o" cost per share 2number o" shares
- 7isplay the total cost in the console.
'. 7iscount &ystem
- 'reate a ariable to store the ariable price at 5C.C8
- 'reate a ariable to store the discount at 1B
- 'reate a ariable to store the mark down price discount H 5BB; 2 price.
- 7ecrease the price+ by the mark down price.
- 7isplay the original price and the new price in the console.
% | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
8/86
Task 4 "eclaring an# Working with &onstants
Write out each o" the "ollowing programs in %isual &tudio. !ou can use the sameproect and simply replace the code each time. Write down the result "rom theconsole.
5. %AT is at a constant rate o" /B. The price ex %AT; o" a phone is K/8.88.Write a program which calculates the amount o" %AT payable "or the phone andthe total price o" the phone inclusie o" %AT. @oth o" these alues should bedisplayed in the console.
%AT alue o" phone:
Lrice o" phone:
%AT now raises to //.8 'hange your constant alue and reIrun your code. Whatare the new prices:
%AT alue o" phone:
Lrice o" phone:
/. A gas company sends a letter to its customers once a year explaining howmuch money they hae saed. This is normally 8 o" the gas bill 0 K/B which isthe price o" their competitor;. Jse appropriate constants and ariables towrite a program which calculates this and displays it on screen.
1. Write a program which displays to the user the di""erence between a constantand a ariable. 'ould you een use them to explain how they workO
' | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
9/86
Task 4 (n#erstan#ing &o#e )cti*ities
or each o" the "ollowing procedures+ decide what the output would be. Writeyour answer down.
r as string+ b as string
r 4 (# say*
b 4 ( AN7 (
'onsole.Write9iner 0 b r 0 b 0 r;6
$utput:
35b;uint)ouseNumber as unsigned integer
str&treet as string
uint)ousenumber 4 5/13
str&treet 4 (,reen 9ane*
'onsole.Write9ineuint)ousenumber 0 ( ( 0 str&treet;
$utput:
35c;n as unsigned integer+ x as string
n 4 8
x 4 (8*
'onsole.Write9inen;
'onsole.Write9inex;
$utput:
+ | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
10/86
35d;p as string+ word as string
p 4 ("*
word 4 (lute *
'onsole.Write9inep 0 word;
'onsole.Write9ineucasep 0 word;;
'onsole.Write9inetrimp 0 word;;
'onsole.Write9inestrconp 0 word;;
$utput:
35e;
address as string
address 4 (5/8 ,reen 9ane !arm+ T&58 CET*
console.writeline (@ob+ your address is ( address
$utput:
35";
shopping as "loat
7im supermarket as string
&upermarket 4 (Asda*
&hopping 4 5/1.3B
'onsole.writeline(!ou shopped at ( supermarket;
'onsole.writeline(!ou spent ( 0 shopping.To&tring(KP.PP*;;$utput:
1, | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
11/86
35g;
myName as string
num9etters as integer
num9etters 4 B
myName 4*Type !our Name*
num9etters 4 9enmyName;
console.writeline(!ou hae ( 0 num9etters 0 ( letters in your name*;
$utput:
35h;telNumber as string
spaceLos as integer
spaceLos 4 B
TelNumber 4 (B5G3/ 888/53*
'onsole.writeline(Area code: ( le"ttelNumber+ 8;;
spaceLos 4 instrtelNumber+* (;
'onsole.writeline(Tel No:* midtelNumber+ spaceLos05+ lentelNumber;;;
Output
11 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
12/86
&E'T#$N /: or each o" the "ollowing procedures+ identi"y any errors you see:
7im phone as single
phone 4 (B5G3/ 881C/C*
console.writelinephone;
'orrection:
x as integer+ y as integer
x 4 5B+BBB
y 4 82x
console.writelinex 0 y;
'orrection:
7im ?uote as string
?uote 4 9i"e is like a box o" chocolatesQ
console.writeline?uote ( ( ()anks*;
'orrection:
7im studName as string
studName 4 9$WE
-
7/27/2019 Guide to Programming Tasks Booklet
13/86
end 4*)appily Eer A"ter*
console.writeline(and they all lied* end;
'orrection:
at as integer
at 4 (58.8*
console.writelineat;
'orrection:
13 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
14/86
Task 8 Extended &tring and Number Actiities: #nput and $utput
7$N-T $
-
7/27/2019 Guide to Programming Tasks Booklet
15/86
@. Take your story and see i" you can make i" more e""icient.I What can you oin togetherOI What could you make store in one ariable instead o" multiple onesOI )ow do you make input betterO What could you store in JLLE
-
7/27/2019 Guide to Programming Tasks Booklet
16/86
,. 'alculating Lro"itI Write a program which takes three alues: company name+ income and expenses+and then calculates pro"itincomeIexpenses; and displays eerything on screen+ along with the sentence(The company has made apro"it o"Q*I Jse ALL
-
7/27/2019 Guide to Programming Tasks Booklet
17/86
7$N-T $
-
7/27/2019 Guide to Programming Tasks Booklet
18/86
9. 7ate ormat 7isplayI Write a program which takes a short date 77HMMH!!!!; and conerts it to longdate e.g. // May /BBC;
and displays this on screen.I Also display today-s date using a "unction; and what date it will be in 1Bdays time.
1' | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
19/86
Task G: &tring and Number unctions
-
7/27/2019 Guide to Programming Tasks Booklet
20/86
5B. &?rrt125/;
55.
-
7/27/2019 Guide to Programming Tasks Booklet
21/86
/5. &tr5/1;
//. &tr5C;
or the "ollowing+ determine what the "unction will do when a 4 8+ b 4 1+ c 4(9ullaby* and d 4 (lab*
/1. 9end;
/3. &?rrt30a;
/8. Jcased;
/G.
-
7/27/2019 Guide to Programming Tasks Booklet
22/86
/.
-
7/27/2019 Guide to Programming Tasks Booklet
23/86
'onsole.Write9inenum%ar;
1.
Week 4 (&unMonTueWedThuri&at*
'onsole.Write9ine(Enter a day o" the week 5 to ;*;
str%ar 4 console
-
7/27/2019 Guide to Programming Tasks Booklet
24/86
3/. 'onsole.WritelineormatNumber5/13+5;
31.
Numar 4 round5./138+5;
'onsole.WritelineormatNumbernumar;
33. 'onsole.Writelineormat'urrency5/13.8;
38. 'onsole.Writelineormat'urrency5/138.G+ B;
3G. 'onsole.Writelineormat'urrency3H8;
3. 'onsole.Writelineormat'urrency325B.8+ /;
3D. 'onsole.WritelineormatLercent.B3+ B;
24 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
25/86
3C. 'onsole.WritelineormatLercent5BB;
8B. 'onsole.WritelineormatLercent1H3+ 5;
85. 'onsole.Writelineormat7ateTimeNow 0 5+7ateormat.9ong7ate;
8/. str%ar 4 ormat7ateTimeNow+7ateormat.9ong7ate;
'onsole.Write9ine(The 7ate is ( str%ar;
Task urther Lractice o" #nputs and $utputs
7$N-T $
-
7/27/2019 Guide to Programming Tasks Booklet
26/86
AN&WE
-
7/27/2019 Guide to Programming Tasks Booklet
27/86
Task D Enumerations and $rdered 9ists
7$N-T $
-
7/27/2019 Guide to Programming Tasks Booklet
28/86
Example o" Enumerated 9ist:
Example o" &ets:
2' | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
29/86
Task C: Jser 7e"ined Types
7$N-T $
-
7/27/2019 Guide to Programming Tasks Booklet
30/86
@ookName as string
@ook7ate as string
End &tructure
&ub Main
@ooks.@ookName 4 (The Anthology*
@ooks.@ook7ate 4 (5/H5/H/BBC*
End &ub
&tructure ootball
Team as string
Loints as integer
End &tructure
&ub Main;
7im teamEntry as Team
teamEntry.team 4 (@oro*
teamEntry.points 4 B
End &ub
&ub Main;
&tructure
-
7/27/2019 Guide to Programming Tasks Booklet
31/86
31 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
32/86
Task 5B:
-
7/27/2019 Guide to Programming Tasks Booklet
33/86
X
Number X 5/
9ess Than
Number /1
More than or E?ual To
9ess than or E?ual To
And
$r
Not
#" &core X4 5/ Then
&core 4 &core 0 5
Else
&core 4 &core I5
End #"
33 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
34/86
'onsole.Write9ine&core;
Explain the selection statement gien in "ull below. Also state whatoutput would be gien i" &core 4 53 on one run and 8 on the next.
#" result 3B Then
result 4 ("ail*
Else#" result 8B Then
result 4 (7*
Else#" result GB Then
result 4 ('*
Else#" result B Then
result 4 (@*
Else#" result DB Then
-
7/27/2019 Guide to Programming Tasks Booklet
35/86
Task 55: #denti"ying $utput in &election
or each o" the "ollowing # blocks+ identi"y what would be outputted to theconsole. %ariable declarations hae beenremoed to make the code simpler to understand.
Num 4 3
#" num 4C Then
'onsole.Write9ine(9ess than 5B*;
Else
#" num 4 3 Then
'onsole.Write9ine(E?ual to 3*;
End #"
End #"
$utput:
A 4 8
#" 12 a F 3 C then
'onsole.Write9ine(
-
7/27/2019 Guide to Programming Tasks Booklet
36/86
#" a2 b c Then
@ 4
Else
@ 4 c 2 a
End i"
'onsole.Write9ineb;
$utput:
A 4 console.readline;
@ 4 console.readline;
#" a X b then
A 4 a05
Else
@ 4 @ 05
End i"
'onsole.Write9inea ( ( b;
$utput assuming and 55 areentered;
3! | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
37/86
'onsole.Write9ine(Enter A+ @+ '*;
9etter 4 console.readline;
#" letter 4 (A* then
'onsole.Write9ine(A is letter 5*;
Elsei" letter 4 (@* then
'onsole.Write9ine(@ is letter /*;
Else#" letter 4 ('* then
'onsole.Write9ine(' is letter 1*;
Else
'onsole.Write9ine(#nalid 9etter*;
End #"
$utput:
Num 4 8
#" num B then
'onsole.Write9ine(Negatie*;
Else
#" num 4 B then
'onsole.Write9ine(Vero*;
Else
'onsole.Write9ine(Lositie*;
End #"
3% | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
38/86
End #"
$utput:
or each o" the "ollowing+ identi"y the errors in the code:
Num 4 .8
#" 5 num 1 Then
'onsole.Write9ine(Number between 5 and 1*;
End i"
Num 4 8
#" num X 3
'onsole.Write9ine(More*;
End #"
Word 4 ()ello*
#" ()ello* 4 Word then
'onsole.Write9ine()ello+ )ow are youO*;
End i"
When &core X4 5/ Then
&core 4 &core 0 5
$thwerwise
&core 4 &core I5
End #"
'onsole.Write9ine&core;
&core 4 CB
3' | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
39/86
#" &core 3B then
'onsole.Writeline(ail*;
Else#" &core 3B then
'onsole.Writeline(Lass*;
End #"
Lassword 4 ("ootball*
#" password 4 ("ootball* Then
'onsole.writeline(Welcome*;
Else End #"
3+ | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
40/86
Task 5/: 'oding &election @locks
7$N-T $
-
7/27/2019 Guide to Programming Tasks Booklet
41/86
Thanks "or logging in 'hris.
drpepper//
Welcome back ayne.
bopper//
Nice to see you again essicaS
8. &?uare
-
7/27/2019 Guide to Programming Tasks Booklet
42/86
Task 5/b: &election 9ogical $perators
AN7
$ cities.
citiesB;
cities5;
cities/;
cities1;
cities3;
cities8;
cities
Newcastle
9ondon
Manchester
9eeds
&underland
' | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
59/86
@irmingham
a. Write down the re"erence "or each o" the "ollowing element alues:
9ondon Manchester &underland
b. Write down what each o" the "ollowing array re"erences re"er to:
cities5; cities1; cities8I1;
8. 'reate a program "or each o" the "ollowing scenarios:
A. &tore a list o" 8 students- names. Then display them in order+ then inreerse order hint: step I5;.@. Extend your program in A+ to count the number o" letters in total o" allnames+ and display this on thescreen with a suitable preceding sentence.'. Ask a user "or 8 di""erent string alues. $nce they hae gien you them+display them in a "ull sentenceboth "orwards and backwards.7. Ask a user "or 5B integer numbers and store them. 7isplay them on screenseparated by commas hint:you will need console.write this time;.E. Extend your program in 7+ so that you display the total o" all the numbers+
the aerage o" them all andwhether each number is bigger or smaller than the aerage.
+ | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
60/86
. 'reate the "ollowing two arrays:
studNamesB;
studNames5;
studNames/;
studNames1;
studNames
ess
ohn
'hris
Ericca
stud&coresB;
stud&cores 5;
stud&cores /;
stud&cores 1;
stud&cores
/8
5B
1B
/D
studNames should be a string array and stud&cores should be an integer array.#nitialise them both with
the data gien.
!, | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
61/86
#n the console+ display each persons name and their corresponding test scorenext to them.
,. Extend your program in . 'alculate the aerage score. Then display eachstudents name+ score andwhether they are (A@$%E* or (@E9$W* aerage. Each students details should be onit-s own line.). Extend your program in , een "urther to count how many people are A@$%E and
@E9$W aerage andoutput these two "igures at the end. )#NT: #;#. Write a program which produces the "ull song o" the Twele 7ays o"'hristmas. !ou will need to storeeach day in an array and loop through them. &ome additional in"ormation isproided below.
E.g.
$n the "irst day o" 'hristmas+my true loe sent to me
A partridge in a pear tree.
$n the second day o"'hristmas+my true loe sent to meTwo turtle does+And a partridge in a peartree.
$n the third day o"'hristmas+my true loe sent to meThree rench hens+Two turtle does+And a partridge in a peartree.
B. A partridge in a pear tree.5. Two turtle does+/. Three rench hens+1. our calling birds+3. ie golden rings+8. &ix geese aIlaying+G. &een swans aIswimming+. Eight maids aImilking+
D. Nine ladies dancing+C. Ten lords aIleaping+
!1 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
62/86
5B. Eleen pipers piping+55. Twele drummers drumming
-
7/27/2019 Guide to Programming Tasks Booklet
63/86
. @elow is a list o" "light details.
light Num
$rigin
7estination
7eparture Time
55
7urham Tees %alley
Laris
BDBB
//8
Newcastle
New !ork
B81B
/GG
Manchester
Madrid
5G3/
'reate arrays which stores each column separately in light+ $rigin+
7estination and 7eparture;. Ask theuser "or a "light number. When the "light number is "ound+ display thecorresponding "light detailsnumber+ origin+ destination and departure time;.
)#NT: 9oop through one array-s element contents till you "ind a match with whatis entered+ then use theindex alue which you will store in a ariable; to return the additionalin"ormation in the other arrays.
!3 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
64/86
>. @elow is a distance table between maor J> cities+ stored in a /7 array:7istance
9ondon
Manchester
$x"ord
&he""ield
9ondon
B
5D3
8G
5GB
Manchester
5D3
B
533
1D
$x"ord
8G
533
B
51B
&he""ield
5GB
1D
51B
B
!4 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
65/86
A. 7eclare a two dimensional array and store the alues "rom the aboe arraythe numbers+ N$T thenames;.@. 'reate a 57 array to store the city names.'. 7isplay on the console+ the name o" each city except 9ondon; and the
traelling distance "rom9ondon.
9. 7eclare a new type structure; o" sweets with the "ollowing in"ormation:
&weetName &weetLrice &weetManu"acturer
Then+ declare an array o" your type sweets to store six di""erent types o"sweets. #nitialise it with aluesi.e. G di""erent types o" sweets details;.
7isplay each record in your console by looping through the array.
M. Extend your program in 9+ to calculate the A%E
-
7/27/2019 Guide to Programming Tasks Booklet
66/86
1; 7isplay name and mark "or the person with the largest and lowest mark.
!! | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
67/86
Task 5G: Working with Text iles
7$N-T $
. Jsing a text editor e.g. notepad;+ create the text "ile below.
-
7/27/2019 Guide to Programming Tasks Booklet
68/86
go through the array and change each word to something else. Write the contentsto a 7#E
-
7/27/2019 Guide to Programming Tasks Booklet
69/86
Task 5: Working with Lrocedures unctions
7$N-T $ermit the "rog*;
End &ub
&ub Uuotation;
'onsole.Write(#t isn-t easy beinggreen.*;
End &ub
!+ | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
70/86
$utput:
&ub Main;
'all LianoDD;
End &ub
&ub Lianobyal keys as integer;
'onsole.writelinekeys ( keys on akeyboard*;.
End &ub
$utput:
&ub Main;
'all Lotato5;
'all Lotato/;
'all Lotato1;
'onsole.writeline3;
End &ub
%, | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
71/86
&ub Lotatoby%al pots as integer;
'onsole.writelinepots ( potato*;
End &ub
$utput:
&ub Main;
Num 4
'all AddTwonum;
'onsole.writelinenum;
End &ub
AddTwobyre" num as integer;
Num 4 num 0/
End sub
$utput:
&ub Main;
Num 4
AddTwonum;
'onsole.writelinenum;
End &ub
AddTwobyal num as integer;
Num 4 num 0/
End sub
%1 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
72/86
$utput:
&ub Main;
A 4 8
Total 4 B
Multiplytotal+ a+ ;
'onsole.writelinetotal;
End &ub
Multiplybyre" sum as integerbyal x as integer byal num asinteger;
&um 4 x 2 num
End sub
$utput:
&ub Main;
Num 4 8
'onsole.writelinetriplenum;;
'onsole.writelinenum;
End sub
unction Tripleno as integer;
Triple 4 no 2 1
End unction
%2 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
73/86
$utput:
&J@ H JN'T#$N N$TE&:
%3 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
74/86
Task 5D: Lrogramming with unctions and Lrocedures
5. Write a program which asks a user "or three numbers and then displays themon screen. 7o this with aprocedure./. Write a program which asks "or a name then calculates how many letters there
are in the name and displaysit on screen. Jse separate procedures "or input+ processing and output.1. Write a program which re?uests a number as input and displays three timesthe number. The output shouldbe determined using a "unction. Jse procedures to diide your input andoutputs.3. Write a program which asks "or three numbers and calculates the aerage andtotal. The program shouldthen display these two alues on screen. )#NT: !ou will need to use proceduresand "unctions which pass byre"erence and alue;.8. Write a program to display "our erses o" $ld Mc7onald had a arm. The
primary erse+ with ariablessubstituted "or the animal and sounds should be contained within a procedure.The program should read inthe alues "rom "ile which you must create below;:
Think about:
)ow are you going to store the data arrayO TypeO Arrayo" typeO;
What will be the same in eery erseO
What can you break down into proceduresO
lamb
baa
cow
moo
chainsaw
brrraaap
computer
beep
An example output would be:
($ld Mc7onald had a "arm. Ey eyi oh.
%4 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
75/86
And on his "arm he had a lamb. Ey eyi oh.
With a baa baa here+ and a baa baa there.
)ere a baa+ there a baa+ eerywhere a baa baa.
$ld Mc7onald had a "arm. Eyi eyi oh.*
G. According to Llato+ a man should marry a woman whose age is hal" his ageplus seen years. Write aprogram that re?uests a man-s age as input and gies the ideal age o" his wi"e.Jse a "unction "or theprocessing and procedures to diide inputs and outputs.
. Write a program which conerts a temperature gien in ahrenheit to 'elsius.Jse a "unction to process thisand procedures so diide your inputs and outputs ormula is: 7egreesahrenheit I1/;28HC;
D. Write a program which conerts pounds lbs; to kilograms kg;. Jse anappropriate "unction and procedures.5 >, 4 /./ 9@&;
C. Write a program which asks "or two alues (?uantity* and (symbol.* Theprogram should then pass these toan appropriate procedure to display the symbol+ ?uantity times. E.g. a ?uantityo" 1 and a symbol o" 2 shoulddisplay 222. 7iide your inputs+ processing and outputs.
% | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
76/86
5B. Write a program that takes three alues R ! and &ymbol. The program shoulddisplay a rectangle which is Rby ! using the symbol speci"ied. Jse appropriate procedures. or example i" x 41 and ! 4 3 and symbol 4 2you would get:
222
222
222
222
55. Write a program to re?uest a person-s "irst and second name and displaytheir initials e.g. '.';. Jseappropriate procedures.
5/. Write a program to re?uest the total bill at a restaurant and thencalculate the 58 tip payable. 7isplay theamount o" tip owed and the total bill total 0 tip;.
Lrocedures H unction Notes:
%! | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
77/86
Task 5C: %alidating Lroects
5. Write a program which asks a user "or the number o" hours worked and theirhourly rate. #t should displayhow much gross income they will receie hours x rate;. The program should bealidated and should notcrash )int: T
A2 4 CB 0 A 4 DB I DC @ 4 B F C
' 4 GB F GC 7 4 8B F 8C E 4 3B F 3C J 4 B F 1C
3. Jsing your proect "rom ?uestion 5 in Task 5D+ add alidation to check thateach o" the numbers entered arebetween 5 and 5BB. #" the user enters anything else+ an error should appear.
(Write a program which asks a user "or three numbers and then displays them onscreen. 7o this with aprocedure.*
8. Jsing your proect "rom ?uestion 1 in task 5D+ add alidation to preentmultiplication o" B.
(Write a program which re?uests a number as input and displays three times thenumber. The output shouldbe determined using a "unction. Jse procedures to diide your input andoutputs. (
G. Jsing your proect "rom ?uestion 55 in Task 5D+ display an error i" a userdoes not enter a "irst name or asecond name.
(Write a program to re?uest a person-s "irst and second name and display theirinitials e.g. '.';. Jseappropriate procedures.*
. 'omplete the table below+ identi"ying an example o" each type o" alidationcheck:
%alidation is_______________________________________________________________________________
%% | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
78/86
____
%alidation 'heck
7escription
Example
-
7/27/2019 Guide to Programming Tasks Booklet
79/86
Task /B: Lseudo 'ode Lractice
#t is
-
7/27/2019 Guide to Programming Tasks Booklet
80/86
Task /5: Algorithms: 9inear &earch and @ubble &ort
9inear &earching
B
5
/
1
3
8
&tudent
Name: ane
Mark:/1
Name: osh
Mark 38
Name: 'hris
Mark: 5C
Name: >ate
Mark: G1
Name: @ob
Mark: C/
Name: 'raig
Mark: /8
5. &et up a trace table with the headings (Element&ought* ('urrentElement* and(ound.* Jsing the arrayaboe+ dry run a linear search "or 'hris.
Element&ought
', | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
81/86
'urrentElement
ound
/. &et up another trace table+ as aboe+ this time dry run a linear search "or>ate.
1. 'reate the aboe array in your code. 'onstruct a program which asks a user"or the name. &earch "or it in thearray. 7isplay the position in the array and their test score. )int: you willneed to set up the type "or astudent record "irst;. #" the name is not "ound+ it should display a messagesaying so. Test your program withpeople who exist within it and people who do not. Jse a "unction to search.
3. Write a "unction to per"orm a linear search on a list o" test scoresinteger alues;. The "unction shouldaccept the array and element sought as parameters. The "unction should returnthe index alue o" the item i"it is "ound in the array. #" not it should return I5. An appropriate messageshould be displayed i" it is not"ound. #" it is "ound+ the position should be displayed. Add appropriate errorhandling.
@ubble &orting
5. $n paper "ollow the bubble sort method and sort the "ollowing numbers+showing "ully your working: D+ 8G+38+ 13+ /1+ 5/. )ow many passes did you needO/. $n paper "ollow the bubble sort method and sort the "ollowing numbers+showing "ully your working: CD+ D+G+ GB+ 13+ /8. )ow many passes did you needO1. &et up a trace table with the column headings NoMore&waps and Element andseen columns "or the arrayelements. 7ry run the bubble sort algorithm with the numbers: C8+ 8C+ 3+ 1/+
/+ G3+ /1.3. #n %@+ create an array o" 5B numbers: //+ 81+ /+ G+ 8/+ GC+ D+ 5B+ 8+ 1G.Write a program which puts
'1 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
82/86
them in order using a bubble sort and then displays the order on screen.8. #n %@ write a program which accepts D numbers "rom the user between 5I5BBalidate thisS; and placesthem into an array. Add a bubble sort which then puts them in order anddisplays the ordered output.G. Test your program aboe using di""erent sets o" numbers+ include numberswhich are in reerse order andnumbers which are already in order.. Extend your program in ?uestion 8 to display the number o" passes needed tothe user when displaying theordered list.
'2 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
83/86
Lrogramming Lroects
The proects outlined below are intended to allow you to demonstrate yourprogramming knowledge. !ou shoulduse all your skills in the course to date to produce e""ectie+ e""icientsolutions.
or each proect+ the "ollowing is needed:
5. An outline o" the problem and a description o" the solution./. An algorithm in structured English and Lseudo 'ode.1. A structure chart3. Trace tables where appropriate;8. A "ully commented+ implemented solution using appropriate "unctions andprocedures.G. Appropriate alidation and error handling.. A "ully e""ectie test plan.D. An ealuation o" the problem and a discussion o" how success"ul you were.C. A sel" created text "ile o" data where appropriate;+ to read intoapplications.
Lroect 5: 'ar uel 'ost 'alculator
A petrol pump calculator is needed to determine the cost o" "uel "or a person.A person should enter the price o" the"uel per litre;+ the MilesIperIgallon o" their ehicle and how many litres thetank can hold. The system should thendetermine how many miles the car can trael on a "ull tank+ and the costinoled. This should be displayed onscreen and also written to "ile.
Jse"ul #n"ormation: 5 9itre 4 B./1 ,allons
Lroect /: &tudent Test &core &ystem
A program is re?uired to store test score in"ormation about students theirname "irst and second;+ test score 5+ testscore /+ total+ aerage and grade;. A text "ile should be made to read in alist o" student names and two test scores.The total+ aerage+ and grade should be calculated using the in"ormation below.#n"ormation must then be sortedinto order o" surname and written back to a binary "ile.
A2 4 CB 0 A 4 DB I DC @ 4 B F C
' 4 GB F GC 7 4 8B F 8C E 4 3B F 3C J 4 B F 1C
)#NT: Jser de"ined typesS
Lroect /@: &tudent Test &core &ystem
-
7/27/2019 Guide to Programming Tasks Booklet
84/86
Lroect 1: Lay 'alculation &ystem
A "ile o" employee data "irst name+ surname+ hours worked+ rate o" pay; mustbe read into a system. Lay must becalculated "or each employee and displayed on screen in turn. The new record o"in"ormation should be written to a
'&% "ile.
'4 | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
85/86
@ibliography and Acknowledgements
Z5[ %isual Lrogramming 5 / 9ecture @ond &. 9ang"ield+ /BBD
Z1[ An #ntroduction to Lrogramming Jsing %isual @asic G+ ourth Edition+ 7.&chneider+ 5CCC
' | P a g e
-
7/27/2019 Guide to Programming Tasks Booklet
86/86