RSS

Category Archives: Matrices

PROGRAM TO FIND THE SUM OF NON CORNER ELEMENTS OF A MATRIX

// THIS PROGRAM FINDS THE SUM OF THE NON CORNER ELEMENTS OF A MATRIX

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

void main()
{
clrscr();
int arr[100][100],size;
cout<<“\nEnter the size of the matrix \n”;
cin>>size;
cout<<“\nInput the matrix \n”;
for(int i =0; i<size; i++)
for(int j =0; j<size; j++)
cin>>arr[i][j];
int sum = 0;
for(i =0; i<size; i++)
for(j =0; j<size; j++)
if((i != 0 && i != size -1) || (j!=0 && j!= size -1))
sum += arr[i][j];
cout<<“\nThe sum of the non corner elements is “<<sum;
getch();
}

Advertisements
 
Leave a comment

Posted by on October 20, 2015 in Matrices

 

PROGRAM TO FIND THE SUM OF A COLUMN IN A MATRIX (USING POINTERS)

// THIS PROGRAM FINDS THE SUM OF A COLUMN IN A MATRIX USING POINTERS

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

void main()
{
clrscr();
int size;
cout<<“\nEnter the size of the matrix \n”;
cin>>size;

int **arr = new int *[size];

for(int i =0; i<size; i++)
{
*(arr + i) = new int[size];
}

cout<<“\nInput the matrix \n”;

for(i = 0; i<size; i++)
{
for(int j =0; j<size; j++)
{
cin>> *(*(arr+i) + j);
}
}

cout<<“\nEnter the column number whose sum you want to find\n”;
int col = -1,sum = 0;
cin>>col;
col -= 1;

for(i = 0; i<size; i++)
for(int j =0; j<size; j++)
if(j == col)
sum += *(*(arr+i) + j);

cout<<“\nThe sum of column is “<<sum;
getch();
}

 
Leave a comment

Posted by on October 20, 2015 in Matrices, Pointers and Arrays

 

PROGRAM TO FIND THE SUM OF BORDER ELEMENTS OF A MATRIX (USING POINTERS) )

// THIS PROGRAM FINDS THE SUM OF BORDER ELEMENTS

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

void main()
{
clrscr();
int size;
cout<<“\nEnter the size of the array \n”;
cin>>size;

int **arr = new int*[size];

for(int i =0; i<size; i++)
{
*(arr + i) = new int [size];
}

cout<<“\nInput the matrix \n”;

for(i =0; i<size; i++)
{
for(int j =0; j<size; j++)
{
cin>>*(*(arr+i)+j);
}
}

int sum = 0;
cout<<“\nDisplaying the border elements\n\n”;
for(i =0; i<size; i++)
{
for(int j =0; j<size; j++)
{
if(i == 0 || i == size-1 || j == 0 || j == size -1 )
{
cout<<arr[i][j]<<‘ ‘;
sum += arr[i][j];
}
else
cout<<‘ ‘<<‘ ‘;
}
cout<<‘\n’;
}

cout<<“\nThe sum of border elements is \n”<<sum;
getch();
}

 
Leave a comment

Posted by on October 20, 2015 in Matrices, Pointers and Arrays

 

PROGRAM TO FIND THE TRACE OF A MATRIX

// THIS PROGRAM FINDS TRACE OF A MATRIX

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

void main()
{
clrscr();
int arr[100][100];
int size;
cout<<“\nEnter the size of the matrix (max 100) \n”;
cin>>size;
cout<<“\nInput the matrix \n”;
for(int i =0; i<size; i++)
for(int j =0; j<size; j++)
cin>>arr[i][j];
cout<<“\nFinding the trace of matrix\n”;
int trace = 0;
for(i =0; i<size; i++)
for(j =0; j<size; j++)
if( i ==j)
trace += arr[i][j];
cout<<“\nThe trace of the matrix is “<<trace;
getch();
}

 
Leave a comment

Posted by on October 4, 2015 in Matrices

 

PROGRAM TO PRINT A 2D ARRAY IN COLUMN MAJOR

// THIS PROGRAM PRINTS A 2D ARRAY IN COLUMN MAJOR FORM

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

void main()
{
clrscr();
int arr[100][100];
int row,column;
cout<<“\nEnter the number of rows and columns \n”;
cin>>row>>column;
cout<<“\nInput the array \n”;

for(int i =0; i<row; i++)
{
for(int j =0; j<column; j++)
cin>>arr[i][j];
}
cout<<“\nPrinting the array in column major form\n”;
for(int j =0; j<column; j++)
{
for(i =0; i<row; i++)
cout<<arr[i][j]<<‘ ‘;
}
getch();
}

 
Leave a comment

Posted by on October 4, 2015 in Matrices

 

PROGRAM TO PRINT SPIRAL MATRIX

// PROGRAM TO PRINT THE SPIRAL MATRIX

#include <iostream.h>
#include <conio.h>
#define size 3

void right(int[][size],int);
void left(int[][size],int);
void bottom(int[][size],int);
void top(int[][size],int);

int i=0,j=0;

void main()
{
clrscr();
int arr[size][size],k=1;
cout<<“INPUT THE MATRIX\n”;
for(int x = 0; x<size; x++)
{
for(int y = 0; y<size; y++)
{
cin>>arr[x][y];
}
}
char move = ‘R’;
cout<<“\nPRINTING THE SPIRAL ELEMENTS\n”;
int l = 2*size – 1, val = size;

while(k<=l)
{
switch(move)
{
case ‘R’: right(arr,val);
move = ‘B’;
break;
case ‘L’: left(arr,val);
move = ‘T’;
break;
case ‘T’: top(arr,val);
move = ‘R’;
break;
case ‘B’: bottom(arr,val);
move = ‘L’;
break;
default:  cout<<“ERROR!\n”;
}
k++;
if(k%2 == 0)
val–;
}
getch();
}

void right(int arr[size][size],int val)
{
while(val>0)
{
cout<<arr[i][j]<<‘ ‘;
j++;
val–;
}
j–;
i++;
}

void bottom(int arr[size][size],int val)
{
while(val>0)
{
cout<<arr[i][j]<<‘ ‘;
i++;
val–;
}
i–;
j–;
}

void left(int arr[size][size],int val)
{
while(val>0)
{
cout<<arr[i][j]<<‘ ‘;
j–;
val–;
}
j++;
i–;
}

void top(int arr[size][size],int val)
{
while(val>0)
{
cout<<arr[i][j]<<‘ ‘;
i–;
val–;
}
i++;
j++;
}

 
Leave a comment

Posted by on June 28, 2015 in Matrices

 

PROGRAM TO PRINT THE UPPER AND LOWER TRIANGLE ELEMENTS IN A MATRIX (BOTH)

// THIS PROGRAM PRINTS THE UPPER AND LOWER TRIANGLE IN A MATRIX

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

void main()
{
clrscr();
int arr[100][100], size=0;

cout<<“Enter the size of Matrix\n”;
cin>>size;
cout<<“\nNow input the Matrix\n”;

for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
cin>>arr[i][j];
}
}

cout<<“\nThe upper Triangle elements are\n”;

for(i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
if(i<j)
{
cout<<arr[i][j]<<‘ ‘;
}
else
cout<<‘ ‘<<‘ ‘;
}
cout<<‘\n’;
}

cout<<“\n\nThe Lower Triangle Elements are \n”;

for(i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
if(i>j)
{
cout<<arr[i][j]<<‘ ‘;
}
else
cout<<‘ ‘;
}
cout<<‘\n’;
}

getch();
}

 
Leave a comment

Posted by on June 2, 2015 in Matrices