The structure that makes "for i of 1 to 50 make"... is called loop (in English, loop). It’s something practically universal in programming and I believe you should be studying it these days.
You already know you need the bow. The other thing you need is variables that will store the values you need while your program works ;)
We’re gonna need three variables for the mathematical part of the thing:
- A variable will store the smallest 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’re going to use that sum in the algorithm that gets the arithmetic mean :)
Let’s create these variables:
int menor;
int maior;
int soma;
I intend to give an answer that works in any of the languages you originally marked (C#, C++, Java), so I’m going to assume you already have an array (array) with fifty numbers. One vector or array is a structure that holds multiple elements in a single variable. Let’s call our vector vetor
and initialize our variables:
menor = vetor[0];
maior = vetor[0];
soma = 0.0;
Note that we are taking the "zero" element of the vector. In all these languages, the first position of the vector is position 0. Therefore, if it has 50 elements, the last position is position 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 instruction for
.
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 line of for
has three commands between parentheses. The first command is executed only once before the loop is executed. The second command must 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 loop round.
And for that, i
is a temporary variable that we use to indicate a position on the vector. We increment i
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 greater than what is stored in them.
After the loop ends, we already have the smallest and largest element in the variables that have these names. Only the arithmetic mean is missing. That average shall be calculated with the following formula::
(sum of all elements) / (number of elements)
We already have the sum of all the elements in the sum variable, so now we only need:
double mediaAritmetica = soma / 50;
Just one more thing: variables like int
always keep only integer values. If you try to store a non-interous value in a type variable int
, or the variable will discard the non-integrated part, or the program will throw an error. Already variables of type double
are made to work with real numbers, which can have non-whole parts. That’s the only reason I’ve indicated this type for arithmetic mean. The conversion of int
for double
is usually done automatically (but not reciprocally, ok?).
Good luck!
c#, java, c, c++? Sounds like too many languages to me
– pmg
how about inpython__author__ = 'Andremart' numeroInsert = int(input("Type an n for it and its next 50 consecutive n with its average ")) Indice = numeroInsert + 50 accumulator = 0 while numeroInsert < Indice: accumulator = numeroInsert + 1 numeroInsert = numeroInsert + 1 accumulator = accumulator / 50 print("Your average is %d"%accumulator)
– André Martins