Thread: hold numbers with array

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    Question hold numbers with array

    I'm writing a problem to output all the prefect number which is less than 1million. But I have a little problem with hold all the numbers by using array. It only shows the first prefect number "6", so how to solve this problem. And here's my code:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int x, b, k, y;
    int prefect(int b);
    
    void main ()
    {
      printf("\nThis is a prefect number: %ld", prefect(b));
    }
    
    int prefect(int b)
    {
      if(b<1000000)
      {
        if(k = 1, k <= 9, k++)
        {
          x=pow(2, k-1);
          y=pow(2, k);
        }
      b = x * (y-1);
      }
      return b;
    }

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Well there are a few things wrong with this code. You only call the prefect() function once. You should call the function in the main, and then print out all perfect values using a loop in the prefect function.

    if(k = 1, k <= 9, k++)
    This is not a valid if statement.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    try this:
    [Code:]

    int x, b, k, y;
    int prefect(int b);

    void main ()
    {

    prefect(b);
    }

    void prefect(int b)
    {
    if(b<1000000)
    {
    for(k = 1;k <= 9; k++)
    {
    x=pow(2, k-1);
    y=pow(2, k);
    b = x * (y-1);
    printf("\nThis is a prefect number: %ld", b);
    }
    }
    }[\Code]

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    Originally posted by Cshot
    Well there are a few things wrong with this code. You only call the prefect() function once. You should call the function in the main, and then print out all perfect values using a loop in the prefect function.

    if(k = 1, k <= 9, k++)
    This is not a valid if statement.
    But i did call the function in the main. But what kind of a loop should I use in the prefect function.
    can I use for instead of if?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >can I use for instead of if?
    An if statement is not a loop, a for statement is.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are more things wrong with your code. I will point some out:

    Code:
    int x, b, k, y;
    int prefect(int b);
    
    void main ()
    {
    
        prefect(b);
    }
    
    void prefect(int b)
    {
        if(b<1000000)
    There are actually only three things wrong with the example above.

    1) Is not really wrong, it just could be done better. The first point is your use of global variables. Do you really need a global? If not, don't use them. You really should only use global variables when you have to.

    2) This is wrong:

    void main ...

    This is correct:

    int main( void )

    Or:

    int main( int argc, char ** argv )

    Or:

    int main( int argc, char *argv[] )

    Or:

    ... I'll stop there, because you don't need to know about passing enviornment variables for use with CGI.

    Anyway, don't use void main. Ever. At least, don't ever use it in C. If your teacher or book states that in C you should use void main, they're wrong. It's as simple as that. It should be:
    Code:
    int main ( void )
    {
    
        return 0; /* return when done with main */
    }
    Anyway, aside from globals, here is the real problem with b:

    3) You use b without ever initializing it. Never ever use a variable without initializing it, unless you have some bizzare need to make your programs crash or not behave as they should.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Unregistered
    Guest
    Thanks for all your guys' advices! Now I fixed my code and almost get the answer
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int i, j, k, b;
    int prefect(int b);
    
    int main (void)
    {
      prefect(b);
      return 0;
    }
    
    int prefect(int b)
    {
      while(b >= 6 && b <= 1000000);
      {
        for(k=2; k<15; k++)
        {
          i=pow(2, k-1);
          j=pow(2, k);
          b=i*(j-1);
          printf("\nThis is a prefect number: %d", b);
        }
    
      }
      return b;
    }
    but here's the output what i get
    Code:
    This is a prefect number: 6
    This is a prefect number: 28
    This is a prefect number: 120
    This is a prefect number: 496
    .
    .
    .
    This is a prefect number: -256
    This is a prefect number: -8192
    .
    .
    .
    This is a prefect number: 57344
    How come I can get these negtive number?

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    Overflow. An integer goes from -2,000,000,000 to +2,000,000,000 (approximately. Exact range is -2^31 to +2^31-1). Due to the way numbers are stored internally, you went higher than that range, and automatically overflowed to negative numbers. You need to use either 64 bit integers (if supported byu compiler/platform), write your own 64 bit integer code, or use doubles (this 3rd option may cause your results to be inexact, doubles may not have sufficient precision to give an exact answer.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by quzah
    <snip>
    3) You use b without ever initializing it. Never ever use a variable without initializing it, unless you have some bizzare need to make your programs crash or not behave as they should.
    But b is global, so I believe it will be initialised to zero by default.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    out of curiosity, since b is delcared globally and is also the name of the variable passed to the function, how will it know which b to use?

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by moi
    out of curiosity, since b is delcared globally and is also the name of the variable passed to the function, how will it know which b to use?
    It will use the one that is most local to it. So :

    - in main() when we call prefect(b), the b here is the global one, as is passed by value to prefect().
    - in prefect(), we use the local copy of b. Any changes to b here will not affect the global b.

    Here's a sample to help explain:
    Code:
    #include <stdio.h>
    
    int b;
    
    void f(int b)
    {
    	printf ("in func before assignment, b is %d\n", b);
    	b = 10;
    	printf ("in func after assignment, b is %d\n", b);
    }
    
    int main(void)
    {
    	printf ("in main, b is %d\n", b);
    	f(b);
    	printf ("in main after calling func, b is %d\n", b);
    	return 0;
    }
    
    /*
    Output:
    in main, b is 0
    in func before assignment, b is 0
    in func after assignment, b is 10
    in main after calling func, b is 0
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    Originally posted by Javariel
    Overflow. An integer goes from -2,000,000,000 to +2,000,000,000 (approximately. Exact range is -2^31 to +2^31-1). Due to the way numbers are stored internally, you went higher than that range, and automatically overflowed to negative numbers. You need to use either 64 bit integers (if supported byu compiler/platform), write your own 64 bit integer code, or use doubles (this 3rd option may cause your results to be inexact, doubles may not have sufficient precision to give an exact answer.
    I tried the 1st. but it still goes to the negative numbers.

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    I tried your code and it worked OK for me. No negative numbers, but the prefect number goes above 1,000,000.

    To get rid of that problem, perhaps you can try this:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include "stdafx.h"
    int i, j, k, b;
    int prefect(int b);
    
    int main (void)
    {
      prefect(b);
      return 0;
    }
    
    int prefect(int b)
    {
      k = 2;
      while(b <= 1000000)
      {
        
          i=pow(2, k-1);
          j=pow(2, k);
          b=i*(j-1);
          k++;
          
          if (b <= 1000000)
          {
              printf("\nThis is a prefect number: %d", b);
          }
      }
    
      return b;
    }

  14. #14
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The reason why you're overflowing is because you're not checking for the value to go over 1 million at the right place. So pretty much you'll break out when k = 15 which is not what you want.

    Here try this:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void prefect();
    
    int main()
    {
      prefect();
      return 0;
    }
    
    void prefect()
    {
       int b, i, j, k;
       for(k = 2; ; k++)
       {
          i = (int)pow(2, k-1);
          j = (int)pow(2, k);
          b = i * (j - 1);
          if(b > 1000000)
             break;
          printf("This is a prefect number: %d\n", b);
       }
    }
    Output:
    This is a prefect number: 6
    This is a prefect number: 28
    This is a prefect number: 120
    This is a prefect number: 496
    This is a prefect number: 2016
    This is a prefect number: 8128
    This is a prefect number: 32640
    This is a prefect number: 130816
    This is a prefect number: 523776

    Hope this helps

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer

    But b is global, so I believe it will be initialised to zero by default.
    I don't think so. Only static variables are initialized. I don't believe globals are initialized. AFAIK, only variables which use the keyword static are given an initial value.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  2. sorting an array of numbers in C
    By JVincent08 in forum C Programming
    Replies: 2
    Last Post: 03-14-2008, 01:42 PM
  3. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 03:41 AM
  4. getting numbers in an array
    By ct26torr in forum C Programming
    Replies: 6
    Last Post: 03-04-2003, 10:31 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM