![]() |
|
#1
|
|||
|
|||
Perfect NumbersDon't you love classes that are required for your major that have nothing to do with what you'll be doing with your degree? Yay!
Okay, I have to write a complete program to write to the file perfect.out all perfect numbers between 2 and a specified named integer constant LIMIT (which we give a value of 1000). This is what I have so far... but I have no clue how to use these functions in my main to get what I need to get. PLEASE HELP!!! I'm going crazy! CPP / C++ / C Code:
|
|||
|
#2
|
||||
|
||||
Re: C++ Help -- Perfect NumbersBy perfect number, you mean the mathematical definition: a number whose divisors summed together equal that number. For example 6 whose divisors are 1, 2, and 3.
1+2+3 = 6 or 28 1+2+4+7+14 = 28 Right? |
|
#3
|
|||
|
|||
Re: C++ Help -- Perfect NumbersYes, that is correct.
|
|
#4
|
|||
|
|||
Re: Perfect NumbersAn example of using your functions could be:
CPP / C++ / C Code:
However, your code won't compile, because in your sumOfPositiveDivisors you missed some closing braces. Anyway, there's an easier way to whether a number is perfect. For example: CPP / C++ / C Code:
Well, it's not a optimized code, but works Your code seems to be too much complicated, thought it has an only error, besides braces: Your divisor variable doesn't change with denom. It must stay in for loop. Hope it helps! Last edited by admin : 29-Mar-2008 at 05:00.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#5
|
|||
|
|||
Re: Perfect NumbersI'd love to use that for isPerfect, but our professor specifically told us that isPerfect must call sumOfPositiveDivisors. Can I use that code in place of sumOfPositiveDivisors to the point where it looks like this?
CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
Re: Perfect NumbersI updated my code a bit... I hate not knowing what the heck I'm doin. For an engineer-to-be, this is very irritating... HAHA!
CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
Re: Perfect NumbersHi,
I'm sure your program doesn't compile. Your must learn to read compiler error messages. For example, I've compiled your first code (without file-management) and the compiler prints: Quote:
CPP / C++ / C Code:
CPP / C++ / C Code:
Anyway, it won't compile, because you are comparing an integer (the number) with a bool value (true or false) in isPerfect. It makes no sense. As I said in my previous message, your first code was good, though has a couple of errors: the braces you missed and not update divisor in each iteration. So, using your first code, this should work: CPP / C++ / C Code:
Regards. |
|
#8
|
|||
|
|||
Re: Perfect NumbersCPP / C++ / C Code:
I still get errors in my MAIN: Quote:
What I need to do is print all perfect numbers (using isPerfect) from 2 to LIMIT (which we declare as 10,000) to the file "perfect.cc" |
|
#9
|
|||
|
|||
Re: Perfect NumbersOf course, it won't compile!! What are you attempting to do with this?
CPP / C++ / C Code:
CPP / C++ / C Code:
Regards |
|
#10
|
|||
|
|||
Re: Perfect NumbersGeez... I feel dumb now. ;)
I was thinking you could tell it to print all isPerfects from 2 to LIMIT in a return. Duuurrrr... The only problem Dev C++'s compiler is telling me is this: 35 variable `std::ofstream outFile' has initializer but incomplete type |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| recursive function for perfect numbers? | cancan | C Programming Language | 6 | 19-Nov-2007 11:46 |
| Prime or perfect numbers and output factors | buomque | C++ Forum | 4 | 26-Oct-2007 09:23 |
| subscript error in coding | warborules | C Programming Language | 6 | 27-Nov-2005 17:16 |
| 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