In: Computer Science
Pseudocode:
The first problem has multiple steps but is a little bit easier than the second.
Define a module main
In main, declare an array of Integers named goldFishPerChild with 4
elements
Set the values of the array to 6, 4, 3, 3 ( you may do both in 1 line)
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.
In main, write a loop to update the value of each element so that it is
2 less by using the accumulator pattern.
Define a function named linearSearchInStringArray
Has String parameter searchTerm : value you are searching for
Has String[] parameter inputArray : array you wish to search through
Has Integer parameter arraySize : number of elements in the array
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
Has Integer[] parameter inputArray : array of elements you are
counting through
Has Integer parameter arraySize : number of elements in array
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...
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)