Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2002 SOLVED QUESTION 2 : ARRAY

Saturday, March 16, 2019

ISC COMPUTER SCIENCE PRACTICAL 2002 SOLVED QUESTION 2 : ARRAY



  Write a program to do the following:- Read an integer n. (where can be at most 50.) then
  read n integers one by one and store them in array data from,
  index 0 to n-1. Now we are to rearrange the integers in data in the following ways:-
  Find the maximum value in data and put it in the center of the array (that is at n/2),
  find the next largest value and put it to its right, and so on alternating right and
  left until all integers in data are done. For example if the array is initially:
  Sample:
 Give the number of integers: 6
 INPUT: 5 1 9 8 2 4
 OUTPUT:  2 5 9 8 4 1
 The idea behind this program is as follows
1. Input the array length and array elements.
2. Sort the array in descending order using any standard sorting technique
3. Find the middle postion according as array length is even or odd
4. Transfer the first element of the sorted ( descending order) in a new array
5. From the 2nd elemets onward, alternately put the elements on the right and left 
    of middle position and  increase the right and left counter
To achieve Step 3, the following code has put to work
pos = (array_length%2==0)? (array_length/2-1):(array_length/2);

To uderstand it consider the following input:
Give the number of integers: 6

 INPUT: 5 1 9 8 2 4
Since 6 is even, pos = 6/2-1=2
After sorting the array it becomes
9 8 5 4 2 1
Now the first element '9' goes at position 0 of  new array.
Now these elements 8[1], 5[2],4[3],2[4],1[5] of need to be inserted old array 
where a[b] represent value at position b.
New array at this point looks like:

Empty[0],Empty[1],9[2],Empty[3],Empty[4],Empty[5]

Now 8[1] will go at Empty[3]
        5[2] will got at Empty[1]
        4[3] will go at Empty[4]
        2[4] will go at Empty[0]
        1[5] will go at Empty[5]
See that elements at odd position goes at position pos+right 
       1 -----  2+1
       3 -----  2+2
       5 -----  2+3
So, it seems obvious to declare a right counter to 1 and increment it by 1 for the next step.
Similar arguments, goes for elements at even position.

The following code carries out this process.
       int pos,right=1,left=1;
       pos = (array_length%2==0)? (array_length/2-1):(array_length/2);
       final_arr[pos]=arr[0];
       for(int t=1;t<=array_length-1;t++) {
              if(t%2 != 0) {
                     final_arr[pos+right]=arr[t];
                     right++;
                     }
              else {
                     final_arr[pos-left]=arr[t];
                     left++;
                     }
              }

See that the for loop runs from 1 to 5
whenever the postion is odd, we are putting the element at the right of middle position
and increasing the right counter and similarly for the elements of even position.


JAVA CODE

import java.util.*;
public class ISC2002Q2 { 
       public static void main(String[] args) {
              int N;
              Scanner in = new Scanner(System.in);
              do {
                 System.out.println("Enter the value of N (<=50):");
                 N = in.nextInt();
                 if(N >50)
                        System.out.println("INVALID INPUT,TRY AGAIN");
                 }while(N>50);
              Array obj = new Array(N);
              System.out.println("Enter the elements of the array:");
              obj.inputArray();
              System.out.println("Entered elements of the array:");
              obj.display();
              obj.generateNewArray();
              in.close();                 
       }
}
class Array {
       public void generateNewArray() {
              int temp;
              // sort the array in descending order
              for(int i = 0; i < array_length; i++) {
            for(int j=1; j < (array_length-i); j++) {
               if(arr[j-1] < arr[j]) {
                  temp = arr[j-1];
                  arr[j-1] = arr[j];
                  arr[j] = temp;
               }
            }
         }
       // putting the elements at required position   
       int pos,right=1,left=1;
       pos = (array_length%2==0)? (array_length/2-1):(array_length/2);
       final_arr[pos]=arr[0];
       for(int t=1;t<=array_length-1;t++) {
              if(t%2 != 0) {
                     final_arr[pos+right]=arr[t];
                     right++;
                     }
              else {
                     final_arr[pos-left]=arr[t];
                     left++;
                     }
              }
       // displaying the new array
       System.out.println();
       System.out.println("Array after required insertion:");
       for(int i=0;i<array_length;i++)
              System.out.print(final_arr[i]+" ");
       }
       public void display() {
              for(int i=0;i<array_length;i++)
                     System.out.print(arr[i]+" ");
       }
       public void inputArray() {
              Scanner in = new Scanner(System.in);
              for(int i=0;i<array_length;i++)
                     arr[i]=in.nextInt();
              in.close();
       }
       Array(int N) {
              array_length = N;
              arr = new int[array_length];
              final_arr = new int[array_length];
              }
       private int array_length;
       private int[] arr;
       private int[] final_arr;
}

2 comments:

  1. alvodhan nammala mudichivitinga ponga

    ReplyDelete