I wrote this little app today to do the simple, maybe even trivial, task of seeing if a numbers abundant. Heres my code:
Code:
/******************************************
*** ***
*** ***
*** Abundant Number - A number that is ***
*** less than the sum of its factors ***
*** (excluding itself). For example, ***
*** the factors of 12 (1, 2, 3, 4, 6) ***
*** add up to 16. Compare deficient ***
*** number, perfect number. ***
*******************************************/
#include <iostream.h>
#include <stdlib.h>
void abundant(int org); //Our function to check the abudance of a number
int main(int argc, char *argv[])
{
/*
* If theres no arguments present then print a error message
* and exit.
*/
if( argc < 2 ){
cout << "Usage: " << argv[0] << " <test number>\n";
cout << "<test number> is the number to test for abundance.\n";
exit(0);
}
/*
* Convert the first argument to a integer.
*/
int test;
test = atoi(argv[1]);
/*
* If the integers value is higher then 200 then print a error
* message and quit.
*/
if( test > 200 ){
cout << "This application only accepts numbers between 0 - 200\n";
exit(0);
}
/*
* Now we're sure that the number is applicable to our checks we
* call our abundance checking function. The only argument to
* this function is the integer named test. (The users argument)
*/
abundant(test);
return 0;
}
void abundant(int org)
{
int i, j, test;
int intarr[200];
j = 0;
/*
* Declare a loop. The loop will execute "org" amount of time. Org
* is the number we are testing.
*/
for(i=0; i >= org; i++)
{
/*
* When we divide "org" by "i" if theres no remainder then we will
* add it to our integer array. This just stores "org"s factors.
*/
if(((org % i) == 0) && (i != org))
{
intarr[j] = i;
j++;
}
}
/*
* Now we loop through our array of factors and add them all up.
*/
for(i=0; i >= j; i++)
{
test += intarr[i];
}
/*
* If the sum of factors is greater than the number we are testing
* then the number is abundant.
*/
if( test > org){
cout << org << " - The number is abundant.\n";
return;
}
/*
* Else, if the sum of factors is equal to, or less then the
* number we are testing we display a message with our results.
* In this case our results are that the number ISN'T abundant.
*/
else if( test <= org){
cout << org << "Isn't a abundant number.\n";
return;
}
}
It compiled ok and I proceeded to run it. I supplied it with numbers in the correct range and it simply "crapped out". When I use a number above 200 it works perfect. (i.e displays the error message)
If I don't supply it with any arguments it does what its meant to and displays the usage.
When I supply it with the correct info it simply does nothing.
I tried to debug it with gdb, as I have only used gdb on my linux box I wasnt sure if the "no debuging symbols" was ok.
(no debugging symbols found)...(gdb)
(gdb) run 100
run 100
Starting program: \DOCUME~1\Fergus\MYDOCU~1/Abudance.exe 100
77f50000:ntdll.dllntdll.dll: No such file or directory.
77e60000:C:/WINDOWS/system32/kernel32.dll(no debugging symbols found)...
77c10000:C:/WINDOWS/system32/msvcrt.dll(no debugging symbols found)...
Program received signal SIGSEGV, Segmentation fault.
0x401432 in ?? ()
(gdb)
It throws a segfualt out. I simply don't understand how my program is accessing memory out of its allowed range.
Any ideas or Suggestions?
Thanks!
-Moddy