Question

In: Computer Science

Pseudocode: The first problem has multiple steps but is a little bit easier than the second....

Pseudocode:

  1. The first problem has multiple steps but is a little bit easier than the second.

    1. Define a module main

    2. In main, declare an array of Integers named goldFishPerChild with 4

      elements

    3. Set the values of the array to 6, 4, 3, 3 ( you may do both in 1 line)

    4. You tried to teach them responsibility, but your kids are terrible with

      pets. Each child killed two of its goldfish. This is a learning

      experience for them and for you.

    5. In main, write a loop to update the value of each element so that it is

      2 less ​by using the accumulator pattern​.

  2. Define a function named linearSearchInStringArray

    1. Has String parameter searchTerm : value you are searching for

    2. Has String[] parameter inputArray : array you wish to search through

    3. Has Integer parameter arraySize : number of elements in the array

    4. Returns Boolean : True if search term is one of the values in the

      array, False if not.

    CLUE: use count-controlled loop to iterate through all elements CLUE: needs to use a decision structure each iteration
    CLUE: can simply return True if you find a match!
    CLUE: consider when you know you can return false.

    CLUE: drawing pictures can help

3. Define a function named evenCount

  1. Has Integer[] parameter inputArray : array of elements you are

    counting through

  2. Has Integer parameter arraySize : number of elements in array

  3. Returns Integer : number of elements with even-numbered values in

    array

CLUE: This will require a loop and an extra variable to keep a running total

CLUE: this is a great opportunity to use the "%" operator. Look up "modulus" operator. This operator tells you the 'remainder' of a division operation.

A remainder of 0 means the number can be evenly divided...

Solutions

Expert Solution

1.

public static void main(String[] args) {
    int[] goldFishPerChild = {6, 4, 3, 3};
    for(int i=0; i<4; i++){
      goldFishPerChild[i] = goldFishPerChild[i] - 2;
}

2.

public static boolean linearSearchInStringArray(String searchTerm, String[] inputArray, int arraySize){
    for(int i=0; i<arraySize; i++){
      if(inputArray[i]==searchTerm){
        return true;
      }
    }
    return false;
}

3.

public static int evenCount(int[] inputArray, int arraySize){
    int count=0;
    for(int i=0; i<arraySize; i++){
      if(inputArray[i] % 2 == 0){
        count++;
      }
    }
    return count;
}

Driver function to test:

class Main {

  public static boolean linearSearchInStringArray(String searchTerm, String[] inputArray, int arraySize){
    for(int i=0; i<arraySize; i++){
      if(inputArray[i]==searchTerm){
        return true;
      }
    }
    return false;
  }

  public static int evenCount(int[] inputArray, int arraySize){
    int count=0;
    for(int i=0; i<arraySize; i++){
      if(inputArray[i] % 2 == 0){
        count++;
      }
    }
    return count;
  }

  public static void main(String[] args) {
    int[] goldFishPerChild = {6, 4, 3, 3};
    for(int i=0; i<4; i++){
      goldFishPerChild[i] = goldFishPerChild[i] - 2;
    }
    String[] str = {"Ronaldo", "Messi", "Kaka"};
    System.out.println("String exists?: "+linearSearchInStringArray("Messi",str,3));

    int[] num = {1, 2, 3, 4, 5, 6};
    System.out.println("Number of evens: "+evenCount(num,6));

  }
}

(Feel free to give an upvote or comment below for any doubts)


Related Solutions

(Multiple Select). First, indicate whether the following is an argument or not. Second, if it is...
(Multiple Select). First, indicate whether the following is an argument or not. Second, if it is an argument identify the conclusion. "Ethics is a matter of opinion. Different people and cultures have different beliefs about whether actions like stealing are right or wrong." Group of answer choices Yes, this is an argument. "Atmospheric carbon dioxide has increased substantially..." is the conclusion "it is likely that global warming is the result of burning fossil fuels" is the concluison No, this is...
For each, indicate whether the first item is (greater than, equal to, less than) the second...
For each, indicate whether the first item is (greater than, equal to, less than) the second item oxygen content in the pulmonary veins      ___________       oxygen content in the carotid arteries Answer 1Choose...greater thanequal toless than maximum pressure in the aorta ___________     maximum pressure in the left atrium   Answer 2Choose...greater thanequal toless than blood flow through the lungs ____________ blood flow through the kidneys Answer 3Choose...greater thanequal toless than flow of blood through a dilated vessel _____________ flow of blood through...
Give two examples explaining where using conservation of energy to solve a problem is easier than...
Give two examples explaining where using conservation of energy to solve a problem is easier than other the methods. Explain how you would use energy to solve the problem and why it is easier. 75-150
You wish to test the claim that the first population mean is greater than the second...
You wish to test the claim that the first population mean is greater than the second population mean at a significance level of α=0.005 Ho:μ1=μ2 Ha:μ1>μ2 You obtain the following two samples of data. Sample #1 Sample #2 57.7 63.3 64.3 70.1 53.6 69.6 72.2 67.3 70.1 31.0 55.7 63.8 71.4 71.4 83.6 87.9 102.3 34.9 84.5 75.1 78.5 58.8 What is the test statistic for this sample? test statistic = _____________ Round to 3 decimal places. What is the...
You wish to test the claim that the first population mean is less than the second...
You wish to test the claim that the first population mean is less than the second population meanat a significance level of α=0.001α=0.001.      Ho:μ1=μ2Ho:μ1=μ2 Ha:μ1<μ2Ha:μ1<μ2 You obtain the following two samples of data. Sample #1 Sample #2 43.3 56.2 64.2 49.7 95.4 61.0 85.0 49.1 50.9 49.1 61.0 63.2 72.7 81.7 41.4 55.8 54.5 83.6 93.5 79.2 74.9 77.8 49.8 48.8 43.5 90.5 90.5 76.2 47.7 What is the test statistic for this sample? test statistic =   Round to 3...
You wish to test the claim that the first population mean is greater than the second...
You wish to test the claim that the first population mean is greater than the second population mean at a significance level of α=0.10 .      Ho:μ1=μ2 Ha:μ1>μ2 You obtain the following two samples of data. Sample #1 Sample #2 76.2 68.1 90.1 74.7 77.1 72.9 70.3 84.0 75.9 80.1 65.7 80.1 76.2 82.0 81.7 74.5 75.1 73.3 45.5 47.8 46.1 55.5 53.7 40.0 67.4 54.6 What is the test statistic for this sample? test statistic = Round to 3...
You wish to test the claim that the first population mean is greater than the second...
You wish to test the claim that the first population mean is greater than the second population mean at a significance level of α=0.002α=0.002.      Ho:μ1=μ2 Ha:μ1>μ2 You obtain the following two samples of data. Sample #1 Sample #2 61.3 55.4 65.4 65.4 65.7 51.0 61.4 61.8 59.4 56.6 60.7 52.0 62.4 24.3 37.2 1.5 42.2 53.5 35.9 65.9 48.3 17.8 49.8 42.6 What is the test statistic for this sample? test statistic =  Round to 4 decimal places. What is...
You wish to test the claim that the first population mean is less than the second...
You wish to test the claim that the first population mean is less than the second population mean at a significance level of α=0.01α=0.01.      Ho:μ1=μ2Ho:μ1=μ2 Ha:μ1<μ2Ha:μ1<μ2 You obtain the following two samples of data. Sample #1 Sample #2 71.2 85.7 85.4 76.5 92.2 81.3 72.7 71.8 86.8 78.3 86.3 78.7 80.3 72.2 93.3 83.2 86.8 85.3 81.6 80.5 87.8 88.6 75.4 What is the test statistic for this sample? test statistic = Round to 3 decimal places. What is...
You wish to test the claim that the first population mean is less than the second...
You wish to test the claim that the first population mean is less than the second population mean at a significance level of α=0.001α=0.001.      Ho:μ1=μ2Ho:μ1=μ2 Ha:μ1<μ2Ha:μ1<μ2 You obtain the following two samples of data. Sample #1 Sample #2 41.5 47.1 71.9 67.8 57.1 72.9 52.3 70.3 65.5 68.8 66.2 69.3 70.4 87.8 79.7 89.4 81.0 70.4 71.4 68.8 91.6 82.1 79.1 What is the test statistic for this sample? test statistic =  Round to 4 decimal places. What is the...
You wish to test the claim that the first population mean is less than the second...
You wish to test the claim that the first population mean is less than the second population mean at a significance level of α=0.01α=0.01.      Ho:μ1=μ2Ho:μ1=μ2 Ha:μ1<μ2Ha:μ1<μ2 You obtain the following two samples of data. Sample #1 Sample #2 71.2 85.7 85.4 76.5 92.2 81.3 72.7 71.8 86.8 78.3 86.3 78.7 80.3 72.2 93.3 83.2 86.8 85.3 81.6 80.5 87.8 88.6 75.4 What is the test statistic for this sample? test statistic = Round to 3 decimal places. What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT