Question

In: Computer Science

4. Please name your driver program XXX_P04 where XXX are your initials. Given the array inputArray...

4. Please name your driver program XXX_P04 where XXX are your initials. Given the array inputArray (doubles), write a method called swap which will swap any two contiguous (next to each other) elements. Swap will be passed two parameters, the index of the first element to be swapped and the array name. Write another method printArray that will print the array 5 elements to a line. PrintArray will be passed one parameter the array name. See the videos for help with this. The array elements are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

Java Eclipse

Solutions

Expert Solution

Code Screenshot :

Executable Code:

public class XXX_P04
{
   //Main Program
   public static void main(String[] args)
   {
       //Declaring required array
       double[] inputArray = {1,2,3,4,5,6,7,8,9,10};
       printArray(inputArray);
       //Calling the method
       swap(5, inputArray);
       System.out.println();
       printArray(inputArray);
   }

   //Method to swap
   public static void swap(int k, double [] input) {
       //Swapping the elements
       double temp = input[k];
       input[k] = input[k+1];
       input[k+1] = temp;
   }

   //Method to print the result
   public static void printArray(double[] input) {
       for(int i = 0; i < input.length; ++i) {
           System.out.print(input[i] + " ");
           //Printing the result
           if((i+1) % 5 == 0) {
               System.out.println();
           }
       }
   }
}

Sample Output:

Please comment below if you have any queries.
Please do give a thumbs up if you liked the answer thanks :)


Related Solutions

Please name your driver program XXX_P03 where XXX are your initials. Please name your array utilities...
Please name your driver program XXX_P03 where XXX are your initials. Please name your array utilities class ArrayUtilities. Have only static methods in your ArrayUtilities class. Make sure your program will work for any array size. Write a driver program with a double array that is initialized with the following test data. You may hard code this data into your array. Echo the input in a neat table formatted exactly as below (5 elements to a line and aligned). Then...
Write a program (name it firstMiddleLast _yourInitials.java (yourInitials represents your initials) that will: 1. Ask the...
Write a program (name it firstMiddleLast _yourInitials.java (yourInitials represents your initials) that will: 1. Ask the user (include appropriate dialog) to enter their: first name middle name last name save each of the above as a String variable as follows: firstName middleName lastName 2. Print to the screen the number of characters in each of the names (first, middle and last) 3. Print to the screen total number of characters in all three names. Include appropriate dialog in your output....
In Java please Your program will sort a two dimensional array (5 * 4) based on...
In Java please Your program will sort a two dimensional array (5 * 4) based on the following: The entire array should be sorted using bubble sort based on the 1st column in ascending order and display the entire array. Reset the array to its original contents. The entire array should again be sorted using selection sort based on the 2nd column in descending order and display the entire array. Reset the array to its original contents. The entire array...
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
// This program performs a linear search on a character array // Place Your Name Here...
// This program performs a linear search on a character array // Place Your Name Here #include<iostream> using namespace std; int searchList( char[], int, char); // function prototype const int SIZE = 8; int main() { char word[SIZE] = "Harpoon"; int found; char ch; cout << "Enter a letter to search for:" << endl; cin >> ch; found = searchList(word, SIZE, ch); if (found == -1) cout << "The letter " << ch      << " was not found in...
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
You will write a single class named WordsXX- replace the XX's with your initials. The program...
You will write a single class named WordsXX- replace the XX's with your initials. The program will have 2 overloaded methods named wordMaker. They will have to be declared as static. Both methods will not have a return type but they will print some text. The first method wordMaker signature will not accept any parameters and when invoked will simply print out the line "lower case words". The second overloaded wordMaker method will accept an integer and print "Words in...
You will write a single class named WordsXX- replace the XX's with your initials. The program...
You will write a single class named WordsXX- replace the XX's with your initials. The program will have 2 overloaded methods named wordMaker. They will have to be declared as static. Both methods will not have a return type but they will print some text. The first method wordMaker signature will not accept any parameters and when invoked will simply print out the line "lower case words". The second overloaded wordMaker method will accept an integer and print "Words in...
Please use Java eclipse Find pair in an array with given sum Given an array of...
Please use Java eclipse Find pair in an array with given sum Given an array of integers A and an integer S, determines whether there exist two elements in the array whose sum is exactly equal to S or not. Display 1 a pair is found in an array with matching sum S else 0. Input     6     5     1 -2 3 8 7     Where, First line represents integer S. Second line represents the size of an array. Third line represents...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT