Question

1. (100 pts) Write a program that swaps the elements of an array pairwise. Start from...

1. (100 pts) Write a program that swaps the elements of an array pairwise. Start from the left of the array, take 2 elements at a time and swap the two elements. Continue in this fashion until you reach the end of the array. Declare an integer array of size 10 and place random numbers in the range [0?9]. Print the original array, pairwise swap the elements of the array and print the final array after swap. Consider the following example.

0123456789

After pairwise swap you get the following array

0123456789

In the array first two numbers 1 and 7 are swapped, 4 and 0 are swapped, 9 and 4 are swapped, 8 and 8 are swapped and 2 and 4 are swapped. Sample execution of the program is given below

Original array 1740948824

After pairwise swap 7104498842

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please Find the below Program for Swapping two consecutive values in an Array.

The Program has three functions:

1. generateArray() : It generates random number and inserts them in the Array. It is currently using the Math.random() function which generates random Values

2. printArray() : It prints the value of the array.

3. swapArrayValue() : It is having the logic which swaps the values inside the array. Logic is simple, take two values from the array at a time,swap them and insert it back to array and then pick the next two values till the array ends.

Note: Here as the array is of predefined size of 10, we do not need an necessary check if the size of the array is odd. In that case we would keep the last odd place value as it is.

The Program can also directly run. Copy the conten of the files in suppose "X.java". Open Command Prompt and go to the location where the file is kept.

Compile : javac X.java

Run : java X

public class SwapProgram {

   int[] randArray = new int[10];

   public void generateArray() {
       for (int i = 0; i < randArray.length; i++) {
           randArray[i] = (int) (Math.random() * 10);
       }
   }

   public void printArray(int[] array) {
       for (int i = 0; i < array.length; i++) {
           System.out.print(array[i]);
       }
   }

   public void swapArrayValue() {
       int temp;
       /*
       * Swapping Logic Here the loop will iterate and would take on two
       * consecutive values and swap them for e.g : in first run i=0,j=1 ->
       * Swap the two values in secound run i=2,j=3 -> Swap the two Values
       */
       for (int i = 0, j = 1; i < randArray.length; i += 2, j += 2) {
           temp = randArray[i];
           randArray[i] = randArray[j];
           randArray[j] = temp;
       }
   }

   public static void main(String[] args) {
       SwapProgram sp = new SwapProgram();

       // Generate Array of Random Integers
       sp.generateArray();

       // Print the Generated Array
       System.out.println("Generated Array:");
       sp.printArray(sp.randArray);

       // Swap the Array Value
       sp.swapArrayValue();

       // Print the New Swapped Array
       System.out.println("\nSwapped Array:");
       sp.printArray(sp.randArray);
   }

}

Add a comment
Know the answer?
Add Answer to:
1. (100 pts) Write a program that swaps the elements of an array pairwise. Start from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider a subroutine swap that takes two parameters and simply swaps their values. For example, after...

    Consider a subroutine swap that takes two parameters and simply swaps their values. For example, after calling swap(X,Y), X should have the original value of Y and Y the original value of X. Assume that variables to be swapped can be simple or subscripted (elements of an array), and they have the same type (integer). Show that it is impossible to write such a general-purpose swap subroutine in a language with: Parameter passing by name. Hint: for the case of...

  • this program is in C. Write a program that computes the number of elements in an...

    this program is in C. Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2,2,4}). Sample execution of the program for this array...

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in -long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; -But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • Description: Write a complete C++ program that will do the following: 1. Declare an array of...

    Description: Write a complete C++ program that will do the following: 1. Declare an array of 30 elements 2. Set the array to random numbers between 0 to 100 inclusive. Don't seed the random number generator. 3. Output the array. 4. Sort the array in ascending order using the selection sort. 5. Output the sorted array. Required IO: Array by F. Last Original: 1 5 ... 2 10 + Sorted: 1 2 ... 5 10 ( +

  • JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the...

    JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the size of the array: 1 Array size must be greater than 1. Please reenter: 0 Array size must be greater than 1. Please reenter: -1 Array size must be greater than 1. Please reenter: 8 Please choose an option: 1 Print the array 2 Find the average 3 Find the largest element 4 Count how many times 3 occurred 5 Count how many elements...

  • Write a Java program to remove the duplicate elements of a given array and return the...

    Write a Java program to remove the duplicate elements of a given array and return the new length of the array. Sample array: [20, 20, 32, 76, 30, 40, 50, 50, 52] After removing the duplicate elements the program should return 6 as the new length of the array. Out put Original array length: 9 Array elements are: 20 20 32 76 30 40 50 50 52 The new length of the array is: 7

  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

  • Swapping Values in python Summary In this lab, you complete a Python program that swaps values...

    Swapping Values in python Summary In this lab, you complete a Python program that swaps values stored in three variables and determines maximum and minimum values. The Python file provided for this lab contains the necessary input and output statements. You want to end up with the smallest value stored in the variable named first and the largest value stored in the variable named third. You need to write the statements that compare the values and swap them if appropriate....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT