Revision task 3 altered solution

The idea behind the algorithm:

  • We need to count how many times each of the numbers in our specified range occurs. This means, that we would need a counter for each of those, however having a separate variable is bad to maintain, especially when you need to increase or decrease the range. This is why we use an array and each of the members is reserved for one of the possible numbers.
  • We don’t need to store the numbers themselves. After we’ve processed the number, we can discard it, since we don’t need to reproduce the order later on. This way we can use only one variable and overwrite the contents after processing it.
  • Changing the range can be done simply by modifying the constant “UNIQUE_NUMBERS”
  • The average rate of occurrence is only about the total numbers entered and total unique numbers. The values themselves don’t affect it.
  • The task solution relies heavily on the limitations set by the task description. This is not a modular solution that can solve any problem – e.g. we limited the amount of unique numbers and the numbers start 0, making it comfortable for us to use a small array to count the rates.
Some explanations for more complex parts:

  • Calculating the average rate is done inside the printf function. We can output and calculate on the same line since the return of an assignment is the assigned value.
  • For the array increment, we’re using the entered number as the index for the array. If the user enters 0, we increase the zeroCount by 1, which for us is stored in the array at the place of 0, instead of having separate variables for each number.
  • Showing the over average is done by using the shorthand if statement [described here]. If the condition evaluates to true, we print the statement where we placed %s in the format for printf. If it’s false, it will print an empty string, not displaying anything.