Docsity

Programming - Lab Session 6: Arrays II (vending machine), Ejercicios de Programación Informática

Toggle sidebar
bg1
Programming Aerospace Engineering, 2020-21
1
Lab 6 (Arrays - II)
Exercise 1. Vending machine
Write a program to manage a vending machine. The program should prompt the user for the price of the
product to be bought, the money provided, and compute the corresponding change. The program should
display how many coins of each type should be returned. The types of coins to be considered are 1, 2, 5, 10, 20
and 50 cents, and 1 and 2 Euros. Assume there are enough coins of all types to give change.
Tips: Do the math in cents!
Exercise 2. Vending machine with limited change
Modify the previous exercise so that the change is computed assuming that there is only a limited number of
coins of each type available. The amount of coins available can be either entered by the user, random or
defined directly in your code (the last option will speed up your testing of the program)
For example, this initialization can be used to set the number of coins of each type available to 2
nCoinsAvailable = [1 0 5 1 0 0 0 100];
Exercise 3. Zebra mussel
In recent years, the zebra mussel is colonizing the Ebro River threatening the local species. To study the process
of colonization, 50 control points have been established. The number of mussels found at each point every
month is recorded.
The objective of this exercise is to develop a program to study the number of mussels found at each of the 50
control points during a maximum of 24 months. The program will display the number of mussels found at each
point and the control point where the maximum number of mussels were found.
Write a MatLab program that:
1. Asks the user to input the number of mussels found at each control point for each month. Each control
point will be identified by a number (1, ... 50). The user must provide this information for at least one
month and a maximum of 24. After reading the information for a month, the program must ask the user
whether he/she wants to continue providing data for one more month or not.
2. Displays the total amount of mussels found at each control point along the study. The program must
print the number of each control point ( 1, 2, ..) followed by the total sum of mussels found at that
point.
3. Displays the total amount of mussels for the control point where the most mussels have been found
(total amount=sum of all months). You must also print the number of that control point.
4. Print in how many months any mussels were found at each control point. That is to say, for each control
point, compute in how many months the number of mussels found is different to 0.
5. Display the total number of specimens found in the study
pf3

Vista previa parcial del texto

¡Descarga Programming - Lab Session 6: Arrays II (vending machine) y más Ejercicios en PDF de Programación Informática solo en Docsity!

Lab 6 (Arrays - II)

Exercise 1. Vending machine

Write a program to manage a vending machine. The program should prompt the user for the price of the

product to be bought, the money provided, and compute the corresponding change. The program should

display how many coins of each type should be returned. The types of coins to be considered are 1, 2, 5, 10, 20

and 50 cents, and 1 and 2 Euros. Assume there are enough coins of all types to give change.

Tips: Do the math in cents!

Exercise 2. Vending machine with limited change

Modify the previous exercise so that the change is computed assuming that there is only a limited number of

coins of each type available. The amount of coins available can be either entered by the user, random or

defined directly in your code (the last option will speed up your testing of the program)

For example, this initialization can be used to set the number of coins of each type available to 2

nCoinsAvailable = [1 0 5 1 0 0 0 100];

Exercise 3. Zebra mussel

In recent years, the zebra mussel is colonizing the Ebro River threatening the local species. To study the process

of colonization, 50 control points have been established. The number of mussels found at each point every

month is recorded.

The objective of this exercise is to develop a program to study the number of mussels found at each of the 50

control points during a maximum of 24 months. The program will display the number of mussels found at each

point and the control point where the maximum number of mussels were found.

Write a MatLab program that:

1. Asks the user to input the number of mussels found at each control point for each month. Each control

point will be identified by a number (1, ... 50). The user must provide this information for at least one

month and a maximum of 24. After reading the information for a month, the program must ask the user

whether he/she wants to continue providing data for one more month or not.

2. Displays the total amount of mussels found at each control point along the study. The program must

print the number of each control point ( 1, 2, ..) followed by the total sum of mussels found at that

point.

3. Displays the total amount of mussels for the control point where the most mussels have been found

(total amount=sum of all months). You must also print the number of that control point.

4. Print in how many months any mussels were found at each control point. That is to say, for each control

point, compute in how many months the number of mussels found is different to 0.

5. Display the total number of specimens found in the study

ADVANCED EXERCISES (Optional)

Exercise 4. Battleship

A videogame company has hired us to develop a simplified version of the Battleship game, according to the

following specification:

a. The board is an 8x8 grid.

b. At the beginning of the game, the coordinates of each ship will be requested to the user. There are 8 ships.

For the sake of simplicity we consider that all the ships occupy only a row (no vertical or diagonal ships), and

that the number of squares for each ship is 2. Therefore, to place a ship on the grid it is enough to provide the

left-most coordinate. Thus, if the coordinate given by the user is (3,4), the boat will be placed at (3,4) and (3,5).

The program must check that the provided coordinate is valid before placing the ship in the board.

c. The game starts once the 8 boats are placed in the board. The game will consist of 8 rounds. In each round

the player provides the coordinates of a target square:

1. If there is a ship on the square, the boat is considered "sunk" and therefore the 2 squares previously

occupied by this boat will be "free" squares in the following rounds. In addition, once a ship is sunk, the

program will give the user a bonus round (i.e., the rounds in which the user sinks a boat will not be

subtracted from the total of available rounds).

2. If the square is free or not valid, the number of left rounds in decreased by one

d. The game ends when all the boats have been sunk or all the rounds have been completed.

e. Once the game is finished, the program will tell whether the player has won or lost. If the player loses the

game, the initial distribution of boats will be printed.

Initial distribution for a possible match. Shaded cells indicate the position of the ships