Week 05 Extra Exercises

Information

  • This page contains extra exercises for week 05.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • You cannot submit any of these exercises, however autotests are available for them (Command included at bottom of each exercise).

Exercise
(●◌◌)
:

Reverse an array

Write a C program, reverse_array.c, which reads integers line by line, and when it reaches the end of input, prints those integers in reverse order, line by line.

You will never be given more than 100 integers to print out.

Examples

dcc reverse_array.c -o reverse_array
./reverse_array
Enter numbers forwards:
10
50
20
40 

Reversed:
40
20
50
10
./reverse_array
Enter numbers forwards:
-5
-4
-3
-2
-1 

Reversed:
-1
-2
-3
-4
-5

When you think your program is working, you can use autotest to run some simple automated tests:

1091 autotest reverse_array

Exercise
(●●◌)
:

Max Pool Volume

Download pool_max_volume.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/24T3/activities/pool_max_volume/pool_max_volume.c .

The Problem

For the last week, it has been raining continously. You have noticed that with this rain, water has been trapped in uneven parts of the land. You, being the curious person that you are, wish to find which pool of trapped water contains the most volume.

The Program

For this exercise, you have been given some starter code to scan in n numbers into an array. You are to write the function seen at the top of this question

The input provided to the program is the height of each consecutive piece of land that you have measured

Water is said to be trapped if it has two pieces of land to both sides of it (does not need to be directly next to either piece) such that both pieces of land are taller, or the same height as this water

An interactive example has been provided below to show where water is trapped

Number of Elements:
(Max 100)

You can generate arrays above and test with those as the maximum pool can be seen easily in the interactive

Examples

 dcc pool_max_volume.c -o pool_max_volume
 ./pool_max_volume
Please enter heights: 7 2 7 6 4 6 8 2 4 5

Maximum volume of pools: 5
 ./pool_max_volume
Please enter heights: 6 2 5 0 5 0 2 8 3 5 7 13 17 17 14 17 14 17 11 14

Maximum volume of pools: 22
 ./pool_max_volume
Please enter heights: 3 9 1 2 0 6 5 3 5 8

Maximum volume of pools: 34
 ./pool_max_volume
Please enter heights: 8 7 6 5 4 3 2 1

Maximum volume of pools: 0

Assumptions/Restrictions/Clarifications

  • You can assume that no more than 200 inputs will be provided
  • If you picked any height in the array, could you move forward in the array and find the the furthest height that could hold water with the selected height? Could you repeat this for every height?
  • It can be assumed that any land that appears before and after the inputs (E.g. not in the array) have a negative infinite height. This means that pools cannot be formed such that they would continue out of the array

When you think your program is working, you can use autotest to run some simple automated tests:

1091 autotest pool_max_volume

Exercise
(●●●)
:

Max Pool Volume Without Nested Looping

Download pool_max_volume_challenge.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/24T3/activities/pool_max_volume_challenge/pool_max_volume_challenge.c .
For this exercise, you will be completing the exact same question as the previous one. The difference however, is that you cannot use nested loops anymore. This means that you cannot put a loop inside of a loop For example, the below is illegal in this exercise!

Download pool_challenge_banned.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/24T3/activities/pool_max_volume_challenge/pool_challenge_banned.c .

When you think your program is working, you can use autotest to run some simple automated tests:

1091 autotest pool_max_volume_challenge