• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code to create array containing only even numbers  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have come up with a below code for your requirement. The requirement to create a array size of exact even number didn't meet although it can be achieved by doing a second iteration through the numbers.

But didn't feel right to write a second for loop to separate move even numbers into the array. If someone come up with a logic with single for loop, it would be awesome I guess.

Here is the code :

 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
I have corrected the code tags in your post; you didn't have them in the right position.
There is lots to criticise in that solution of yours; in fact there is so much that I think it merits discussion in a thre‍ad of its own.

I shall start by telling you always to format code consistently. We have some suggestions of our own. But you have squashed the code together horribly, not leaving space after ; or : (which is why you had the symbol), which makes your code really difficult to read, never mind understand.
Please post the code anew with correct spacing and consistent indentation, then we shall discuss it some more.
 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should think about what is needed BEFORE you start writing code. Or maybe write the beginnings of the code, with only comments specifying what needs to be done. for example:
 
Naveen N Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell and Sorry about the formatting, it was my first post.  I have made some changes to the logic, I still don't feel it is an efficient logic but close to what we were looking.

   
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still need to correct the formatting. Look at lines 12, 26 and 28, for example. Once you have got the formatting right, there are lots of other style problems. Once we have sorted them out, you will be able to see the really big errors.
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few hints:
1. What makes you think that zero is not an even number?
2. Your array size is incorrect. Check it by hand with a few examples. For example, what size does your code give for int1=-10 and int2 = 1 ?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!