Arrays (Solved)

download Arrays (Solved)

of 14

description

Arrays

Transcript of Arrays (Solved)

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 1

    Chapter No.9(Solved Questions on Arrays- 8 Marks)

    OUTSIDE DELHI 20133.a) Write a function void ChangeOver(int P[ ], int N) in C++, which re-positions all the elements of thearray by shifting each of them to the next position and by shifting the last element to the first position.Example:If the content of array P is12, 15, 17, 13, 21The content of array P should become21, 12, 15, 17, 13Ans:- void ChangeOver(int P[ ],int N)

    { int temp=A[N-1];for(i=N-2; i>=0; i--)

    A[i+1]=A [i];A [0]=temp;

    }3.c) Write a function DispTen (int A [ ][4], int N, int M) in C++ to find and display allthe numbers which are divisible by 10.For example:If the array is containing:12 20 132 10 30The output will be: 20 10 30Ans:- void DispTen (int A [ ][4], int N, int M)

    { for(int i=0; i

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 2

    }}

    }

    3.d) Write a function ALTERNATE (int A [ ][3], int N, int M) in C++ to display all alternateelements from two dimensional array A (starting from A [0][0]).For example:If the array is containing:23 54 7637 19 2862 13 19The output will be: 23 76 19 62 19Ans:- void ALTERNATE (int A [ ][3], int N, int M)

    { int c=0;for(int i=0;i

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 3

    OUTSIDE DELHI 20103.a) Write a function REASSIGN( ) in C++, which accepts an array of integers and its size asarguments and divide all those array elements by 5 which are divisible by 5 and multiply otherarray elements by 2.Ans:- void REASSIGN (int A[ ], int n){ for(int i=0;i

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 4

    3.d)Write a function SWAPCOL ( ) in C++ to swap the first column elements with the lastcolumn elements, for a two dimensional array passed as the arguments of the function.Example: If two dimensional arrays contains2 1 4 9

    1 3 7 7

    5 8 6 3

    7 2 1 2

    After swapping array should be9 1 4 2

    7 3 7 1

    3 8 6 5

    2 2 1 7

    Solution:void SWAPCOL(int A[ ][ ], int r, int c){ int i, j, temp;

    for(i=0;i

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 5

    3.d)Write a function in C++ to print the product of each column of a two dimensional arraypassed as the arguments of the function.Example : If the two dimensional array containsThen the output should appear as:

    Product of Column 1 = 24Product of Column 2 = 30Product of Column 3 =240

    Solution:void productcol(int A[ ][ ],int r,int c){ int i,j;

    for(j=0;j

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 6

    Solution:void productrow(int A[ ][ ],int r,int c){ int i,j;

    for(i=0;i

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 7

    Solution: void daigonalarray(int a[ ][ ],int n){ cout

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 8

    Solution: void middlearray(int a[ ][ ],int size){ cout

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 9

    Solution:void input (int a[ ],int size){ int b[size] [size];

    for (int i=0;i

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 10

    DELHI 20043.a) Define the function SwapArray(int[ ], int),that would expect a 1D integer arrayNUMBERS and its size N. the function should rearrange the array in such a way that the valuesof that locations of the array are exchanged. (Assume the size of the array to be even).Example :If the array initially contains {2, 5, 9, 14, 17, 8, 19, 16}Then after rearrangement the array should contain {5, 2, 14, 9, 8, 17, 16, 19}Solution: Dear students, Try this answer as an assignment.

    3.c) Write a function in C++ to find the sum of diagonal elements from a 2D array of type float.Use the array and its size as parameters with float as its return type.Solution: float displaysum( int A[ ][ ],int n)

    { float sum=0;for(int i=0;i

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 11

    3.c)Write a user-defined function in C++ to display those elements of 2D array T[4][4] which aredivisible by 100. Assume the content of the array is already present and the function prototype isas follows: void showhundred( int T[4][4]);

    void showhundred(int T[4][4]){ int i,j;

    cout

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 12

    Solution: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

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 13

    else{ while(b>=0)

    C[c++]=B[b--];}

    }

    3.b) An array VAL[115][110] is stored in the memory with each element requiring 4 bytesof storage. If the base address of the array VAL is 1500, determine the location of VAL[12][9]when the array VAL is stored (i) Row wise (ii) Column wise.Solution: Given Data:

    VAL[115][110]Word Length (W) = 4 BytesBase Address of VAL(B) = 1500VAL[12][9] = ?Nc = Total No of Columns=10 Nr = Total No of Rows=15

    Lr = Least Row=1 Lc = Least Column=1( i ) Row Major:

    Address of an element (I,J) in row major = B + W (Nc (I-Lr) + (J Lc))VAL [12][9] = 1500 + 4 (10 * (12-1) + (9-1))

    = 1500 + 4 (10 * 11+8)= 1500 + 4 (118)= 1972.

    ( ii ) Column Major:Address of an element (I,J) in column major = B + W ( (I-Lr) + Nr (J Lc))

    VAL [12][9] = 1500 + 4 ((12-1) +15 * (9-1))= 1500 + 4 (11 + 15 * 8)= 1500 + 4 ( 11+ 120)= 1500 + 4 * 131= 1500 + 524= 2024.

    3.c) Write a user-defined function in C++ to find and display the sum of diagonal elements froma 2D array MATRIX[6][6] containing integers.Solution: Dear students, Try this answer as an assignment

    DELHI 19993.a) Suppose a 1D array AR containing integers is arranged in ascending order. Write a userdefined function in C++ to search for one integer from AR with the help of binary searchmethod, to show presence of the number in the array. The function should have three parameters:(1) an array AR (2) the number to be searched and (3) the number of elements N in the array.

    void BinSearch(int AR[ ], int item, int N){ int beg=0, last= N-1,mid, flag=0;

    while(beg

  • Computer Science, Class XII (2013-14)

    Mohd. Hashim, PGT (Computer Sc.), E-mail: [email protected], 7417786626 14

    last=mid-1;else

    beg=mid+1;}if( flag = = 0)

    cout