What condition should I use to make my code work with a local variable?
up vote
-1
down vote
favorite
So this code gives us the prime numbers. But it only works if int a[1499]
is a global variable because if an array is global it automatically initializes all the members to 0
. How can I change the condition of the for
-loop so that I can make a[1499]
a local variable and move it to function main
?
#include <iostream>
using namespace std;
int a[1499];
int main()
int PrimeCounter = 0;
int PrimeNumberNeeded = 1500;
bool Isprime;
int TestNumber = 2;
a[0]=2;
while (PrimeCounter != PrimeNumberNeeded)
Isprime = true;
for(int x=0; a[x]!= 0; x++)
if(TestNumber%a[x] == 0)
Isprime = false;
break;
if (Isprime)
a[PrimeCounter] = TestNumber;
PrimeCounter++;
TestNumber++;
// end of while
if (PrimeCounter == PrimeNumberNeeded)
cout << "Prime counter is: " << PrimeCounter << "nTest number is: "<< a[1499]<< endl;
return 0;
c++ global local
add a comment |
up vote
-1
down vote
favorite
So this code gives us the prime numbers. But it only works if int a[1499]
is a global variable because if an array is global it automatically initializes all the members to 0
. How can I change the condition of the for
-loop so that I can make a[1499]
a local variable and move it to function main
?
#include <iostream>
using namespace std;
int a[1499];
int main()
int PrimeCounter = 0;
int PrimeNumberNeeded = 1500;
bool Isprime;
int TestNumber = 2;
a[0]=2;
while (PrimeCounter != PrimeNumberNeeded)
Isprime = true;
for(int x=0; a[x]!= 0; x++)
if(TestNumber%a[x] == 0)
Isprime = false;
break;
if (Isprime)
a[PrimeCounter] = TestNumber;
PrimeCounter++;
TestNumber++;
// end of while
if (PrimeCounter == PrimeNumberNeeded)
cout << "Prime counter is: " << PrimeCounter << "nTest number is: "<< a[1499]<< endl;
return 0;
c++ global local
1
int a[1499] = 0;
insidemain()
will also initialize all the elements to zero...
– Paul Griffiths
Nov 10 at 4:02
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
So this code gives us the prime numbers. But it only works if int a[1499]
is a global variable because if an array is global it automatically initializes all the members to 0
. How can I change the condition of the for
-loop so that I can make a[1499]
a local variable and move it to function main
?
#include <iostream>
using namespace std;
int a[1499];
int main()
int PrimeCounter = 0;
int PrimeNumberNeeded = 1500;
bool Isprime;
int TestNumber = 2;
a[0]=2;
while (PrimeCounter != PrimeNumberNeeded)
Isprime = true;
for(int x=0; a[x]!= 0; x++)
if(TestNumber%a[x] == 0)
Isprime = false;
break;
if (Isprime)
a[PrimeCounter] = TestNumber;
PrimeCounter++;
TestNumber++;
// end of while
if (PrimeCounter == PrimeNumberNeeded)
cout << "Prime counter is: " << PrimeCounter << "nTest number is: "<< a[1499]<< endl;
return 0;
c++ global local
So this code gives us the prime numbers. But it only works if int a[1499]
is a global variable because if an array is global it automatically initializes all the members to 0
. How can I change the condition of the for
-loop so that I can make a[1499]
a local variable and move it to function main
?
#include <iostream>
using namespace std;
int a[1499];
int main()
int PrimeCounter = 0;
int PrimeNumberNeeded = 1500;
bool Isprime;
int TestNumber = 2;
a[0]=2;
while (PrimeCounter != PrimeNumberNeeded)
Isprime = true;
for(int x=0; a[x]!= 0; x++)
if(TestNumber%a[x] == 0)
Isprime = false;
break;
if (Isprime)
a[PrimeCounter] = TestNumber;
PrimeCounter++;
TestNumber++;
// end of while
if (PrimeCounter == PrimeNumberNeeded)
cout << "Prime counter is: " << PrimeCounter << "nTest number is: "<< a[1499]<< endl;
return 0;
c++ global local
c++ global local
edited Nov 10 at 4:11
dbush
90.7k12100131
90.7k12100131
asked Nov 10 at 3:59
eMe
103
103
1
int a[1499] = 0;
insidemain()
will also initialize all the elements to zero...
– Paul Griffiths
Nov 10 at 4:02
add a comment |
1
int a[1499] = 0;
insidemain()
will also initialize all the elements to zero...
– Paul Griffiths
Nov 10 at 4:02
1
1
int a[1499] = 0;
inside main()
will also initialize all the elements to zero...– Paul Griffiths
Nov 10 at 4:02
int a[1499] = 0;
inside main()
will also initialize all the elements to zero...– Paul Griffiths
Nov 10 at 4:02
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Move the definition of a
inside the function and give it an initializer:
int a[1499] = 0;
This explicitly initializes the first element to 0, and implicitly initializes the rest to 0 as well.
If I use an online c++ compiler it works but on code::blocks it doesn't work. It's not giving any error, it just sits there doing nothing.
– eMe
Nov 10 at 4:14
1
The fact you're trying to put 1,500 numbers into 1,499 boxes probably isn't helping matters.
– Paul Griffiths
Nov 10 at 4:17
Thank you! It works now. Although I don't get it why it works when use it as a global.
– eMe
Nov 10 at 4:20
That's undefined behavior for you. But it's probably because it's not trampling all over your stack when it's global, or over any other global data when it's the only global.
– Paul Griffiths
Nov 10 at 4:21
Great explanation! it's first time I hear about UB. I'll look more into it. Thanks again!
– eMe
Nov 10 at 4:26
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Move the definition of a
inside the function and give it an initializer:
int a[1499] = 0;
This explicitly initializes the first element to 0, and implicitly initializes the rest to 0 as well.
If I use an online c++ compiler it works but on code::blocks it doesn't work. It's not giving any error, it just sits there doing nothing.
– eMe
Nov 10 at 4:14
1
The fact you're trying to put 1,500 numbers into 1,499 boxes probably isn't helping matters.
– Paul Griffiths
Nov 10 at 4:17
Thank you! It works now. Although I don't get it why it works when use it as a global.
– eMe
Nov 10 at 4:20
That's undefined behavior for you. But it's probably because it's not trampling all over your stack when it's global, or over any other global data when it's the only global.
– Paul Griffiths
Nov 10 at 4:21
Great explanation! it's first time I hear about UB. I'll look more into it. Thanks again!
– eMe
Nov 10 at 4:26
add a comment |
up vote
1
down vote
Move the definition of a
inside the function and give it an initializer:
int a[1499] = 0;
This explicitly initializes the first element to 0, and implicitly initializes the rest to 0 as well.
If I use an online c++ compiler it works but on code::blocks it doesn't work. It's not giving any error, it just sits there doing nothing.
– eMe
Nov 10 at 4:14
1
The fact you're trying to put 1,500 numbers into 1,499 boxes probably isn't helping matters.
– Paul Griffiths
Nov 10 at 4:17
Thank you! It works now. Although I don't get it why it works when use it as a global.
– eMe
Nov 10 at 4:20
That's undefined behavior for you. But it's probably because it's not trampling all over your stack when it's global, or over any other global data when it's the only global.
– Paul Griffiths
Nov 10 at 4:21
Great explanation! it's first time I hear about UB. I'll look more into it. Thanks again!
– eMe
Nov 10 at 4:26
add a comment |
up vote
1
down vote
up vote
1
down vote
Move the definition of a
inside the function and give it an initializer:
int a[1499] = 0;
This explicitly initializes the first element to 0, and implicitly initializes the rest to 0 as well.
Move the definition of a
inside the function and give it an initializer:
int a[1499] = 0;
This explicitly initializes the first element to 0, and implicitly initializes the rest to 0 as well.
answered Nov 10 at 4:09
dbush
90.7k12100131
90.7k12100131
If I use an online c++ compiler it works but on code::blocks it doesn't work. It's not giving any error, it just sits there doing nothing.
– eMe
Nov 10 at 4:14
1
The fact you're trying to put 1,500 numbers into 1,499 boxes probably isn't helping matters.
– Paul Griffiths
Nov 10 at 4:17
Thank you! It works now. Although I don't get it why it works when use it as a global.
– eMe
Nov 10 at 4:20
That's undefined behavior for you. But it's probably because it's not trampling all over your stack when it's global, or over any other global data when it's the only global.
– Paul Griffiths
Nov 10 at 4:21
Great explanation! it's first time I hear about UB. I'll look more into it. Thanks again!
– eMe
Nov 10 at 4:26
add a comment |
If I use an online c++ compiler it works but on code::blocks it doesn't work. It's not giving any error, it just sits there doing nothing.
– eMe
Nov 10 at 4:14
1
The fact you're trying to put 1,500 numbers into 1,499 boxes probably isn't helping matters.
– Paul Griffiths
Nov 10 at 4:17
Thank you! It works now. Although I don't get it why it works when use it as a global.
– eMe
Nov 10 at 4:20
That's undefined behavior for you. But it's probably because it's not trampling all over your stack when it's global, or over any other global data when it's the only global.
– Paul Griffiths
Nov 10 at 4:21
Great explanation! it's first time I hear about UB. I'll look more into it. Thanks again!
– eMe
Nov 10 at 4:26
If I use an online c++ compiler it works but on code::blocks it doesn't work. It's not giving any error, it just sits there doing nothing.
– eMe
Nov 10 at 4:14
If I use an online c++ compiler it works but on code::blocks it doesn't work. It's not giving any error, it just sits there doing nothing.
– eMe
Nov 10 at 4:14
1
1
The fact you're trying to put 1,500 numbers into 1,499 boxes probably isn't helping matters.
– Paul Griffiths
Nov 10 at 4:17
The fact you're trying to put 1,500 numbers into 1,499 boxes probably isn't helping matters.
– Paul Griffiths
Nov 10 at 4:17
Thank you! It works now. Although I don't get it why it works when use it as a global.
– eMe
Nov 10 at 4:20
Thank you! It works now. Although I don't get it why it works when use it as a global.
– eMe
Nov 10 at 4:20
That's undefined behavior for you. But it's probably because it's not trampling all over your stack when it's global, or over any other global data when it's the only global.
– Paul Griffiths
Nov 10 at 4:21
That's undefined behavior for you. But it's probably because it's not trampling all over your stack when it's global, or over any other global data when it's the only global.
– Paul Griffiths
Nov 10 at 4:21
Great explanation! it's first time I hear about UB. I'll look more into it. Thanks again!
– eMe
Nov 10 at 4:26
Great explanation! it's first time I hear about UB. I'll look more into it. Thanks again!
– eMe
Nov 10 at 4:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235888%2fwhat-condition-should-i-use-to-make-my-code-work-with-a-local-variable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
int a[1499] = 0;
insidemain()
will also initialize all the elements to zero...– Paul Griffiths
Nov 10 at 4:02