I need some help writing an inclusive program that computes the sum of the range of a user input. I have an idea of how to get an inclusive program to work, but I can not get the program I wrote to compile.
Here is the problem: write a program that computes the sum of all the integers in a range specified by the user. In addition to the main function, there should be a function that accepts two parameters that specify a lower and upper bound. It should compute the sum of all the integers between those bounds, inclusive, and display that sum. The main function should prompt the user for the two bounds and call that function passing in those values. It should then allow the user to compute another interval sum or to quit.
I was trying to store and add both even and odd numbers. I made a program earlier that added all the even numbers and was thinking this program would be similar.
The mod operator (%) divides two numbers and returns the remainder after that division.
So 11 % 3 would be 2 because 11/3 is 3 remainder 2.
For it to work, you have to give it a number to divide by. num1 % == 0 makes no sense, because the part after the % sign needs to be the number you want to divide by.
If you want to check for odd/even numbers, then you'd want to divide by 2: num1 % 2 == 0
Although there is no reason to check for odd/even numbers here, as whether a number is odd or even has no significance at all for this problem.
For instance lower bound = 2 upper bound = 4 sum = 9.
How do you know the sum is 9? I'm not being a wiseass or anything... I'm trying to get you to think critically.
You have to think about the very basic steps involved. Try to explain them in English. Once you have the basic steps explained... that's your logic. All you have to do from there is convert that logic to code.
So what do you do, mentally, to know that the sum is 9 in this case?
#include <iostream>
usingnamespace std;
void inclusive(int num1, int num2, int sum = 0){ // can I declare sum = 0 in this statement?
// int v = 0; << ---- Works fine when used
while (num1 <= num2){
sum += num1;
num1++;
}
// sum = v;
cout << "Sum = " << sum << endl; //When I don't add an extra variable I get the same number 2130567177
}
int main()
{
int num1, num2, sent1 = 1, sum;
while (sent1 != 0){
cout << "Enter a lower bound integer: ";
cin >> num1;
cout << "Enter an upper bound integer: ";
cin >> num2;
inclusive(num1,num2,sum);
cout << "Enter 0 to quit, 1 to continue: ";
cin >> sent1;
}
return 0;
}
This isn't necessarily about math. This is about logic.
Although your latest attempt seems like it would work. So it looks like you've figured the logic out. Well done. =)
Can you define an integer within a function?
Yes. And you should.
Furthermore, sum is not being used in main at all, so it probably should not be there. There's also no reason for sum to be a parameter:
1 2 3 4 5
void inclusive(int num1, int num2){ // <- sum shouldn't be a parameter
int sum = 0; // <- it's only used in the 'inclusive' function, so make it local to 'inclusive'
//...
}
Thanks for the help. Worked fine after I declared the sum in the int main.
Thanks for the tip about sum in the parameter. I was under the impression that for sum to be, for lack of a better word , a "returned" value it had to be in the parameter.