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;










share|improve this question



















  • 1




    int a[1499] = 0; inside main() will also initialize all the elements to zero...
    – Paul Griffiths
    Nov 10 at 4:02














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;










share|improve this question



















  • 1




    int a[1499] = 0; inside main() will also initialize all the elements to zero...
    – Paul Griffiths
    Nov 10 at 4:02












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;










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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; inside main() will also initialize all the elements to zero...
    – Paul Griffiths
    Nov 10 at 4:02












  • 1




    int a[1499] = 0; inside main() 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












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.






share|improve this answer




















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20