Question

In: Computer Science

Q1: Write a method called testArray that accepts a one-dimensional array of any size and prints...

Q1: Write a method called testArray that accepts a one-dimensional array of any size and prints the following summary information: a) all array elements at an even index b) every fifth element c) all elements but the first and last Write a program that demonstrates this method by using an array of 20 random integers from 100 to 200.

Q2: Write a method called displayGrid that accepts a two-dimensional array of integer values from 0 to 99 and prints this array an evenly spaced set of rows and columns. Write a program that demonstrates this method by using 6 row by 4 column array of random integers from 0 to 99.

Q3: Edit your displayGrid method so that it accepts a two-dimensional array of double values and a threshold value and displays a two-dimensional grid of only those values that are greater than the given threshold. Any values not over this threshold should be printed as spaces. Write a program that demonstrates this method by using 6 row by 4 column array of random integers from 0 to 99. For example, display only elements with a value greater than the 10

Solutions

Expert Solution

1.

public class Tweet{
   public static void testArray (int arr[]){
       System.out.println("All elements at even index: ");
       for(int i = 0; i < arr.length; i += 2){
           System.out.print(arr[i] + " ");
       }
       System.out.println("\nEvery 5th element: ");
       for(int i = 0; i < arr.length; i += 5){
           System.out.print(arr[i] + " ");
       }
       System.out.println("\nAll elements except first and last: ");
       for(int i = 1; i < arr.length - 1; i++){
           System.out.print(arr[i] + " ");
       }
   }
  
   public static void main(String args[]){
       int arr[] = new int[20];
       for(int i = 0; i < 20; i++){
           arr[i] = (int) (Math.random() * 100 + 100);
       }
       testArray(arr);
   }
}

2.

public class Tweet{
   public static void displayGrid(int arr[][]){
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               if(arr[i][j] < 10) System.out.print(" ");
               System.out.print(arr[i][j] + " ");
           }
           System.out.println();
       }
   }
  
   public static void main(String args[]){
       int arr[][] = new int[6][4];
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               arr[i][j] = (int) (Math.random() * 100);
           }
       }
       displayGrid(arr);
   }
}

3.

public class Tweet{
   public static void displayGrid(int arr[][], int threshold){
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               if(arr[i][j] > threshold){
                   if(arr[i][j] < 10) System.out.print(" ");
                   System.out.print(arr[i][j] + " ");
               }
               else{
                   System.out.print(" ");
               }
           }
           System.out.println();
       }
   }
  
   public static void main(String args[]){
       int arr[][] = new int[6][4];
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               arr[i][j] = (int) (Math.random() * 100);
           }
       }
       displayGrid(arr, 40);
   }
}


Related Solutions

1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
In this third part, write a function that accepts an array of integers and its size as arguments.
in C++In this third part, write a function that accepts an array of integers and its size as arguments. The function should create a new array that is of half size the argument array (round up the fraction if the size of the argument array is odd). The value of the first element (Element 0) of the new array should be the sum of the first two elements of the argument array (Element 0 and 1). Element 1 of the...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.
C++ ProgramWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and returns only the odd index entries. Do this by first setting the even entries to 0, and then removing the 0 entries by using a logical array. The first line of your code should read function p = ReturnOddEntries(p) For example, if you run in the command window p = ReturnOddEntries([1.2 7.1 8.4 -42 100.1 7 -2 4 6]), then you should get p...
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with...
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with random integers between a given min and max value. It must accept for parameters, min and max values for the creation of random integers, and rows and columns for the number of rows and columns of the array. The method should return an array of rows by columns size, filled with random integers between min and max values. The second method is called printRA,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT