Hello
I passed to functions and I have the next exercise.
you are expected to 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.

A sample dialog for this program looks as follows:

Enter a lower and upper bound: 2 4
Sum = 9
Enter 0 to quit, 1 to continue: 0
I have written this

``````#include<iostream>
using namespace std;
void sum ( int value, int& count);
int main()
{
int value1, value2,i,sum=0;
cout<<"Enter a lower and upper";
cin>>value1>>value2;

system("pause");
return 0;
}
but I do not know how to determine how many numbers are between the start and the end value.``````
3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by eduard77

You don't really have to compute the number of numbers...

``````while (lower <= higher)
{
//do something
++lower;
}``````

Edited by gerard4143: n/a

Yes but I am supposed to use functions

``````int range(int lower, int higher, int sum=0)
{

while (lower <= higher)
{
//do something
++lower;
}

return sum;
}``````

Edited by Akill10: n/a

I wrote this and the result is a huge value

``````#include<iostream>
using namespace std;
int range(int lower, int higher, int sum=0);
int main ()
{
int lower, higher,sum=0;
cout<< " Enter lower and higher ";
cin>>lower>>higher;
while (lower <= higher)
{
//do something
++lower;

sum = lower + higher;
cout<<sum;
}
system("pause");

return sum;
}``````

I would try sum += lower...

``````int range(int lower, int higher, int sum=0)
{

while (lower <= higher)
{
sum += lower;
++lower;
}

return sum;
}``````

Edited by gerard4143: n/a

I wrote this and the result is a huge value

``````#include<iostream>
using namespace std;
int range(int lower, int higher, int sum=0);
int main ()
{
int lower, higher,sum=0;
cout<< " Enter lower and higher ";
cin>>lower>>higher;
while (lower <= higher)
{
//do something
++lower;

sum = lower + higher;
cout<<sum;
}
system("pause");

return sum;
}``````

Where have you written the range method? All I see is the prototype
You should have the method as gerard4143 has written. Then you should pass the values from the user into the method.
e.g.

``````int input1, input2;
cout<< " Enter lower and higher ";
cin>>input1>>input2;

range(input1,input2);

int range(int lower, int higher, int sum=0)
{..//etc``````

Edited by Akill10: n/a

OK
Thanks