"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

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();

}

Write a function int ALTERSUM( int B[][5] , int N, int M) in c++ to find and return the sum of elements from all alternate elements of a two-dimensional array starting from B[0][0].

Hint: If the following is the content of the array

4 5 1
2 8 7
9 6 3
The function should add elements B[0][0], B[0][2], B[1][1], B[2][0], B[2][2]

CBSE 2010


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

#define x 10
#define y 5

int ALTERSUM(int B[][5], int M, int N)
{
int flag = 0, sum = 0;
for(int i = 0 ; i < M ; i++)
for(int j = 0 ; j < N ; j++)
{
if(flag == 0)
{ 
   sum = sum + B[i][j];
   flag = 1;
}
else
   flag = 0;
}

return sum;
}

void main()
{
int arr[x][y], r, c, i , j;

cout << "\n How many rows(<10)? " ;
cin >> r;

cout << "\n How many cols(< 5)?";
cin >> c;
/* Input elements */
for( i = 0 ; i < r ; i++)
for(j = 0 ; j< c ; j++)
{
cout << "\n Enter elements :";
cin >> arr[i][j];
}

/* display matrix */
for(i = 0; i < r  ; i++)
for(j = 0; j < c ; j ++)
    cout << arr[i][j] << " ";
cout<< endl;

int sum = ALTERSUM(arr, r, c);
cout << "\n The Sum of alternate elements = " << sum;

getch();
}


Saturday, March 27, 2010

Write a function in C++ to print the product of each column of a two dimensional integer array passed as the argument of the function.

                                                    CBSE 2008

Explain : If the two dimensional array contains

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

The output should appear as :

Product of Column 1 = 24
Product of Column 2 = 30
Product of Column 3 = 240
                                                                                             
#include<iostream.h>
#include<conio.h>
#define r 10
# define c 10
void ProCol(int a[][], int m , int n) 
// n represents column
// m represents row
{
int pro[n], i, j ;
for(i = 0; i  <  n ; i++)
{
pro[i] = 1;
for(j = 0 ; j  <  m ; j ++)
{
pro[i] = pro[i] * a[j][i];
}
cout << "\n Product of Column 1 : " <<  pro[i];
}
}

void main()
{
int arr[r][c];

int row, col, i, j;

// Input dimension ~
cout <<  "\n Enter row & Column :";
cin >>  row >>  col ;

// Input elements for the array ~
for( i = 0; i <  row ; i++)
for ( j = 0 ; j <  col ; j++)
{
cout << "\n Enter the element for position : "   << i+1 << " , " <<  j+1 ;
cin >>  arr[i][j];
}

// Display the array
for( i = 0;  i < row ; i++ )
{
for( j =0 ; j < col ; j ++)
    cout   <<  arr[i][j]   <<  "  ";
cout  <<   endl;
}

// Calling the function
ProCol(arr, row, col);

getch();

}

Wednesday, March 24, 2010

Define a function SWAPCOL() in C++ to swap (interchange) the first column elements with the last column elements, for a two dimensional integer array passed as the argument of the function.

Example : If the two dimensional array contains 

       2   1   4   9
       1   3   7   7 
       5   8   6   3
       7   2   1   2

After swapping of the content of 1st column and the last column, it should be :
      9   1   4   2
      7   3   7   1
      3   8   6   5
      2   2   1   7                                (CBSE 2009)


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

void swapcol(int a[][], int r, int c)
  
// r -> row, c ->col
{ int t;
for(int i = 0; i< r ; i++)
{
t = a[i][0];
a[i][0] = a[i][c-1];
a[i][c-1] = t;
}
}

void main()
{
int arr[10][10];
int m, n, i, j;
cout<<"\n Enter rows & columns :";
int m, n;
cin >> m >> n;

/* Input Array Elements */

for ( i = 0; i< m ; i++)
for( j = 0; j < n ; j++)
{
cout<< "\n Enter element for the array[ " << i+1 << " ] "<< " [ " << j+i << "]" ;
cin >> arr[i][j];
}

/* Swap function call */
swapcol(arr, m, n);

/* Display Array in Matrix form */

for( i = 0; i < m ; << i ++)
{ cout<< endl;
for (  j = 0; j < n ; j ++)
cout << arr[i][j] << "  ";
}

getch();
}




Wednesday, February 10, 2010

Write a program to store 20 integers in an array. Then, accept another integer value and search for that integer in the array.


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


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


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


int x;


cout<<" \n Enter an element to be searched for :";
cin>> x;


for(i = 0; i< n ; i++)
{
if(x == arr[i])
{
cout<<" \n The element is found in position : " << i+1;
getch();
exit(1);
}
}


cout<< "\n The element not found ";
getch();
}

Tuesday, February 9, 2010

Write a program to enter the names and monthly salaries of 10 employees in a company. Now check individual per annum salary. If annual salary is greater than or equal to Rs 2,50000/- , then print name, salary and a message 'tax to be paid'; else print name, salary, and a message 'no tax'.


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

void main()
{
char emp_nm[10], mon_sal[10];

/* ....Input .... */

for(int i = 0; i< 10; i++)
{
cout<<"\n enter the name of an employee :";
cin >> emp_nm[i];

cout<<"\n Enter the salary :";
cin>>mon_sal[i];
}


cout<< "Name" << "\t\t" << "Salary" << endl;
for(i = 0; i< 10; i++)
{


// Check annual salary ....
if(mon_sal[i]*12 >= 250000)
   cout<< emp_nm[i] << "\t\t"<< mon_sal[i] << "tax-to-be-paid"<< "\n" ;
else
   cout<< emp_nm[i] << "\t\t"<< mon_sal[i] << "no-tax"<< "\n" ;
}

getch();
}

Write a program to enter names; marks of 5 subjects for individual students for a class ( assume class can hold max. 30 students). Now, calculate total marks and print names , total in columnar format. (use 1D or 2D array )


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

void main()
{

char name[30] ;
int marks[50][50], tot [ 50];

for ( int i = 0; i < 30 ; i++)
{
cout << "\n Enter Name of the student ";
cin >> name [i];

tot[i] = 0;

cout<< "\n Enter marks for 5 subjects : ";
for (int j = 0; j < 5 ; j++)
{ 
cout<< "\n Sub " << j+1;
cin >> marks[i][j];
tot[i] = tot[i] + marks[i][j];
}
}
clrscr(); 
cout<<"\n\n Name \t\t Total \n\n";
for( i = 0; i<=18 ; i ++)
{
cout<< name[i]  <<"\t\t" << tot[i] <<  "\n" ;
}
cout<<"\n Press any key to continue..... ";
getch();

clrscr();
cout<<"\n\n Name \t\t Total \n\n";
for( i = 19; i< 30 ; i ++)
{
cout<< name[i]  <<"\t\t" << tot[i]  <<  "\n" ;
}
 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.