11 Replies - 928 Views - Last Post: 15 December 2012 - 07:51 PM Rate Topic: -----

#1 stvo131  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-December 12

Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 12:49 PM

/**
  
 * S.M. 
 * 12-15-12
 */
public class arrarys
{

    public static void one()
    {
       int countEvens=0;
       int countOdds=0;
       
        int[] numbs= new int []{2, 4, 6, 8, 10, 12, 14, 19, 21, 47};
       for(int index=0;index<numbs.length;index++){
if(index%2 == 0) {
countEvens++;
}
else {
countOdds++;
}
}
               
        
    
    System.out.println("The number of even numbers is" + countEvens);
    System.out.println("The number of odd numbers is" + countOdds);
}}



I know that if I use
 for(int index : numbs) 

I can get my code to work, but I want to know why the way I have it now as
for(int index=0;index<numbs.length;index++)
does not work. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Counting all even numbers and odd numbers in a given arrary

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12178
  • View blog
  • Posts: 45,247
  • Joined: 27-December 08

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 12:53 PM

In the enhanced for loop, the term index takes on the value of an element in the array. This is not the case in a regular for loop. You would have to use the syntax numbs[index] to access a specific element in the array.
Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 12:55 PM

This part is your problem
if(index%2 == 0) {


You are checking if index is even or odd instead of checking the number in the array

You can lookup values in the array by doing numbs[<some integer here>]
numbs[0]; //Will be 2
numbs[1]; //Will be 4
numbs[2]; //Will be 6
...


Was This Post Helpful? 1
  • +
  • -

#4 stvo131  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-December 12

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 12:59 PM

Oh wait why didn't I see it before what I have isn't reading the array it's just repeating 9 times....

Thank's guys. I'll tinker with it a bit and see what I get.
Was This Post Helpful? 0
  • +
  • -

#5 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 05:21 PM

FOR-EACH: Is based on elements, and
FOR: are based on index
public class ArraysPractice
{
	public static void main(String[] args)
	{
		int evenNums = 0;
		int oddNums = 0;
		
		int[] nums = {2, 4, 6, 8, 10, 12, 14, 19, 21, 47}; // no need to include new int [] , becuase {value1, value2, etc} determine the number of elements and allocates the mem
		
       for(int index = 0; index < nums.length; index++) // start at index 0    
	   {
			if(nums[index] % 2 == 0)
			{
				evenNums++;
			}
			else 
			{
				oddNums++;
			}
		}
		
		/*for(int element : nums) // the for-each loop is based on elements, the for loop is based on index
		{
			if(element % 2 == 0)
			{
				evenNums++;
			}
			else
			{
				oddNums++;
			}
		}
		*/
		System.out.println("even numbers total: " + evenNums);
		//7
		System.out.print("odd numbers totoal: " + oddNums);
		//3
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 stvo131  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-December 12

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 06:04 PM

I ended up just doing this, but thank you guys.



public static void one()
    {
       int countEvens=0;
       int countOdds=0;
       
        int[] numbs= new int []{2, 4, 6, 8, 10, 12, 14, 19, 21, 47};
       //for(int index=0;index<numbs.length;index++){
if
(numbs[0]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
if
(numbs[1]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
               
  if
(numbs[2]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}      
 if
(numbs[3]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}   
if
(numbs[4]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



} 

if
(numbs[5]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
if
(numbs[6]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
  
if
(numbs[7]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
if
(numbs[8]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
if
(numbs[9]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
System.out.println("The number of even numbers is" + countEvens);
    System.out.println("The number of odd numbers is" + countOdds);
}}

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 06:14 PM

Why ???
Why not using a for() loop ?
What will you do when your array will have 25000 elements ???
Was This Post Helpful? 0
  • +
  • -

#8 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 07:04 PM

View Poststvo131, on 15 December 2012 - 08:04 PM, said:

I ended up just doing this, but thank you guys.



public static void one()
    {
       int countEvens=0;
       int countOdds=0;
       
        int[] numbs= new int []{2, 4, 6, 8, 10, 12, 14, 19, 21, 47};
       //for(int index=0;index<numbs.length;index++){
if
(numbs[0]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
if
(numbs[1]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
               
  if
(numbs[2]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}      
 if
(numbs[3]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}   
if
(numbs[4]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



} 

if
(numbs[5]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
if
(numbs[6]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
  
if
(numbs[7]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
if
(numbs[8]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
if
(numbs[9]%2 == 0)


{
countEvens++;
}
else {
countOdds++;



}
System.out.println("The number of even numbers is" + countEvens);
    System.out.println("The number of odd numbers is" + countOdds);
}}


no offense, but that is extremely inefficient code you are rocking there. I'd suggest using either a for or a for-each loop, like I suggested.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12178
  • View blog
  • Posts: 45,247
  • Joined: 27-December 08

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 07:09 PM

You should also look into a legible indentation and spacing convention set. Your code is horrid to read, no offense intended.
Was This Post Helpful? 1
  • +
  • -

#10 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 07:10 PM

View Postmacosxnerd101, on 15 December 2012 - 09:09 PM, said:

You should also look into a legible indentation and spacing convention set. Your code is horrid to read, no offense intended.

That's what copy + pasting does.
Was This Post Helpful? 0
  • +
  • -

#11 stvo131  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-December 12

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 07:31 PM

yeah, I kno that a for loop or a for-each loop is much, much more effective, but this is what my teacher wants. A kid in my class asked the same thing: "What if our arrary has 30000 elements in it?"

My teacher responded that we will go over how to do it without copying and pasting next week.
Was This Post Helpful? 0
  • +
  • -

#12 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 07:51 PM

teaching arrays before loops? why would anyone do that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1