C ++ | question about ranges

1

I hope you can help me with information to solve this type of problem. They did not give me a topic title or anything like that, only that I tried to solve it.

The problem is to request x, initial number, final number x and then add from the initial number to the initial number.

printf("Introduce el numero inicial");
scanf("%d",&numeroinicial);
printf("Introduce el numero final");
scanf("%d",&numerofinal);

If I enter as initial number 4 and final number 10 the program must do the sum of 4 + 5 + 6 + 7 + 8 + 9 + 10

I wanted to solve it with a for cycle but I do not know how to indicate the sum of the range.

Thank you very much!

    
asked by DOOM 15.10.2018 в 23:16
source

4 answers

2

To add all the numbers between two values, simply multiply the number of values between both numbers by adding the first and the last and dividing by two:

Therefore, the sum of all the numbers between 4 and 10 is:

int números = (7 * (4 + 10)) / 2;

To be taken into account.

  • printf and scanf are the console reading functions of C, in C ++ the stream objects are used: std::cout and std::cin .

Proposal.

int main()
{
    int numeroinicial, numerofinal;

    std::cout << "Introduce el numero inicial";
    std::cin >> numeroinicial;

    std::cout << "Introduce el numero final";
    std::cin >> numerofinal;

    std::cout << "La suma de los valores entre " << numeroinicial
              << " y " << numerofinal
              << " es " << ((numerofinal - numeroinicial + 1) * (numeroinicial + numerofinal)) / 2;

    return 0;
}

Note that the above code assumes that numeroinicial will be less than numerofinal .

If instead of arithmetically you want to do it cyclically, you can use a for loop:

int main()
{
    int numeroinicial, numerofinal;

    std::cout << "Introduce el numero inicial";
    std::cin >> numeroinicial;

    std::cout << "Introduce el numero final";
    std::cin >> numerofinal;

    int suma{};
    for (int valor = numeroinicial; valor < numerofinal; ++valor)
        suma += valor;

    std::cout << "La suma de los valores entre " << numeroinicial
              << " y " << numerofinal
              << " es " << suma;

    return 0;
}

You can also generate ( std::generate ) a collection of numbers within the range and accumulate them ( std::accumulate ):

int main()
{
    int numeroinicial, numerofinal;

    std::cout << "Introduce el numero inicial";
    std::cin >> numeroinicial;

    std::cout << "Introduce el numero final";
    std::cin >> numerofinal;

    std::vector valores(numerofinal - numeroinicial + 1);
    std::generate(valores.begin(), valores.end(), [i = b]() mutable { return i++; });

    std::cout << "La suma de los valores entre " << numeroinicial
              << " y " << numerofinal
              << " es " << std::accumulate(valores.begin(), valores.end(), 0);

    return 0;
}
    
answered by 16.10.2018 / 10:24
source
1

It would be something like this:

//Declaración de variables

int NumInicial = 0, NumFinal = 0, contador = 1;
int Operacion = 0, Operacion2 = 0, SumaTotal = 0;
int Dato = 0;

cout<<"Ingrese Numero Inicial"<<endl;
cin>>NumInicial;
cout<<"Ingrese Numero Final"<<endl;
cin>>NumFinal;
//Guardamos el numero inicial
Dato = NumInicial;
for(int i=NumInicial;i<NumFinal;i++){

    Operacion = i + contador; //Operacion es igual 4 + 1, al volverse a repetir ya seria 4 + 2 y así sucesivamente.

    Operacion2 = Dato + Operacion; //Operacion2 es igual a 4 + 5, al volverse a repetir ya seria 9 + 6 y así sucesivamente.

    SumaTotal = Operacion2; //Aqui SumaTotal toma el resultado para luego mostrarlo.

    Dato = SumaTotal; //La variable Dato toma la suma actual para luego ir sumandole los demás números hasta el NumFinal.

    cout<<"Total:"<<SumaTotal<<endl;

}
    
answered by 16.10.2018 в 01:59
0

In the for loop, the first instance declares a variable 'i', and equals this to the initial variable, in the second, the loop is made while 'i' is less than or equal to the final number, and in the third instance, put the operator i ++, which will make that at the end of the loop add 1 to the indicated variable (i)

for(i=variableNumInicial, i<=variableNumFinal, i++){
código para la suma
}

Remember to set a variable to save the value of each sum to add the new number.

Good luck;)

    
answered by 15.10.2018 в 23:41
0

I have been reading the answers and have found a simpler way to do this exercise:

#include <iostream>

using namespace std;

int main(){

    int numInicial, numFinal, suma = 0;

    cout << "Introduce el numero inicial: ";
    cin >> numInicial;

    cout << "Final: ";
    cin >> numFinal;

    for (int i = numInicial; i <= numFinal; i++){

        suma += i;

    }

    cout << suma << endl;

    return 0;
}

The program will ask you to enter the value for numInicial and numFinal .

What I have done has been to declare three variables: int numInicial; (it will store the initial number), int numFinal; (it will store the final number) and suma = 0; (it will be in charge of storing the sum of the numbers).

In the for loop, I have matched the variable i to numInicial : int i = numInicial; . With this we want the loop to start with the first number we have entered to start the series. The condition of the loop is that the variable i is less than or equal to numFinal , to be able to exit the loop when passing the final number. Within the loop, I have been updating the variable sum , adding the value of i : suma += i; .

When exiting the loop, the program shows the value of the sum.

I hope you have been useful and greetings.

    
answered by 16.10.2018 в 18:55