COMPUTER SCIENCE ARRAYS

DELHI 2002:

3.a) Define array and pointer.

Solution: An array refer to a named list of a finite number n of similar data elements. Each of the data elements can be referenced respectively by a set of consecutive numbers. Arrays can be one dimensional, two dimensional or multi dimensional. An array can be declared as :

Syntax: data_type Array_name[size];

Eg: int A[10]; //Then location of the//array are A[0], A[1],…….A[9].
      int B[5][4];//This array can holds 5 X 4 = 20 elements.

3.d) The array A[20][10] is stored in the memory with each element requiring one byte of storage if the base address of a is 0, determine the location of A[10][5] when the array A is stored by column major.

Solution: Children, Try this answer as an assignment.

3.c) Considering the following key set: 42,29,74,11,65,58, use insertion sort to sort the data in ascending order and indicate the sequences of steps required.

Solution:In this, Suppose an array A with n elements A[1],A[2],…A[N] is in memory. The insertion sort algorithm scans A from A[1] to A[N], insertion each element A[K] into its proper position in the previously sorted subarray A[1],A[2],…,A[K-1].

This sorting algorithm is frequently used when n is small.
The array contains 6 elements as follows:42,29,74,11,65,58

Pass

Pass A[0] A[1 ] A[2] A[3] A[4] A[5] A[6]
K=1 -32768 42 29 74 11 65 58
K=2 -32768 42 29 74 11 65 58
K=3 -32768 29 42 74 11 65 58
K=4 -32768 29 42 74 11 65 58
K=5 -32768 11 29 42 11 65 58
K=6 -32768 11 29 42 65 74 58
Sorted -32768 11 29 42 58 65 74

 

DELHI 2001

3.a) Given two arrays of integers X and Y of sizes m and n respectively. Write a function named MERGE() which will third array named Z, such that the following sequence is followed.

(i) All odd numbers of X from left to right are copied into Z from left to right.
(ii) All even numbers of X from left to right are copied into Z from right to left.
(iii) All odd numbers of Y from left to right are copied into Z from left to right.
(iv) All even numbers of Y from left to right are copied into Z from right to left. X, Y and Z are passed as arguments to MERGE().

Eg. X is {3, 2, 1, 7, 6, 3} and {9, 3, 5, 6, 2, 8, 10} the resultant array Z is {3, 1, 7, 3, 9, 3, 5, 10, 8, 2, 6, 6, 2}

void MERGE(int X[ ], int m,int Y[ ],int
n,int Z[ ])
{ int mn,i,,left=0,right=mn-1;
mn=m+n;
for(i=0;i<m;i++)
if (X[i]%2= = 1)
Z[left++]=X[i];//For copying odd numbers of X into Z from left to right
else
Z[right- -]=X[i];
//For copying even number of X into Z from right to left
for(i=0;i<n;i++)
if (X[i]%2= = 1)
Z[left++]=Y[i];
//For copying odd numbers of Y into Z from left to right
else
Z[right- -]=Y[i];
//For copying even number of X into Z from right to left
}

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )