Question

In: Computer Science

public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return...

public int getIndexOfWord(String[] arr, String word){
// if found, return the index of word, otherwise return -1

}

public boolean areArrays(int[] arr1, int[] arr2){
// 1. initial check: both arrays need to have the same length, return false if not the same
// 2. return true if both given arrats are equals(same values in the same indices), false otherwise

}


public boolean areArraysEqual(String[] arr1, String[] arr2){
// 1. initial check: both arrays need to have the same length, return false if not the same
// 2. return true if both given arrays are equals(same values in the same indices), false otherwise

}

Solutions

Expert Solution

public int getIndexOfWord(String[] arr, String word) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(word))
                return i;
        }
        return -1;

    }

    public boolean areArrays(int[] arr1, int[] arr2) {
        if (arr1.length != arr2.length)
            return false;
        for (int i = 0; i < arr1.length; i++)
            if (arr1[i] != arr2[i])
                return false;
        // 1. initial check: both arrays need to have the same length, return false if
        // not the same
        // 2. return true if both given arrats are equals(same values in the same
        // indices), false otherwise
        return true;
    }

    public boolean areArraysEqual(String[] arr1, String[] arr2) {
        // 1. initial check: both arrays need to have the same length, return false if
        // not the same
        // 2. return true if both given arrays are equals(same values in the same
        // indices), false otherwise
        if (arr1.length != arr2.length)
            return false;
        for (int i = 0; i < arr1.length; i++)
            if (!arr1[i].equals( arr2[i]))
                return false;
        // 1. initial check: both arrays need to have the same length, return false if
        // not the same
        // 2. return true if both given arrats are equals(same values in the same
        // indices), false otherwise
        return true;

    }

Related Solutions

class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
public boolean isPrime(int num){ // return true if the number is prime, false otherwise } public...
public boolean isPrime(int num){ // return true if the number is prime, false otherwise } public boolean isOdd(int num){ // return true if the number is odd, false otherwise } public String reverseMyString(String input){ // using a loop, reverse a string // i.e. input = "hello", reversed answer: "olleh" // i.e. input = "bye", reversed answer: "eyb" } public int pow(int base, int power){ // using for loop, calculate base raised to power // i.e. base = 2, power =...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index - 1; // Decrementing the index System.out.printf("%d%n", a[index]); reverse(a, index); // Recursive call } return; } public static void main (String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int n=array.length; reverse(array,n); // function call } } Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or...
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int containsWithin(const int *arr,int size , int k,int i , int j); int *paddedCopy(const int *arr,int oleSize , int newSize); void reverse(int *arr,int size); int *reverseCopy(const int *arr,int size); //array_utils.c #include"array_utils.h" #include<stdlib.h> int contains(const int *arr,int size , int k){    int i=0;    for(i=0;i<size;i++){        if(arr[i] == k)return 1;    }    return 0; } int containsWithin(const int *arr,int size , int k,int...
FILE *fin = NULL; fin = fopen(in.txt, "r"); int *arr = (int *) malloc (sizeof (int)...
FILE *fin = NULL; fin = fopen(in.txt, "r"); int *arr = (int *) malloc (sizeof (int) * fin); ***************************************************** If I want to open a in.txt, only the integer in the file, is the malloc code correct? What if the in.txt includes the string and integer, how can I use the malloc?   Thanks
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT