Hey Guys, sorry but i guess I posted a code snippet when I shouldnt've. That was my first daniweb thread.


The program I'm trying to run checks to see if a number is perfect, abundant or deficient. It's perfect if its factors add up to the number(ie 6 = 3+2+1), deficient if the number is greater than the factors, and abundant if the factors add up to more than the number. This is one of the first times ive coded in c, we had to make a java one and it worked fine when I did it then.

Anyways i get a bus error after it asks for an upper bound...What is a bus error and how can I get this to work?

s84n109:desktop teg3$ gcc -o perfectnumbers perfectnumbers.c
s84n109:desktop teg3$ ./perfectnumbers
Enter an upper bound
10
Bus error


Thanks alot guys

/*
      Tom George
      9/16/09
      perfectnumbers.c
      tells whether given number is perfect, deficient, or abundant
       */

       
      #include <stdio.h>
      int get_sum_of_divs(int num);
      void detect(int x);
      int num;

       
      int main()
      {
      printf("Enter an upper bound\n"); //Finds the number to check to
      scanf("%d", num);
       
      int x = 2;
       
      while(x<=num) //Checks the numbers for their status
      {
       
      detect(num);
      x++;
      }
       
      return 0;
      }
       
      int get_sum_of_divs(int num) //finds the sum of the divisors
      {
      int sum = 0;
      int i;
      for(i=1; i<num; i++)
      {
      if(num % i == 0)
      sum = sum+i;
      }
      return sum;
      }       

      void detect(int x)
      {
      int sum = get_sum_of_divs(x);
      if(x>sum)
      printf("\n%d is deficient\n", x);
      if(sum>x)
      printf("\n%d is abundant\n",x);
      if(sum==x)
      printf("\n%d is perfect\n",x);
      }

Other than adding the "&" to the scanf statement . . .

Why do you do the following loop?

while(x<=num)		//Checks the numbers for their status
	{

		detect(num);
		x++;
	}

If you want to check all numbers between 2 and num, then you must detect(x) rather than detect(num). Currently, all your program does is print out the result num times, which I doubt is what you want. ; )

I don't get the floating point exception you mentioned on my machine.

Edited 7 Years Ago by wheel: n/a

Other than adding the "&" to the scanf statement . . .

Why do you do the following loop?

while(x<=num)		//Checks the numbers for their status
	{

		detect(num);
		x++;
	}

If you want to check all numbers between 2 and num, then you must detect(x) rather than detect(num). Currently, all your program does is print out the result num times, which I doubt is what you want. ; )

I don't get the floating point exception you mentioned on my machine.

I'm not sure why the OP even needs the x variable. Get rid of the loop and the variable x in main().

Also, in the get_sum_of_divs function, you only need to check up to num/2 in the for loop.

for (i = 1; i <= num / 2; i++)

Finally, for the OP, learn how to post code properly - read this:
http://www.daniweb.com/forums/thread93280.html
[Edit]
OK .. I see now, the OP wishes to check all numbers up to the upper bound entered - I thought the number entered was the only number to be checked for "perfectness" or otherwise. Sorry - OP go with Wheel's suggestion.
[/Edit]

Also, in the detect() function, use if-else if - that way you aren't executing any unnecessary additional checks.

Edited 7 Years Ago by yellowSnow: early hours of the morning brain fart

This article has been dead for over six months. Start a new discussion instead.