C ++ Program that adds and removes the media with FOR

0

I have to introduce the FOR structure and modify this existing code, but no matter how much I turn over the question I can not understand what it refers to.

#include <iostream>
using namespace std;

void main(){
    float Jordi, Pascual, Jose, Carlos, Paco, suma;
    cout << "Jordi, ingresa cuanto dinero tienes \n";
    cin >> Jordi;
    cout << "Pascual, ingresa cuanto dinero tienes \n";
    cin >> Pascual;
    cout << "Jose, ingresa cuanto dinero tienes \n";
    cin >> Jose;
    cout << "Carlos, ingresa cuanto dinero tienes \n";
    cin >> Carlos;
    cout << "Paco, ingresa cuanto dinero tienes \n";
    cin >> Paco;
    suma = Jordi + Pascual + Jose + Carlos + Paco;
    cout << "Teneis " << suma << " euros" << endl;
    system("PAUSE");
}
    
asked by Dan Cezanne Galavan 15.10.2018 в 20:11
source

3 answers

3

It certainly refers to that instead of writing five times the same instruction with different variables, use a loop to get the data in a cyclical way:

#include <iostream>
#include <string>
#include <numeric>

void main()
{
    using namespace std;

    float valor[5]{};
    string nombre[5]{"Jodri", "Pacsual", "Jeso", "Calros", "Poca"}

    for (int indice = 0; indice < 5; ++indice)
    {
        cout << nombre[indice] << ", ingresa cuanto dinero tienes \n";
        cin >> valor[indice];
    }

    auto total = accumulate(begin(valor), end(valor), 0.f);
    cout << "Teneis " << total << " euros, " << (total / 5.f) << " euros de media\n";

    return 0;
}

Other things to consider.

  • The main function must have a return value of type int and return a value at the end of its execution. Read this thread to find out why.
  • There is no obligation to use the using namespace std; clause since it is only an aid to the writing of code; If you decide to use this clause do not do it in the global scope, use it in the smallest possible scope. Read this thread to find out why.
  • Avoid abusing std::endl (because it can cause performance problems) and favors the use of the explicit line break ( \n ). Read this thread to find out why.
answered by 16.10.2018 в 08:20
0

One option I can think of is the following:

float media = 0.0;
float suma = 0.0;
int imax = (la cantidad max que vas a usar)
int cantidad_aportantes = 0;
float dinero = 1.0;
while( dinero != 0) {
cout<<"Ingrese cantidad de dinero"<<endl;
cin>>dinero;
suma = suma + dinero;
cantidad_aportantes++;
}
media = suma / cantidad_aportantes;

The idea is that as users load the amount of money, it is added (var: sum), the amount is counted (var: amount_portant) and at the end of the cycle when you know all the data you can get the average.

Another detail, I assumed that the cut condition is that they enter money == 0, you can use another as a maximum number of iterations or the one that you consider better.

I hope it is useful, any questions we communicate.

    
answered by 16.10.2018 в 01:08
0

The first thing is that I see that you have declared variables for each person, when using the for loop, you only need a variable of type float in this case. In my case, I'm going to call the variable: money . float dinero; This variable will store the money that each person has. Next, we will create another variable of type float called sum : float suma = 0.0f; . Here it is important to equal it to 0 because otherwise, the variable sum will be initialized with the value that is stored in the memory address in which our variable has been saved. With this variable, we will be carrying the sum of all the money.

Before continuing with the code, I give you another tip when it comes to naming variables: EVITA start the first letter of a variable in capital , that is, it is better this int numero; than this int Numero; . The first uppercase letter is usually used when creating classes or methods .

Now yes, we start with the code:

#include <iostream>

using namespace std;

int main(){

    float dinero, suma = 0;

    for (int i = 1; i <= 5; i++){

        cout << "Introduce el dinero que tienes: ";
        cin >> dinero;

        suma += dinero;
    }

    cout << "Teneis " << suma << " euros." << endl;

    return 0;
}

I created the for loop as you can see in the code. In that loop, we start by creating a variable that is responsible for keeping track of the cycle of the loop: int i = 1; . I initialize it to 1 so that the loop repeats 5 times.

Next, we set the condition to exit the loop. In this case it is: i <= 5; . This means that as long as the value of the variable i is less than or equal to 5, the loop will continue to run.

The last thing is to increase the value of the variable i to 1 ( i++ ), otherwise the variable i will always be the initial value and the loop will be Infinite .

Inside the loop, you will ask us to enter the money we have. In the following line of code, the variable sum will store the sum of the value entered in each cycle of the loop: suma += dinero; (is the same as putting suma = suma + dinero; .) But in this case, we repeat suma twice and it's not good to repeat code in programming).

When i is 6, we will exit the loop and the content of the sum variable will be displayed on the screen.

Another thing to mention is that you have to put return 0; at the end of the program, if not, the program will close automatically. You have used system("PAUSE"); . I do not know if you have run the program, but seeing your code, it will not let you compile the program. If you want to use that line I think you have to add another library.

This is all. I have seen that in other answers they have done the example with vectors. I think without vectors, it's simpler and faster to do. Since each one choose how to do this exercise. Greetings and I hope I have helped you!

    
answered by 16.10.2018 в 18:27