Wap to input an integer array of order m*n and arrange them along the ROW
22---Wap to input an integer array of order m*n and arrange them along the
ROW in a way given below.
SAMPLE INPUT SAMPLE OUTPUT
3 2 4 1 5 5 3 1 2 4
4 5 7 9 0 9 5 0 4 7
import java.util.*;
class Pendulum_Array
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of elements: "); // Inputting the number of elements
int n = sc.nextInt();
int A[]=new int[n]; //original array
int B[]=new int[n]; //array for storing the result
/*Inputting the Array*/
for(int i=0; i<n; i++)
{
System.out.print("Enter Element "+(i+1)+": ");
A[i] = sc.nextInt();
}
/*Sorting the Inputted Array in Ascending Order*/
int t=0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(A[i]>A[j])
{
t=A[i];
A[i]=A[j];
A[j]=t;
}
}
}
/*Printing the Sorted Array*/
System.out.println("\nThe Sorted Array Is");
for(int i=0; i<n; i++)
{
System.out.print(A[i]+"\t");
}
int mid = (n-1)/2; //finding index of middle cell
int x = 1, lim = n-1-mid;
/*'x' is for accessing elements of array A[] and
'lim' is for the number of times we have to make this to-and-fro movement*/
/* Pendulum Arrangement Starts Here */
B[mid]=A[0]; //putting the minimum element in the middle cell
for(int i=1; i<=lim; i++)
{
if((mid+i)<n) //going to the right side
B[mid+i]=A[x++];
if((mid-i)>=0) //going to the left side
B[mid-i]=A[x++];
}
/*Printing the Result*/
System.out.println("\n\nThe Result Is");
for(int i=0; i<n; i++)
{
System.out.print(B[i]+"\t");
}
}
}OUTPUT---
Enter number of elements: 5
Enter Element 1: 1
Enter Element 2: 2
Enter Element 3: 3
Enter Element 4: 4
Enter Element 5: 5
The Sorted Array Is
1 2 3 4 5
The Result Is
5 3 1 2 4
ROW in a way given below.
SAMPLE INPUT SAMPLE OUTPUT
3 2 4 1 5 5 3 1 2 4
4 5 7 9 0 9 5 0 4 7
import java.util.*;
class Pendulum_Array
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of elements: "); // Inputting the number of elements
int n = sc.nextInt();
int A[]=new int[n]; //original array
int B[]=new int[n]; //array for storing the result
/*Inputting the Array*/
for(int i=0; i<n; i++)
{
System.out.print("Enter Element "+(i+1)+": ");
A[i] = sc.nextInt();
}
/*Sorting the Inputted Array in Ascending Order*/
int t=0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(A[i]>A[j])
{
t=A[i];
A[i]=A[j];
A[j]=t;
}
}
}
/*Printing the Sorted Array*/
System.out.println("\nThe Sorted Array Is");
for(int i=0; i<n; i++)
{
System.out.print(A[i]+"\t");
}
int mid = (n-1)/2; //finding index of middle cell
int x = 1, lim = n-1-mid;
/*'x' is for accessing elements of array A[] and
'lim' is for the number of times we have to make this to-and-fro movement*/
/* Pendulum Arrangement Starts Here */
B[mid]=A[0]; //putting the minimum element in the middle cell
for(int i=1; i<=lim; i++)
{
if((mid+i)<n) //going to the right side
B[mid+i]=A[x++];
if((mid-i)>=0) //going to the left side
B[mid-i]=A[x++];
}
/*Printing the Result*/
System.out.println("\n\nThe Result Is");
for(int i=0; i<n; i++)
{
System.out.print(B[i]+"\t");
}
}
}OUTPUT---
Enter number of elements: 5
Enter Element 1: 1
Enter Element 2: 2
Enter Element 3: 3
Enter Element 4: 4
Enter Element 5: 5
The Sorted Array Is
1 2 3 4 5
The Result Is
5 3 1 2 4
Comments
Post a Comment