RSS

Category Archives: Matrices

PROGRAM TO PRINT UPPER TRIANGLE ELEMENTS OF A MATRIX(USING POINTERS)

// THIS PROGRAM PRINTS UPPER TRIANGLE ELEMENTS OF 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<<“\nTHE UPPER TRIANGLE ELEMENTS OF THE MATRIX ARE \n”;

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

getch();
}

Advertisements
 
Leave a comment

Posted by on November 21, 2015 in Matrices, Pointers and Arrays

 

PROGRAM TO SUBTRACT TWO MATRICES(USING POINTERS)

// THIS PROGRAM SUBTRACTS TWO MATRICES (USING POINTERS)

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

void main()
{
clrscr();
int size;
cout<<“\nENTER THE SIZE OF THE MATRIX \n”;
cin>>size;

int **arr1 = new int *[size];
int **arr2 = new int *[size];
int **res = new int *[size];

for(int i =0; i<size; i++)
{
arr1[i] = new int [size];
arr2[i] = new int [size];
res [i] = new int [size];
}

cout<<“\nINPUT FIRST MATRIX \n”;

for(i =0; i<size; i++)
for(int j =0; j<size;j++)
cin>>*(*(arr1 + i) + j);

cout<<“\nINPUT SECOND MATRIX \n”;

for(i =0; i<size; i++)
for(j =0; j<size; j++)
cin>>*(*(arr2 + i) + j);

cout<<“THE SUBTRACTED MATRIX IS \n”;

for(i =0; i<size; i++)
{
for(j =0; j<size; j++)
{
*(*(res + i) + j) = *(*(arr1 + i) + j) – *(*(arr2 + i) + j);
cout<<*(*(res + i) + j)<<‘ ‘;
}
cout<<‘\n’;
}

getch();
}

 
Leave a comment

Posted by on November 21, 2015 in Matrices, Pointers and Arrays

 

PROGRAM TO PRINT THE SUM OF ALL ROWS AND COLUMNS OF A MATRIX

// THIS PROGRAM PRINTS THE ROW SUM AND COLUMN SUM OF A MATRIX

#include <iostream>

using namespace std;

int main()
{
int row, col;
int arr[100][100];

cout<<“\nEnter the number of rows in the matrix \n”;
cin>>row;
cout<<“\nEnter the number of columns in the matrix \n”;
cin>>col;
cout<<“\nInput the matrix \n”;

for(int i =0; i<row; i++)
for(int j =0; j<col; j++)
cin>>arr[i][j];

int rowsum = 0, colsum = 0;

for(int i =0; i<row; i++)
{
for(int j = 0; j<col; j++)
{
rowsum += arr[i][j];
}
cout<<“\nSum of row “<<i+1<<” is “<<rowsum;
rowsum = 0;
cout<<‘\n’;
}

colsum = 0;
cout<<“\n\n”;

for(int j = 0; j<col; j++)
{
for(int i =0; i<row; i++)
{
colsum += arr[i][j];
}
cout<<“\nSum of column “<<j+1<<” is “<<colsum;
colsum = 0;
cout<<‘\n’;
}
return 0;
}

 
Leave a comment

Posted by on November 21, 2015 in Matrices

 

PROGRAM TO PRINT THE LOWER TRIANGLE ELEMENTS (USING POINTERS)

// THIS PROGRAM PRINTS LOWER TRIANGLE ELEMENTS OF 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<<“\nTHE LOWER TRIANGLE ELEMENTS OF THE MATRIX ARE \n”;

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

getch();
}

 
Leave a comment

Posted by on November 21, 2015 in Matrices, Pointers and Arrays

 

PROGRAM TO FIND THE SUM OF LOWER DIAGONAL ELEMENTS (USING POINTERS)

// THIS PROGRAM FINDS THE SUM OF LOWER DIAGONAL ELEMENTS 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);

int sum = 0;

for(i =0; i<size; i++)
for(j = 0; j<size; j++)
if( i +j == size – 1)
sum += *(*(arr +i) + j);

cout<<“\nTHE SUM OF THE LOWER DIAGONAL ELEMENTS IS “<<sum;
getch();
}

 
Leave a comment

Posted by on November 21, 2015 in Matrices, Pointers and Arrays

 

PROGRAM TO FIND THE SUM OF MAIN DIAGONAL ELEMENTS (USING POINTERS)

//THIS PROGRAM FINDS THE SUM OF MAIN DIAGONAL ELEMENTS 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<<“\nFINDING THE SUM OF DIAGONAL ELEMENTS IN THE MATRIX \n”;

int sum = 0;

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

cout<<“\nTHE SUM OF DIAGONAL ELEMENTS IS \n”<<sum;
getch();
}

 
Leave a comment

Posted by on November 21, 2015 in Matrices, Pointers and Arrays

 

PROGRAM TO ADD TWO MATRICES (USING POINTERS)

// THIS PROGRAM ADDS TWO MATRICES (USING POINTERS)

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

void main()
{
clrscr();
int size;
cout<<“\nENTER THE SIZE OF THE MATRICES \n”;
cin>>size;
int **arr1= new int *[size], **arr2 = new int *[size], **res = new int *[size];

for(int i = 0; i<size; i++)
{
arr1[i] = new int [size];
arr2[i] = new int [size];
res[i] = new int [size];
}

cout<<“\nINPUT FIRST MATRIX \n”;

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

cout<<“\nINPUT SECOND MATRIX \n”;

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

cout<<“\nTHE ADDED MATRIX IS \n”;

for(i = 0; i<size; i++)
{
for(int j =0; j <size; j++)
{
*(*(res+i) + j) = *(*(arr1 +i) + j) + *(*(arr2 + i)+j);
cout<<*(*(res + i) + j)<<‘ ‘;
}
cout<<‘\n’;
}

getch();
}

 
Leave a comment

Posted by on November 21, 2015 in Matrices, Pointers and Arrays