The structure that makes "for i from 1 to 50 do" ... is called loop (in English, loop ). It's practically universal in programming and I believe you should be studying it these days.
You already know you need the loop. The other thing you need is variables that will store the values you need while your program works;)
We'll need three variables for the mathematical part of the thing:
- A variable will store the lowest value found;
- Another variable will store the highest value;
- The third variable will accumulate the sum of all the numbers in the loop. Why do we need it? Because we will use this sum in the algorithm that gets the arithmetic mean:)
Let's create these variables:
int menor;
int maior;
int soma;
I want to give an answer that works in any of the languages that you had originally marked (C #, C ++, Java), so I'll assume you already have an array with fifty numbers. A vector or array is a structure that holds multiple elements in a single variable. Let's call our vector of vetor
and initialize our variables:
menor = vetor[0];
maior = vetor[0];
soma = 0.0;
Note that we are taking the "zest" element of the vector. It is that in all these languages the first position of the vector is position 0. Consequently, if it has 50 elements, the last position is 49. It may seem strange, but it is paradigm. In the future you will understand the meaning of this.
Now, let's read the fifty numbers. In these languages, a loop can be made with the for
statement.
for (int i = 0; i < 50; i++) {
soma += vetor[i];
if (vetor[i] < menor) {
menor = vetor[i];
}
if (vetor[i] > maior) {
maior = vetor[i];
}
}
Note that the for
line has three commands between parentheses. The first command is executed only once before the loop executes. The second command should return a Boolean value and indicate whether the loop should continue or not - if it returns true
, the loop continues, otherwise it stops. The third command is executed at the end of each round of the loop.
And in this way, i
is a temporary variable that we use to indicate a position in the vector. We increase i
with each step to move to the next element.
Note that within the loop itself we have three operations. We are feeding the sum, and also replacing the values of the variables menor
and maior
every time we find a number smaller or larger than what is stored in them.
After the loop ends, we already have the smallest and largest elements in the variables that have these names. Only the arithmetic mean is missing. This average is calculated with the following formula:
(sum of all elements) / (number of elements)
We already have the sum of all elements in the sum variable, so now it's only missing:
double mediaAritmetica = soma / 50;
Just one more thing: variables of type int
always only hold integer values. If you try to save a non-integer value to a variable of type int
, or the variable will discard the non-integer part, or the program will throw an error. Variables of type double
are already made to work with real numbers, which may have non integer parts. That is why I indicated this type for the arithmetic mean. Conversion from int
to double
is usually done automatically (but not reciprocal, ok?).
Good luck!