Task specification
Create a UML activity diagram for the following task:
- User enters N integers.
- Find and store all positive integers in a separate array and output them.
Discussion
Q1: Where does N come from?
In this task, N is an unspecified size constant. It describes the amount of numbers for which the solution must work with. The importance of this size depends with the task – it may be important to ask for more clear specifications of what the N can be (e.g. lower and upper bounds, common size rage).
For this task, there are no issues of handling N of any size so we can easily leave it as an unknown and make our solution applicable for any N. If you wish to write this application as a test program in C, pick a small integer value for N.
Q2: How many arrays are needed to solve the task?
The task can be solved with either 1 or 2 arrays. Both solutions have advantages and disadvantages.
Single array solution would be more memory efficient and possibly quicker, but it’s also specialized so functionality becomes limited. I.e. you would not be able to reproduce to entered numbers or reuse the solution as a part of another program.
When solving it with 2 arrays, we can reproduce the entire input sequence, however this requires us to keep a second array in memory, likely doubling our resource usage.
K3: What are the lengths of the arrays?
Since this is purely an algorithmic description of the solution, we do not need to specify any length. Our solution will work for any N. However if you wish to write it as similarly as possible to C, you need to define the value for N. The length of the array would be the same for both arrays, since in the worst case scenario, all numbers will be positive.
K4: Is 0 considered a positive integer?
This is again a question that should always be discussed with the client. Edge cases are important and this is one of the most common critical edge cases that may break systems.
In this case, we will consider anything higher than 0 to be positive.
Sample solution with 1 array
The sample provided is not the only available correct solution. I.e.
- When reading the array, the size of it can be fixed or not. The solution should not be programming language dependent and it’s the task of the developer how to implement the solution. It is important that the algorithm itself is clear and understood the same way by everyone.
- The solution uses a counter i, which can be omitted entirely and replaced by text descriptions. Similarly the limits can be omitted and replaced by text.
It is important to show indexing of the arrays in the programming 1 and 2 subjects!
Sample solution with 2 arrays
In the followiing example, we have separated data entry from processing, making it a bit more of a classical separation of tasks (input-proccessing-output). it gives the solution better modularity, allowing parts of it to be reused, however it increases the resource usage.