COMPUTER SCIENCE ARRAYS

DELHI 2006:

3.a) Write function in C++ which accepts aninteger array and size as arguments andassign values into a 2D array of integers in the following format : If the array is 1, 2, 3, 4, 5, 6 The resultant 2D array is given below

1    2    3    4    5    6
1    2    3    4    5    0
1    2    3    4    0    0
1    2    3    0    0    0
1    2    0    0    0    0
1    0    0    0    0    0

If the array is 1, 2, 3.The resultant 2D array is given :

1    2    3
1    2    0
1    0    0

Solution:

void input (int a[ ],int size)
{ int b[size] [size];
for (int i=0;i.<size;i++)
{
for (int j=0;j<size;j++)
{
if(( i+j)>=size)
b[i][j]=0;
else
b[i][j]=a[j];
cout<<b[i][j]<<’\t’;
}
cout<<endl;
}
}

3.b) An array MAT[30][10] is stored in the memory along column wise with each element occupying 8 bytes of the memory. Find out the Base address and the address of element MAT[20][5] , if the location MAT[3][7] is stored at the address 1000.

Solution: Children, Try this answer as an assignment.

OUTSIDE DELHI 2006:

3.a) Write function in C++ which accepts an integer array and size as arguments and assign values into a 2D array of integers in the following format :

If the array is 1, 2, 3, 4, 5, 6
The resultant 2D array is given below :

1    0    0    0    0    0
1    2    0    0   0    0
1    2    3    0    0    0
1    2    3    4    0    0
1    2    3    4    5    0
1    2    3    4    5    6

If the array is 1, 2, 3
The resultant 2D array is given :

1    0    0
1    2    0
1    2    3

Solution:

void input (int a[ ],int size)
{ int b[size] [size];
for (int i=0;i.<size;i++)
{ for (int j=0;j<size;j++)
{ if(( i<j)
b[i][j]=0;
else
b[i][j]=a[j];
cout<<b[i][j]<<’\t’;
}
cout<<endl;
}
}

 

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