Sei sulla pagina 1di 16

COMPUTER PROGRAMMING 1

Session 8: Wrapping up for Exam 2

Iván Amaya
CONTENTS
 BREAK AND CONTINUE

 EXAMPLES

 EXERCISES

 EXAMS: 20/06 (MONDAY) – 21/06 (TUESDAY)

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 2


BREAK AND CONTINUE
Can be used to control the execution of loops

break; Exits a loop immediately


continue; Move on to the next iteration of the loop

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 3


EXAMPLES
You are tasked with developing code for handling a display made up of 8
rows and 48 columns. Since the display should print several letters at a
time, you decide to limit each letter to a width of six pixels. Write the
program for printing the uppercase version of the first three letters.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 4


EXAMPLES

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 5


EXAMPLES
Write a program for finding the minimum value among n integers. Read n
from the user and then receive the data.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 6


EXERCISES
1. Modify the first program shown here. Print an indicator that the loop
actually stopped working as soon as the first divisor was found.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 7


EXERCISES
2. Read the ID of a person and his/her brute salary. Should the person earn
over 5000 USD, deduct 10% of the salary. Otherwise, deduct only 5%. The
program should print:

ID, Brute salary, Discount, Net salary

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 8


EXERCISES
3. Read three numbers and print the highest one. There must be a message
stating: “Highest number is: ____”.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 9


EXERCISES
4. Read three numbers and print the highest and lowest ones. There must
be a message stating: “Highest number is: ____”, and another stating:
“Lowest number is: ____”.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 10


EXERCISES
5. You are thinking on opening an account at a bank. There are several
options available and, thus, you would like to have a means of quickly
assessing which one is the best option for you. Specifically, you would like
to know how many months would it take to double an initial investment C,
at a given interest rate. Create a program to sort this problem out, reporting
the value after each month has passed.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 11


EXERCISES
6. After each bounce, a ball losses part of its energy. Assuming an initial
height (H), create a program that simulates each bounce, reporting the
number of bounces until the ball is unable to reach a height equal to one
fifth of the initial value.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 12


EXERCISES
7. You are developing an algorithm for data analysis. Said analysis requires
you to identify the amount of odd and even numbers in the data set.
Knowing that each data set contains a hundred data points, write the code
for carrying out the required analysis.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 13


EXERCISES
8. There are a couple cities (A and B). At the beginning of year 2000, city
A had popA inhabitants whilst city B had 50% more. If each city grows at a
rate ratA and ratB (respectively), create a program that simulates both
growths and report the year in which the population of A becomes higher
than the one of B. The program must print a line containing each year of
the simulation and the population at each city (separated by tab stops).
Remember to create appropriate labels for each column.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 14


EXERCISES
9. A school offers a vast amount of courses and each one has a varied
demand of students. But, due to institutional policy, all classes are graded
throughout four exams. Thus, the board requires you to build a program for
processing the grades of each student in a single course, calculating the
average grade of each student, as well as the number of students who failed
and the average grade achieved by the whole class.

Note: Student lists are ended by including a student with ID and grades
equal to zero. Also, average student grades should be printed with the ID.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 15


EXERCISES
10. The Fibonacci series is a recursive procedure, where the current
number corresponds to the sum of the two previous ones. Since the first
two elements of the series are zero and one, this means that the third
element is also one, followed by two, three, five, eight, and so on. Create a
program that outputs the first n elements in the series, separated by a space.

COMPUTER PROGRAMMING – SESSION 8 IVÁN MAURICIO AMAYA CONTRERAS 16

Potrebbero piacerti anche