"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Sunday, March 28, 2010

Write a function in C++ which accepts an integer array and its size as arguments / parameters and assign the elements into a two dimensional array of integers in the following format:

                                                                         CBSE 2006
If the array is 1 2 3 4 5 6
The resultant 2 D 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 2 D array is given below

1 2 3
1 2 0
1 0 0


#include<iostream.h>
#include<conio.h>

void form_matrix(int ar[], int m)
{
// forming the matrix 
int mat[m][m];
int max = m;
int min = 1;
for(int i = 0; i < m ; i++)
{
min= 1;
for(int j = 0; j < m ; j++, min++)
{
if(min < = max )
   mat[i][j] = min;
else
   mat[i][j] = 0;
}
max--;
}

cout<<" \n The matrix :";
// Displaying the matrix
for( i = 0 ; i < m; i++)
{
cout << "\n ";
for( j = 0 ; j< m ; j++)
cout << mat[i][j] << " ";
}

}

void main()
{
int m[10], sz, i, j;

cout<< " \n What is the size of an array ?";
cin >>  sz;

/* for inputing the  elements.....*/
for( i = 0 ; i < sz; i++)
m[i] = i+1;

cout << "\n The array ";
/* display array */
for( i = 0; i < sz ; i++)
cout<< m[i];

 /* call the function */
form_matrix(m, sz);

getch();
}

Write a function REASSIGN() in c++ , which accepts an array of integers and its size as parameters and divide all those array elements by 5 which are divisible by 5 and multiply other array elements by 2

CBSE 2010


#include<iostream.h>
#include<conio.h>

void REASSIGN(int a[], int size)
{
for(int i = 0 ; i < size ; i++)
{
if(a[i]%5 == 0)
  a[i] = a[i] / 5;
else 
   a[i] = a[i] * 2;
}

void main()
{
int arr[25];
int n, i;

cout << "\n Enter the size of an array (<25):":
cin >> n;

for( i = 0 ; i < n ; i++)
{cout<< "\n Enter an element :";
cin >> arr[i];
}

/* Display original array */
cout<< "\n The Original array :";
for(i = 0 ; i < n ; i++)
cout<< arr[i] << " ";

REASSIGN(arr, n);

cout<< "\n The changed array :";
/* Display the changed array */
for(i = 0 ; i < n ; i++)
cout<< arr[i] << " ";

getch();

}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.