Algorithm that reads 50 numbers, calculates and shows the arithmetic mean, the largest and smallest number

Asked

Viewed 12,649 times

3

I’m a beginner in programming and I came across an algorithm that I need to solve but I’m not getting it.

Read the 50 numbers using the "for i from 1 to 50 knife" I did, now I can’t insert the arithmetic mean into it let alone display the highest and lowest number..

Does anyone have any suggestions?

  • 1

    c#, java, c, c++? Sounds like too many languages to me

  • 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)

1 answer

5

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!

  • Thank you so much for the help! It was very helpful!! And sorry for so many tags.. I know that "css" is not a programming language, it is used on the web, right? However, I needed to put it in order to finish this topic. Forgive me. rsrs. If more. Att. André.

  • mark the answer as right @André.

  • @André CSS is a technology that you use to apply style (bold, font color, placement, sizes etc.) to page elements on the Internet. One day you will mess with the web and you will have to learn CSS, but for now don’t worry about it.

  • @André You just have to put one tag for question to be valid. 5 is the maximum of acceptable tags in a question, not the minimum.

  • 1

    @mgibsonbr is right, the loop was grossly wrong. I will fix!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.