Sunday, October 21, 2018

How to use loop to analyze large data sets?

If we have a set of data {1, 3, 5, 6, 10, 12}, this should be no problem to analyze.
We may sum it up to get their total, or calculate their average , get their maximum, minimum or mode during one glance.

However, what about we have datasets with 100 data, or up to 1000 or even much more?
Can we calculate the amount manually?

That is why we use computer to solve our daily problem.
We need to shorten our time to analyze large amount of data.

In order to analyze the data, we use loop to access the data one by one.
For sure, we need stopping criteria to tell the compiler when it should stop the execution,
else we might get run time error (the computer keep repeat the steps and do not know when to stop).

I will be demonstrating how loop helps us by using the sample question from Australian Informatics Olympiad programme sample questions. You may registered and get the training from their training site here Australian Mathematics Trust.

According to the question, we need to access the input data from 'rainin.txt' and output the result to 'rainout.txt'. From the input file, the first line give number of days n and capacity of water tanks in liters c. The remaining n lines shows the amount of rain in litres for the n days.

Here comes the question : What is the number of days for the tank to be filled up?

In this question, you do not need to check or validate the data. The main purpose of the program is to calculate number of days to fill up the tank instead of doing validation on the input data.

So, please refer to the pseudocode below for the steps.

Pseudocode:
1.  Open the input file and assign the data to a list. Remember to close the file.
2.  Obtain number of days n, capacity of water tank c from the first element of the list.
3.  Delete the first element of the list, since we do not need it in the list anymore. We had assigned the data of n and c in variables previously
4. I will be using the for loop to access all the element in the list.
5. During each loop, I sum up the water filled in the tank. I increment the number of days need to fill up the tank.
6. Then, I check if the total amount of water filled in the tank reach or more than the capacity. If so break and exit the loop, else repeat the steps until we get the desired amount of water.
7. Finally, open a output file and write the output string to the file. Close the file.

See, the question might seems difficult and confusing intitially. It become easier when we understand the requirement and break down to smaller part. We might get the result.

The next item we need to worry about is the syntax. No worry, there are resources regarding the language syntax online and it is free! Practice makes perfect. Please do refer the link below for the solution

How to solve drought problem

No comments:

Post a Comment

Superior of partial fraction

What is complex fraction? Complex fraction is a fraction whereby its numerator or denominator or both consists of fractions. Partial frac...