Hello everyone,
I am writing a function to find a sum of a range. I have a function that works in the main function, but I cannot figure how to call the function in main.
#include <iostream>
usingnamespace std;
int sumOfRange(int, int);
int main() {
int sumOfRange(int firstName, int lastName);
return ;
} // End of main
int sumOfRange(int firstNum, int lastNum) {
int Sum = 0;
int First, Last;
cout << "Enter the first: ";
cin >> First;
cout << "Enter the last: ";
cin >> Last;
if (First > Last)
{
int Temp;
Temp = First;
First = Last;
Last = Temp;
}
for (int Counter = First; Counter <= Last; Counter++)
Sum += Counter;
cout << Sum << endl;
return 0;
}
Hello, I am just a beginner too, so what I say might very well be wrong, but I think your problem might be that you are not declaring your paramters ofsumOfRrange before you pass them. I have your program working as this
#include <iostream>
usingnamespace std;
int sumOfRange(int, int);
int main() {
int firstName;
int lastName;
sumOfRange(firstName, lastName);
return 0;
} // End of main
int sumOfRange(int firstNum, int lastNum) {
int Sum = 0;
int First, Last;
cout << "Enter the first: ";
cin >> First;
cout << "Enter the last: ";
cin >> Last;
if (First > Last)
{
int Temp;
Temp = First;
First = Last;
Last = Temp;
}
for (int Counter = First; Counter <= Last; Counter++)
Sum += Counter;
cout << Sum << endl;
return 0;
}
Yep you're right. By declaring the variables inside the [what would be a] function call, it actually makes it a function declaration: int sumOfRange(int firstName, int lastName); declares a function (redundant because it's already declared on line 5) but does not call it.
#include <iostream>
usingnamespace std;
int sumOfRange(int, int); // <--- The variable names may not be required, but they do help.
int main()
{
int firstName; // <--- What does Name have to d with a numeric variable?
int lastName; // <--- Uninitialized and contain garbage values.
sumOfRange(firstName, lastName); // <--- Uninitialized variables containing garbage values. Also an error with
// my compiler. Gives a warning in the shell program.
return 0; // <--- Not required, but makes a good break point.
} // End of main
// <--- This should be a void vunction.
int sumOfRange(int firstNum, int lastNum) // <--- Variables received and with a better name.
// Still contain garbage values and never used int the function.
{
int Sum = 0;
int First, Last;
cout << "Enter the first: ";
cin >> First;
cout << "Enter the last: ";
cin >> Last;
if (First > Last)
{
int Temp;
Temp = First;
First = Last;
Last = Temp;
}
for (int Counter = First; Counter <= Last; Counter++) // <--- Loops 1 more time with the "<=". Is that what you want?
Sum += Counter; // <--- Sum may be greater than what you expect.
cout << Sum << endl;
return 0; // <--- This is not needed as there is nothing to return. And if
// you make it a void function you can not return a value.
}
Thank you both, it will run in the stacks on this website, but will not run in visual studio because it says there are unused variables on line 8 and i have no clue what is going on.
#include <iostream>
usingnamespace std;
int sumOfRange(int firstNum, int lastNum);
int main() {
int firstNum, lastNum;
cout << sumOfRange(firstNum, lastNum);
/*int sumOfRange(int firstNum, int lastNum);*/
} // End of main
int sumOfRange(int firstNum, int lastNum) {
int Sum = 0;
int First, Last;
cout << "Enter the first: ";
cin >> First;
cout << "Enter the last: ";
cin >> Last;
if (First > Last) {
int Temp;
Temp = First;
First = Last;
Last = Temp;
}
for (int Counter = First; Counter < Last; Counter++)
Sum += Counter;
cout << Sum << endl;
return 0;
}
#include <iostream>
void sumOfRange();
int main()
{
sumOfRange();
}
void sumOfRange()
{
std::cout << "Enter the first: ";
int first;
std::cin >> first;
std::cout << "Enter the last: ";
int last;
std::cin >> last;
if (first > last)
{
int temp = first;
first = last;
last = temp;
}
int sum { };
for (int counter = first; counter <= last; counter++) sum += counter;
std::cout << "\nThe sum of " << first << " to " << last << " is: " << sum << '\n';
}
Enter the first: 1
Enter the last: 100
The sum of 1 to 100 is: 5050
but will not run in visual studio because it says there are unused variables on line 8 and i have no clue what is going on.
Strange my VS 2017 says:
Severity Code Description Project File Line
Error C4700 uninitialized local variable 'lastNum' used main v2.cpp 11
If you did not understand the comments I put in the code that I posted just ask.
In "main" you are defining 2 variables "firstNum" and "lastNum", but they have no value just what happens to be in the memory they use for storage. Fo me this usually turns into an "int" of "-858993460" or known as garbage. Then when you try to call the function with out giving "firstNum" and "lastNum" a proper value the compiler complains.
int sumOfRange(int firstNum, int lastNum) {
int Sum = 0;
int First, Last;
cout << "Enter the first: ";
cin >> First;
cout << "Enter the last: ";
cin >> Last;
if (First > Last) {
int Temp;
Temp = First;
First = Last;
Last = Temp;
}
for (int Counter = First; Counter < Last; Counter++)
Sum += Counter;
cout << Sum << endl;
return 0;
}
You have sent "firstNum" and "lastNum" to your function, but where in this code do you use them.
The formula foe the sum of the range of consecutive integer numbers is given as
(n - m + 1) * (m + n) / 2
where m is the first number and n the last. This is derived from the standard formula for the sum of x consecutive numbers starting from 1 which is x * (x + 1) / 2
The required sum is the sum of the numbers from 1 to (m - 1) subtracted from the sum of numbers from 1 to n. Thus:
n * (n + 1) / 2 - (m - 1) * (m - 1 + 1) / 2
[n * (n + 1) - (m - 1) * m] / 2
[n* n + n - m * m + m] / 2
[(n + m) + n * n- m * m] / 2
[(n + m) + (n - m) * (n + m)] / 2
(n + m)( n - m +1) / 2