#include <iostream>
#include <cmath>
usingnamespace std;
void isPerfect();
void showPerfect();
int main()
{
isPerfect();
showPerfect();
}
void isPerfect ()
{
int n, sum = 0, f;
cout << "Enter a number: " << endl;
cin >> n;
while (n < 0)
{
cout << "Enter a number: " << endl;
cin >> n;
}
bool result;
for( f = 1; f <= n/2; f++)
{
if ( n % f == 0)
{
sum += f;
}
}
if ( sum == n)
{
result = true;
}
else
{
result = false;
}
if (result == true)
{
cout << "The number " << n << " is a perfect number." << endl;
}
else
{
cout << "The number " << n << " is not a perfect number." << endl;
}
}
void showPerfect()
{
int sum, f, n;
if (sum == f)
{
n = 1;
cout << f << " = ";
while (n<f)
{
if (f%n == 0)
cout << " + " << n;
n++;
}
}
}
line 15 and line 56 - these are local variables within the respective functions. showPerfect() doesn't know what values were calculated for the sum and f variables inside isPerfect().
Well, I'm not sure that I've seen a description of what the program is supposed to do. Getting it to do something is fairly easy, but it would be better if it was more clear what the assignment or task is intended to achieve.
Perfect Numbers
A positive integer value is said to be "perfect" if it is the sum of its proper divisors; that is, the value is equal to sum of all the numbers (except itself) dividable by it.
For example, the number 28 is perfect because it is the sum
28 = 1 + 2 + 4 + 7 + 14
Write a program that:
1. Contains a function isPerfect that determines if a number passed as its argument is perfect,
2. Contains a second function showPerfect that outputs, using the above format for 28, the equation showing the sum of a number that is already known to be perfect,
3. Uses the two functions from main to find and show all perfect numbers in a sum format between zero and 8000.
Note 1: the function to determine if the number is perfect should be as efficient as possible. That is, to check for proper divisors, it should only count from 2 to the number whose square is greater than or equal to the number (1 is always a divisor), adding the divisor and its larger counterpart to the sum. For example, for 28, only count to 6, adding 2 & 14 and 4 & 7 along the way.
Note 2: The function to output the sums does not need to (and cannot) be as efficient, since it will only be called a few times.
Note 3: The body of your main program should include a statement something like this (the loop MUST start at 0):
for (i = 0; i <= 8000; i++)
{
if (isPerfect(i)) showPerfect(i);
}
There are some very specific instructions there which give a good clarification about the program logic. In particular, Note 3. Though some of the instructions are rather loosely worded, I would take the phrase "main program" to mean the function main().
In that case, this existing code
5 6 7 8 9 10 11 12
void isPerfect();
void showPerfect();
int main()
{
isPerfect();
showPerfect();
}
Would change to look like this:
5 6 7 8 9 10 11 12 13 14 15
bool isPerfect(int n);
void showPerfect(int n);
int main()
{
for (int i = 0; i <= 8000; i++)
{
if (isPerfect(i)) showPerfect(i);
}
}
Notice that the prototypes of the two functions have changed. isPerfect() now returns a bool (true or false) value to show whether or not the number is perfect. Both functions have a a single integer parameter, meaning they deal with checking and printing just one number at a time.
As a consequence a little bit of re-working of your existing code would be required.
That doesn't mean you are limited to just those two functions of course. If you find it necessary or useful, you could use additional 'helper' functions.
That means, in general that the function named in the message does not exist.
In this particular case, it almost certainly means that you changed the prototypes and you changed main, but you didn't yet change the functions themselves to match. The old versions of the functions will require the function header changing to agree with the prototype. The code inside the functions will require some rework.
Function isperfect is almost correct as it stands. You just need to remove all the cin and cout statements. Instead of getting the number n from cin, use the value passed as the function parameter. Mostly that means removing a few lines of code and tidying up.
Function showPerfect will require a bit more work, but you can borrow most of the code from your existing function, instead of adding the value to the sum, instead output it using cout.
#include <iostream>
#include <cmath>
usingnamespace std;
bool isPerfect(int n);
void showPerfect(int n);
int main()
{
for (int i = 0; i <= 8000; i++)
{
if (isPerfect(i)) showPerfect(i);
}
}
bool isPerfect ()
{
int n, sum = 0, f;
return n;
while (n < 0)
{
return n;
}
bool result;
for( f = 1; f <= n/2; f++)
{
if ( n % f == 0)
{
sum += f;
}
}
if ( sum == n)
{
result = true;
}
else
{
result = false;
}
if (result == true)
{
cout << "The number " << n << " is a perfect number." << endl;
}
else
{
cout << "The number " << n << " is not a perfect number." << endl;
}
}
void showPerfect()
{
int sum, f, n;
if (sum == f)
for( f = 1; f <= n/2; f++)
{
if ( n % f == 0)
{
cout << " + " << f;
}
}
}
But I keep getting the error saying 'undefined reference to' either function
void isPerfect ()
{
int n, sum = 0, f;
cout << "Enter a number: " << endl;
cin >> n;
while (n < 0)
{
cout << "Enter a number: " << endl;
cin >> n;
}
bool result;
for( f = 1; f <= n/2; f++)
{
if ( n % f == 0)
{
sum += f;
}
}
if ( sum == n)
{
result = true;
}
else
{
result = false;
}
if (result == true)
{
cout << "The number " << n << " is a perfect number." << endl;
}
else
{
cout << "The number " << n << " is not a perfect number." << endl;
}
}
Now follow these three steps:
1. change the function header.
2. remove all cin and all cout (in this particular function). Instead of cin n, use the parameter n.
3. add a return statement right at the end (function should return a bool value).
4. tidy up if necessary.
bool isPerfect(int n)
{
int sum = 0, f;
bool result;
for( f = 1; f <= n/2; f++)
{
if ( n % f == 0)
{
sum += f;
}
}
if ( sum == n)
{
result = true;
}
else
{
result = false;
}
return result;
}
That same code could be written more concisely, but that can wait until another time.
#include <iostream>
#include <cmath>
usingnamespace std;
bool isPerfect(int n);
void showPerfect(int n);
int main()
{
for (int i = 0; i <= 8000; i++)
{
if (isPerfect(i)) showPerfect(i);
}
}
bool isPerfect(int n)
{
int sum = 0, f;
bool result;
for( f = 1; f <= n/2; f++)
{
if ( n % f == 0)
{
sum += f;
}
}
if ( sum == n)
{
result = true;
}
else
{
result = false;
}
return result;
}
void showPerfect(int n)
{
int sum, f;
if (sum == f)
for( f = 1; f <= n/2; f++)
{
if ( n % f == 0)
{
cout << " + " << f;
}
}
}
1. Contains a function isPerfect that determines if a number passed as its argument is perfect,
2. Contains a second function showPerfect that outputs, using the above format for 28, the equation showing the sum of a number that is already known to be perfect,
3. Uses the two functions from main to find and show all perfect numbers in a sum format between zero and 8000.
I can't locate the part where the program is to prompt the user to enter a number.
On the other hand, there are still some more things to be done.
1. the output of function showPerfect should be in this format:
28 = 1 + 2 + 4 + 7 + 14
That will require a couple of minor changes to that function - I think you already had some of the code in an earlier version.
2. Refer again to the first note in the assignment:
Note 1: the function to determine if the number is perfect should be as efficient as possible. That is, to check for proper divisors, it should only count from 2 to the number whose square is greater than or equal to the number (1 is always a divisor), adding the divisor and its larger counterpart to the sum. For example, for 28, only count to 6, adding 2 & 14 and 4 & 7 along the way.
That means it isn't enough to simply have a program which works properly and gives the required results. It is asking that the function isPerfect is written in a particular way.
(user input not shown - I ran this online at the cpp.sh site)
The main body of the output is there, it just needs a newline at the end of each result - a single cout << '\n'; would fix that. And then you need to add the start of the result, which you almost had right at the start of this thread.
The only minor point then is to suppress the "+" output before the first factor (which is always 1).