Chapter 7 Conclusions and Future Work
2. Conditional Problems
1. A bus company has the following charges for a tour. If a person buys less than 5 tickets they cost $1 each, otherwise they cost 0.75. Write a program which calculates a customers bill given a number of tickets as well as data.
2. Write program that takes as input a positive integer and outputs a message indicating whether the number is even or odd.
3. Write a program which takes as input real values a, b, and c for a quadratic ax2 +bx + c and returns the message "real roots" or "no real roots".
4. Write a program which takes a real value as input and outputs the inverse, square root and square of the value. If the value entered is zero an error message must be displayed when calculating the inverse. Similarly, if the value entered is negative, an error message must be returned when calculating the square root.
5. A certain city classifies a pollution index less than 35 as "pleasant", 35 through 60 as
"unpleasant", and above 60 as "hazardous". Write a method that returns the appropriate classification for a pollution index.
6. Write a program that takes in three marks and outputs a message indicating whether the student has passed or not. A student has passed if he/she has obtained at least 50% for all three marks otherwise the student has failed.
7. Write a program that takes two positive integer numbers as input and displays a message indicating whether the first number is a multiple of the second number or not.
8. Write a program which takes an integer representing a day of the week as input and outputs the day of the week, e.g. an input of 1 results in Monday being displayed.
9. Write a program which takes a single character as input and outputs a message indicating whether the character is a vowel or consonant.
10. A mail order house sells five different products whose retail prices are shown in the following table:
Product Number Retail Price
1 $2.98
2 $4.50
3 $9.98
4 $4.49
5 $6.87
Write a program that reads a pair of numbers: product number and quantity sold. Your program should use a switch statement to help determine the retail price for each product.
Your program should calculate and display the total sales for a particular product.
3. Iterative Problems
1. Write a program that calculates and outputs the sum of a list of numbers entered by the user.
2. Find the largest number in a sequence of integers.
3. Write a program that takes in two integers m and n with m<=n and outputs the sum ofthe square of the numbers from m to n.
4. Write a program which calculates the factorial of some nonnegative integer N.
5. Given a list of numbers, calculate the average of the numbers.
6. Write a program that inputs a number of single digits and combines them into an integer value. The user will enter a negative number to indicate the end of the digits. For example, inputting
2 3 4 -1
should result in the program outputting 234.
7. Write a program to calculate and display the sum of the sequence
1 1 1 1
"2
+"3 +"4+····+N .8. A piece of text is terminated by the special character #. Write a program to count the number of spaces which occur in the text.
9. Write a program that reads in a string in and converts the string lower case.
10. Given an integer number x and a nonnegative integer n, use a loop to calculate X' and output the answer.
4. ASCII Graphics Problems
1. (Language caters for "gotoxy-type" commands): Write program to print the following two-toned square:
+--- ++-- +++- ++++
2. (Language caters for "gotoxy-type" commands): Write a program that takes a nonzero, positive integer, size, and displays a square of asterisks of the specified size on the screen.
For example if the user enters 3 for the size and the following square should be displayed on the screen:
***
** *
***
3. (Language caters for "gotoxy-type" commands): Write a program that takes a non-zero, positive integer, indicating the size of the triangle, as input. The program must display an inverted right-angled triangle of asterisks of the specified size on the screen. For example, if the user enters a size of 3 the following triangle must be displayed:
** *
**
*
4. (Language caters for "gotoxy-type" commands): Write a program that takes a non-zero, positive integer value as input and displays a right-angled number triangle on the screen.
For example, if the user enters a value of 3 the following triangle must be displayed on the screen.
1 22 333
5. (Language caters for "gotoxy-type" commands): Write a program that takes a non-zero, positive integer value as input and displays a right-angled number triangle on the screen.
For example, if the user enters a value of 3 the following triangle must be displayed on the screen.
1 22 333
6. (Language that does not cater for "gotoxy-type" commands): Write program to print the following two-toned square:
+--- ++-- +++- ++++
7. (Language that does not cater for "gotoxy-type" commands): Write a program that takes a non zero, positive integer, size, and displays a square of asterisks of the specified size on the screen. For example if the user enters 3 for the size and the following square should be displayed on the screen:
***
***
***
8. (Language that does not cater for "gotoxy-type" commands): Write a program that takes a non-zero, positive integer, indicating the size of the triangle, as input. The program must display an inverted right-angled triangle of asterisks of the specified size on the screen. For example, if the user enters a size of 3 the following triangle must be displayed:
***
**
*
9. (Language that does not cater for "gotoxy-type" commands): Write a program that takes a non-zero, positive integer value as input and displays a right-angled number triangle on the screen. For example, if the user enters a value of 3 the following triangle must be displayed on the screen.
1 22 333
10. (Language that does not cater for "gotoxy-type" commands): Write a program that takes a non-zero, positive integer value as input and displays a right-angled number triangle on the screen. For example, if the user enters a value of 3 the following triangle must be displayed on the screen.
1 22 333
5. Recursive Problems
1. Write a recursive function to calculate the factorial of a non-negative number.
2. Write a recursive function to calculate the length of a given string. Each string is terminated by a hash. The string may also contain spaces. A space is represented by a tilde.
3. Write a recursive function that outputs the Nth element of the Fibonacci sequence given N.
4. The highest common factor HeF of two non-negative integers M and N can be defined as follows: If M is equal to N then the highest common factor is M or N. If M is less than N then the highest common factor is the highest common factor of M and N minus M.
However, ifN is less than M then the highest common factor is the highest common factor of N and M minus N. Write a recursive function to calculate the highest common factor of two non-negative integers.
5. Write a recursive function which calculates the sum of the numbers in the range one to the given positive integer N. For example, if N is 4 the function must output 10.
Appendix B - Running Simulations
This appendix provides details on how to run simulations. Consult Appendix C to get an approximation of how long each simulation will take to find a solution.
1. System Requirements Windows 2000 or Windows XP The D: drive is the CD drive.
Screen resolution: 1024 by 768 pixels (see Figure B.1.1) Color quality: Highest (32 bit) (see Figure B.1.1)
Displ,'y PI opertirs (1]~
Drag the rroor-Otor icons to match the physicalarrangemenl 01 your monitors.
Screen lesoLOOn
Less
' --0--
More1024 by 768 pOrels I:e th de, ... e.:t tf- l.. In ,I'
ldentiy
11
TrotbleshooL11
AdvancedI
r
OK1 1
CancelI
A~~lyFigure B.1.1: Screen Resolution and Colour
2. Running the Program
•
Double click on the batch file D:\gp.bat on the CD. The file is denoted by the icon:'
~ gp
The following panel illustrated in Figure B.2.1 will be displayed .
)t
x y\W ~ 'f ~ ~~ tor 1:
33322
5eqeta Problems Condilimal PrOblems .er.M! Problems Recu ... Problems ASCH ~cs Problems
Figure B.2.1: GP Panel
3. Running Simulations
Click on the icon in the GP Panel (see Figure B.2.1) representing the category of the problem you wish to run a simulation for. For example, if you wish to run a simulation for an iterative problem click: ~{or
~
A dialog box, similar to that displayed in Figure B.3.1, will be displayed for the particular problem category.
•
~ Iteriltive Problems ~I - @
Problem oomber:
1034007003030 • 1008351890570 1026110679330 Seed: 1027029949760 1023430196170
6 1025834715630
7 ~ 102611 0735850 ~
Figure B.3.1: Problem Category Dialog Box
Click on the number ofthe problem you wish to run a simulation for (see Appendix A).
Click on the seed that you wish to run a simulation for.
Click the Run button ~ .
The Working Directory box in Figure B.3.2 will be displayed.
I!I
VJorkine Directoryr:l ""
~ID:JprOblemsl
Figure B.3.2: Working Directory Dialog Box
This box displays the directory in which the problem specification, genetic programming parameters and local optima parameters for each problem are stored. To run simulations off the CD this should be D:/problems. If the correct directory is displayed click the OK button
I Mm- .
If the incorrect directory is displayed, type in the correct directory and click on the OK button. Alternatively, you can click on the Browse buttonl-!!i!W* and choose the correct directory from the Directory dialog box illustrated in Figure B.3.3.
r!!! S"I Dimclory
G1 Q 1 rgJ
Look!n: H@c:I ... 1 ~
fiJ~ [ID
D·o." .!;]
~D ai2003
.
c:Jcd ~
!Cl clips c:J CB-IT Cl cs1x2003
c:J Oalabases
,.-; "'"
.
FHe!!lIIlIe:
I
11 QpenI
FHes Of !)Ipe: 1 AI Files ('.')
"' 11
ClIIICeII
Figure B.3.3: Directory Dialog Box
The Simulation box in Figure B.3.4 will be displayed on the screen.
'r Sillllll •• lioll Se .. rl.103400/003030 r;] - [?iJ
ITERATIVE PROBLEM NO. 2
Figure B.3.4: Simulation Dialog Box
This box indicates the progress of the simulation and will remain on the screen until the end of the simulation. If you wish to stop the simulation at any point click on the Close button
m
of thewindow. Please note that in some cases it may take a minute or two to for the simulation to stop as the current operation (i.e. crossover, mutation, or evaluation of an individual)being performed by the system will be completed prior to the halting of the simulation.
Once the simulation is complete a Solution dialog box will be displayed. If a solution has been found a box similar to that illustrated in Figure B.3.5 will be displayed.
m
Soluliflllr:l -
~Start time: 19: 13: 33 Fin time: 19: 13: 38
Generation: 0, Run No.: 1
fuel: (sqrt«F . F) • F) + L) / F - S
_ " 'CM
Figure B.3.5: Solution Dialog Box
If a solution is not found a box similar to that illustrated in Figure B.3.6 will be displayed. If you wish to view the genetic programming parameters and local optima parameters used and the problem specification click on the Details button
The System Parameters box similar to that depicted in Figure B.3.7 will be displayed. Click on the tab of the parameters that you wish to view. To close the System Parameters box click on the Close button
m .
(g SollllilJll