I have written the following code. I hope to improve how it completes the given task, since I think that my code - even if "properly working" - is quite "brute" and it could greatly use some improvement.
The exercise is about Agility (dogs) and it wants me to calculate, for a total of 9 pairs (dog+human), the total penalties (extracted from the probability that they will either refuse to clear an obstacle and/or make a mistake while clearing an obstacle) for each dog, for a total of 20 obstacles. Specifically, a dog cannot refuse to do an obstacle if he has already tried it and made a mistake, nor can he be penalized twice for the same refusal/mistake.
Supposedly there's a special obstacle - the slalom - that circumvents the rules: if a dog makes a mistake while performing the slalom, it can then also refuse to go through it (and this would result in 2 points of penalty). My code does not consider this special obstacle.
(The probability of the dogs refusing/making a mistake is supposed to be 5%)
//Agility
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PAIRS 9
#define OBSTACLES 20
int main(void)
{
int refusal[PAIRS] = {0}, error[PAIRS] = {0};
srand(time(NULL));
//Print header
printf("| %4s | %8s | %6s | %9s |\n", "Pair", "Refusals", "Errors", "Penalties");
for (int i = 0; i < PAIRS; ++i) {
//Calculate refusals
for (int o = 0; o < OBSTACLES; ++o) {
int temp_refusal_2[OBSTACLES];
int temp_refusal = 1 + rand() % 100;
if (temp_refusal > 0 && temp_refusal < 6) {
temp_refusal_2[o] += 1;
if (temp_refusal_2[o] == 1) {
refusal[i] += 1;
}
}else { //Calculate errors
int temp_error_2[OBSTACLES];
int temp_error = 1 + rand() % 100;
if (temp_error > 0 && temp_error < 6) {
temp_error_2[o] += 1;
if (temp_error_2[o] == 1) {
error[i] += 1;
}
}
}
}
//Print summary
printf("| %4d | %8d | %6d | %9d |\n", (i + 1), refusal[i], error[i], (refusal[i] + error[i]));
}
puts("");
return 0;
}