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
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);
}
}
1. (100 pts) Write a program that swaps the elements of an array pairwise. Start from...
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 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...
(Done in Eclipse Java) 1. Given an integer between 1—100 captured from a user, perform the following conditional actions: • If is odd, print Weird • If is even and in the inclusive range of 2 to 5, print Not Weird • If is even and in the inclusive range of 6 to 20, print Weird • If is even and greater than 20, print Not Weird Note: Validate that your algorithm works for all cases. 2. Read an integer...
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 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 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 ( +
"you will write a program in C++ which will read a list of up to 100 integers from the user, and print them back to the screen in sorted order. The user can indicate that they are done entering numbers by entering any negative value. Your program will store the entered numbers into an array. It will then sort the array, using the selection sort algorithm. After each iteration of the sorting algorithm, it will print the current state of...
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 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