Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Activity 2 - Counting items in an Array that Meet a Certain Criterion In this ac

ID: 650355 • Letter: A

Question

Activity 2 - Counting items in an Array that Meet a Certain Criterion In this activity, you will work with a loop that counts how many of the elements in an array meet a given criterion. We start with a loop that counts the number of array elements that are even. To find out if the element meets this criterion, we use an if statement. Specifically, an array is created to contain the values of: 1, 4, 9, 4 and 8. The program will then use a loop to count how many of the values in the array are even. We can see that the values of 4. 4, and 8 are even and so the count should be 3. Create a new file, add the following code and save as file Array2.java. Read the comments of the code before running. Compile and run your program and verify that it runs correctly. Confirm that the output is correct for the sample data. Note that the program should print a count of 3, since there are 3 even numbers stored in the array. Modify the condition so that instead of counting even numbers, it counts odd numbers. Compile and run this new version of Array2 and verify the output.

Explanation / Answer

Hope it may helps you ....such a easy one you just change the logic in if condition to not equal to like

if(myArray[i]%2!=0)

you can view full code here.....

public clas Array2{
public static void main(String arg[]){
//declare variables..
int []myArray={1,4,9,4,8};

int count=0;
for(int i=0;i<myArray.length;i++){
   if(myArray[i]%2!=0){//here just put notequal to zero...which are not even
   count++;
   }
}
System.out.println("The number of odd values is"+count);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote