[go: up one dir, main page]

0% found this document useful (0 votes)
72 views2 pages

COMP100: Introduction To Computer Science Practical 5 Preparation For Test 2 and Functions

This document provides instructions and exercises for a computer science practical focusing on random numbers and Fibonacci numbers. It includes 3 problems involving writing programs to generate random numbers within a given range and display rejected numbers. It also includes instructions to rewrite the programs using or without using break/continue statements. Finally, it includes 2 problems involving writing programs related to Fibonacci numbers - one to print the first Fibonacci number not less than a given value, and another to determine if a given number is a Fibonacci number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views2 pages

COMP100: Introduction To Computer Science Practical 5 Preparation For Test 2 and Functions

This document provides instructions and exercises for a computer science practical focusing on random numbers and Fibonacci numbers. It includes 3 problems involving writing programs to generate random numbers within a given range and display rejected numbers. It also includes instructions to rewrite the programs using or without using break/continue statements. Finally, it includes 2 problems involving writing programs related to Fibonacci numbers - one to print the first Fibonacci number not less than a given value, and another to determine if a given number is a Fibonacci number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COMP100: Introduction to Computer Science

Practical 5
Preparation for Test 2 and Functions
24 April 2023
Notes
• You must complete these tasks individually.
• You can use the Math and random module for these exercises if you want.
• Try and make your programs as user friendly as possible. Be creative with your user
interfaces.
• You need to submit solutions to exercises xxx by xxx.
• For the program(s) that you submit
o include comments explaining what your program does.
o Include your first name, surname, student number as a comment in your program.
o Ensure that your program is user friendly – you will get marks for this.
• You should aim to complete all the exercises before 5pm today. It is possible!
• Demonstrators will be available to answer any questions during the Practical (until 17h10)
Please DO NOT use WhatsApp to ask for assistance with your practical.
• Demonstrators will always try and guide you to the answer, but they will not be able to give
you the solution.

1. Random numbers
Problem 1:
Write a program that asks the user for a real number M (20<=M<=100) and integer N
(1<=N<=5). Randomly generate real numbers in the range 5 to 100 until a random number
generated is within N units from M. Use the python built in round function to round the values
to 3 decimal places.

Example 1: Example 2:
Enter a value for M:23.7 Enter a value for M:34.46
Enter a value for N:12 Enter a value for N:3
Value within 11.7 and 35.7 will stop program Value within 31.46 and 37.46 will stop program
73.548 13.333
32.935 85.743
End 51.197
41.583
92.076
46.173
37.758
41.239
40.449
76.104
71.452
96.742
35.948
End
Problem 2:
Write a program that does the same thing as problem 1 except it always generates 10
random numbers. The numbers that do not fall within the range are displayed in the list. The
numbers that were rejected are displayed after the loop has terminated.

Example 1: Example 2:
Enter a value for M:34.7 Enter a value for M:76.3456
Enter a value for N:7 Enter a value for N:20
Values within 27.7 and 41.7 will be rejected Values within 56.346 and 96.346 will be rejected
9.437 42.302
96.738 36.708
88.406 53.586
89.719 46.099
11.092 Rejected: 76.385 83.419 68.461 69.975 87.931 91.496
87.181 End
51.362
70.435
Rejected: 34.809 32.307
End

Problem 3:
If you used break and/or continue in the above, see if you can write programs to achieve the
same thing without these statements. Likewise, if you didn’t use break and/or continue see if
you can write programs to achieve the same results with these statements.

2. Fibonacci numbers

The Fibonacci numbers are F0, F1, ... are defined as follows
F0 = 0 F1 = 1 Fn+2 = Fn+1 +Fn for all n ≥ 0.

Thus, the first 8 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, ....


a) Write a program that reads a positive integer M and prints the first Fibonacci number
which is not less than M.
b) Write a program which reads an integer N and determines whether or not it is a
Fibonacci number.

FIN

You might also like