Home > Mobile >  I want to find the sum of range between two numbers?
I want to find the sum of range between two numbers?

Time:12-22

#include <iostream>
#include <algorithm>
using namespace std; 

void sumrange(int x, int y) {
int sum;
    cout << "Enter the first number: ";
cin >> x;
  cout << "Enter the second number: ";
cin >> y ;



int bnum = max(x,y);
int snum = min(x,y);

int i = snum;
while (i < bnum) { sum =  i; i  ; }

cout << sum;
}

int main() {

int x, y;
sumrange(x,y);

return 0;

} 

I can't get the right answer from my input I tried 1 and 4 and the answer was 3 but it supposes to be 1 2 3 4 which is 10.

CodePudding user response:

You have inverted the = sign to = It should be sum = i . sum = i assigns the number to itself

Edit: you should also initialize sum, stated in the comment

CodePudding user response:

Couple of observations:

  • don't use using namespace std; it is a bad practice and might lead to name conflicts
  • variable int sum needs to be explicitly initialized before use
  • sum = i should be sum = i
  • you need to use <= if you want to include the upper bound
  • take a look at the arithmetic sequence
  • sumrange function doesn't need parameters, sice they are passed by value int x, y; from your main are not used at all

Following code should do what you want:

#include <iostream>
#include <algorithm>

void sumrange()
{
    int x;
    int y;
    int sum = 0;
    std::cout << "Enter the first number: ";
    std::cin >> x;
    std::cout << "Enter the second number: ";
    std::cin >> y ;

    int bnum = std::max(x,y);
    int snum = std::min(x,y);

    int i = snum;
    while (i <= bnum) { sum  = i; i  ; }

    std::cout << sum;
}

int main() {
    sumrange();
    return 0;
} 

CodePudding user response:

There are 3 problems with your given code snippet.

Problem 1: = should be = in the statement:

sum =  i;   //=  should be  =

Problem 2: The variable sum is uninitialized and so it has a garbage value. Using that garbage value leads to undefined behavior.

Problem 3: Should use <= instead of < in the statement

while (i < bnum)  //should be i<=bnum

So the modified(corrected) program looks like below:

#include <iostream>
#include <algorithm>

void sumrange(int x, int y) {
    int sum=0;///INITIALIZE sum
    std::cout << "Enter the first number: ";
    std::cin >> x;
    
    std::cout << "Enter the second number: ";
    std::cin >> y ;



    int bnum = std::max(x,y);
    int snum = std::min(x,y);

    int i = snum;
    while (i <= bnum) //USED <=
    { 
        sum  = i; //USED  =
        i  ;
     }

    std::cout << sum;
}

int main() {

int x, y;
sumrange(x,y);

return 0;

} 

Alternative solution

Note that you can find the sum without using a loop as shown below:

#include <iostream>
#include <algorithm>

void sumrange(int x, int y) {
    int sum=0;//initialize sum
    std::cout << "Enter the first number: ";
    std::cin >> x;
    
    std::cout << "Enter the second number: ";
    std::cin >> y ;

    sum = (std::abs(x - y)   1) * (x   y) / 2; //ADDED THIS 

    std::cout << sum;
}

int main() {

int x, y;
sumrange(x,y);

return 0;

} 

CodePudding user response:

A C 20 solution :

#include <algorithm>
#include <iostream>
#include <numeric>
#include <ranges>

int main()
{
    int a{ 0 };
    int b{ 0 };
    std::cout << "Enter the first number: ";
    std::cin >> a;
    std::cout << "Enter the second number: ";
    std::cin >> b;


    auto lower_bound = std::min(a, b);
    auto upper_bound = std::max(a, b)   1;  // iota view is exclusive so add 1

    auto view = std::ranges::iota_view{ lower_bound , upper_bound };
    auto sum = std::accumulate(view.begin(), view.end(), 0);

    std::cout << "sum = " << sum << "\n";

    return 0;

}

CodePudding user response:

Values of the function arguments are not used in this call

int x, y;
sumrange(x,y);

So such a call does not make a sense.

You need in main to ask the user to enter a range of numbers and pass the range to the function.

The task of the function is to return the calculated sun for the specified range in main.

Within the function sumrange the variable sum is not initialized.

int sum;

You need to initialize it to 0.

Within this while loop

while (i < bnum) { sum =  i; i  ; }
                       ^^

there is a typo. Instead of the compound operator = you are using the assignment operator = and the unary operator

Moreover as it follows from your comment you need also to add the value of bnum to the sum.

So the loop should look at least like

while (i <= bnum) { sum  = i; i  ; }

Pay attention to that in general an overflow can occur. So it is better to declare the variable sum as having the type long long int.

The function and the program can look the following way

#include <iostream>
#include <utility>
#include <algorithm>

long long int range_sum( int x, int y )
{
    long long int sum = 0;

    auto [first, last] = std::minmax( { x, y } );

    do
    {
        sum  = first;
    } while (first   != last);

    return sum;
}

int main()
{
    int x = 0, y = 0;

    std::cout << "Enter the first number: ";
    std::cin >> x;

    std::cout << "Enter the second number: ";
    std::cin >> y;

    std::tie( x, y ) = std::minmax( { x, y } );

    std::cout << "The sum of numbers in the range ["
              << x << ", " << y << "] is equal to "
              << range_sum( x, y )
              << '\n';
}

The program output might look like

Enter the first number: 1
Enter the second number: 10
The sum of numbers in the range [1, 10] is equal to 55

CodePudding user response:

constexpr auto sumrange(const int x, const int y) noexcept {

    auto sum = 0;

    if (x < y) for (auto i = x; i <= y;   i) sum  = i; else 
    if (y < x) for (auto i = y; i <= x;   i) sum  = i;

    return sum;

}

int main() {

    constexpr auto x = 1;
    constexpr auto y = 3;

    constexpr auto r = sumrange(x, y); // 1   2   3

    std::cout << r << '\n'; // 6

}
  • Related