GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Oct-2009, 21:51
DDVX DDVX is offline
New Member
 
Join Date: Oct 2009
Posts: 8
DDVX is on a distinguished road

Perfect Numbers with functions


Hey guys, I'm having problem with a typical C++ beginning program where I find all the perfect numbers between 1 and 10000. I have made a program to find this before, but we have been asked to rewrite it using the two following functions:
int sum_of_positive_divisors(int number)
and
bool is_perfect(int number).

If you could help me figure out what I did wrong it would be greatly appreciated. When I compile it there is an error at the bool function, but I feel like there might be more than that. Thanks in advance!

CPP / C++ / C Code:
/**
*Programming Assignment #4
*
*@file perfect.cc
*@author - Matthew Cobb - , - cs125366 -
*@date - October 12, 2009 -
*@version 1.0
*/

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

const int LIMIT = 10000;

int main() {
ofstream outfile;

outfile.open("perfect.out");
if (outfile.fail()) {
  cerr << "error: can't open perfect.out for output\n";
  return 1;
}

}
/**
* Sum_of_positive_divisors calculates and returns the sum of all posiive
* divisors of the argument number.
*/

int sum_of_positive_divisors(int number){
    int possible_divisor = 0, sum_of_positive_divisors = 0;
    while (number < LIMIT){
      for (int i = 1; i < number; i++){
        if (!(number % i)){
            possible_divisor = i;
            sum_of_positive_divisors += possible_divisor;
        }
      }
    }
    return sum_of_positive_divisors;
}


/**
* Bool is_perfect is a predicate (a boolean-valued function) that returns true
* if and only if the integer argument is a perfect number and false otherwise.
* Is_perfect() will call sum_of_positive_divisors(); is_perfect() will only be
* called by main().
*/

bool is_perfect(int number);
if (possible_divisor == number){
  return true;
  }
  else
    return false;
}

/*
    * If our assumption was shown to be correct, print the perfect number.
    */
    if (sum==number)
      cout << number << " is perfect.\n";
      number++;

outfile.close();
return 0;
}
  #2  
Old 20-Oct-2009, 07:25
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Perfect Numbers with functions


You have some semicolons and brackets out of place. Try starting with this as a skeleton:
CPP / C++ / C Code:
int main() 
{
  return 0;
}

/**
* Sum_of_positive_divisors calculates and returns the sum of all posiive
* divisors of the argument number.
*/
int sum_of_positive_divisors(int number)
{
  return 0;
}


/**
* Bool is_perfect is a predicate (a boolean-valued function) that returns true
* if and only if the integer argument is a perfect number and false otherwise.
* Is_perfect() will call sum_of_positive_divisors(); is_perfect() will only be
* called by main().
*/
bool is_perfect(int number)
{
    return false;
}
As you add code, do a small piece then recompile to make sure you didn't break anything.
  #3  
Old 20-Oct-2009, 09:53
DDVX DDVX is offline
New Member
 
Join Date: Oct 2009
Posts: 8
DDVX is on a distinguished road

Re: Perfect Numbers with functions


Ok, I didn't realize that the int sum_of_positive_integers and bool weren't included in int main(). I'm now getting an error at:
if (is_perfect(number))
return true;
cout << number << " is perfect.\n";
else
return false;

which mentions an expected primary expression before the else statement, expected ; before the else statement.
CPP / C++ / C Code:
const int LIMIT = 10000;

int main() {
ofstream outfile;

outfile.open("perfect.out");
if (outfile.fail()) {
  cerr << "error: can't open perfect.out for output\n";
  return 1;
}

return 0;

int sum_of_positive_divisors(int number){
    int possible_divisor = 0, sum_of_positive_divisors = 0;
    while (number < LIMIT){
      for (int i = 1; i < number; i++){
        if (number % i == 0){
            possible_divisor = i;
            sum_of_positive_divisors += possible_divisor;
        }
      }
    }
    return sum_of_positive_divisors;
}

bool is_perfect(int number){
if (is_perfect(number))
  return true;
  cout << number << " is perfect.\n";
  else 
    return false;
}

outfile.close();
return 0;
}

  #4  
Old 20-Oct-2009, 13:06
DDVX DDVX is offline
New Member
 
Join Date: Oct 2009
Posts: 8
DDVX is on a distinguished road

Re: Perfect Numbers with functions


OK, I updated it a bit. I still have a problem with my else statement, but my main problem is that the cout statement is supposed to be in int main(), but I'm not sure how to do that. If i take out the if else statement completely then there are no errors, but there is obviously no output since I didn't use cout. So basically, what do I put under the bool expression, and how do I use cout in main? I've used it before, but never while referencing a previous function. Hope that made sense.

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

const int LIMIT = 10000;

int sum_of_positive_divisors(int number){
    int possible_divisor = 0, sum_of_positive_divisors = 0;
    while (number < LIMIT){
      for (int i = 1; i < number; i++){
        if (number % i == 0){
            possible_divisor = i;
            sum_of_positive_divisors += possible_divisor;
        }
      }
    }
    return sum_of_positive_divisors;
}

bool is_perfect(int number){
if (is_perfect(number))
  return true;
  cout << number << " is perfect.\n";
  else
  return false;
}

int main() {
ofstream outfile;

outfile.open("perfect.out");
if (outfile.fail()) {
  cerr << "error: can't open perfect.out for output\n";
  return 1;
}


outfile.close();
return 0;
}
  #5  
Old 20-Oct-2009, 13:35
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Perfect Numbers with functions


There is something that you are not quite getting yet...brackets. For each opening bracket {, you need a closing bracket } somewhere after it.

When you are using if/else statements, it is primarily best to use brackets like:
CPP / C++ / C Code:
if(x == y)
{
  statement1();
  statement2();
}
else
{
  statement3();
  statement4();
}
If you do not use brackets, only one statement will be run like:
CPP / C++ / C Code:
if(x == y)
  statement1();
else
  statement3();

The following will produce compile errors:
CPP / C++ / C Code:
if(x == y)
  statement1(); // this statement ends the if() section
  statement2(); // this statement will always execute
else // THIS IS A MISPLACED ELSE BECAUSE THE IF() HAS ALREADY ENDED
  statement3();

Check your code again and see if any of this applies.
  #6  
Old 20-Oct-2009, 15:29
DDVX DDVX is offline
New Member
 
Join Date: Oct 2009
Posts: 8
DDVX is on a distinguished road

Re: Perfect Numbers with functions


Ok, thanks. I see what you're saying.

CPP / C++ / C Code:
bool is_perfect(int number){
  if (is_perfect(number)){
  return true;
  }
  else{
  return false;
  }
}

The program compiles and runs successfully, but there is still no output since I'm not sure how to include it in int main().
  #7  
Old 20-Oct-2009, 15:58
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Perfect Numbers with functions


Quote:
Originally Posted by DDVX
The program compiles and runs successfully, but there is still no output since I'm not sure how to include it in int main().
Try printing to the screen with cout:
CPP / C++ / C Code:
if( is_perfect(122) ) 
{
  cout << "122 is perfect" << endl;
}
else
{
  cout << "122 is NOT perfect" << endl;
}
  #8  
Old 20-Oct-2009, 17:37
DDVX DDVX is offline
New Member
 
Join Date: Oct 2009
Posts: 8
DDVX is on a distinguished road

Re: Perfect Numbers with functions


There are no errors again, but there is no output. When I run the program, it quits responding.


CPP / C++ / C Code:
int main() {
ofstream outfile;

outfile.open("perfect.out");
if (outfile.fail()) {
  cerr << "error: can't open perfect.out for output\n";
  return 1;
}

int number;
  if (is_perfect( number )){
    cout << number << '\n';
  }   

outfile.close();
return 0;
}
  #9  
Old 21-Oct-2009, 07:00
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Perfect Numbers with functions


First of all, it would only print if the number will be found to be perfect so you may want to print something if the number was not found to be perfect and, as may not have noticed, you never set the number so it will be the value of whatever is at that location in memory (~randomish).
  #10  
Old 21-Oct-2009, 07:20
DDVX DDVX is offline
New Member
 
Join Date: Oct 2009
Posts: 8
DDVX is on a distinguished road

Re: Perfect Numbers with functions


Ah ok, I didn't realize about the value of the number. I'm not showing output of the non perfect numbers since my range is from 1-10000. That would be a lot of output. I did this:

CPP / C++ / C Code:
int main() {
ofstream outfile;

outfile.open("perfect.out");
if (outfile.fail()) {
  cerr << "error: can't open perfect.out for output\n";
  return 1;
}

int number = 1;
  if (is_perfect( number )){
    cout << number << '\n';
    number++;
  }   

outfile.close();
return 0;
}

but again with no success. Thanks again for your help fake. This is all new to me and you're definitely helping. I appreciate it.
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Perfect numbers within a range aedude94 C++ Forum 4 09-Oct-2008 08:28
Perfect Numbers Corrupt Shadow C++ Forum 9 31-Mar-2008 06:43
Prime or perfect numbers and output factors buomque C++ Forum 4 26-Oct-2007 09:23
Linear Search eccoflame C Programming Language 3 19-Apr-2005 08:36
Program that will find the first 4 perfect numbers????? bicknell83 C Programming Language 3 30-Mar-2005 01:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:57.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.