# hold numbers with array

This is a discussion on hold numbers with array within the C Programming forums, part of the General Programming Boards category; I'm writing a problem to output all the prefect number which is less than 1million. But I have a little ...

1. ## 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. 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. 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. 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. >can I use for instead of if?
An if statement is not a loop, a for statement is.

6. 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.

7. 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. 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. 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.

10. 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. 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
*/```

12. 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. 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. 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. 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.

Page 1 of 2 12 Last