Indian Institute of Technology Delhi: Submitted by
Indian Institute of Technology Delhi: Submitted by
Indian Institute of Technology Delhi: Submitted by
MAL-701
Data Structure Assignment-1
Part [2]
Submitted By
Devendra Singh 2012EEN2332
Rajeev Kumar Pandey 2012EEN2343
Siddharth Sabharwal 2012JVL2899
Problem Statement 6:- Compute the value of the Following
equation Y(n)= 1
3
*(1
3
+2
3
)*( 1
3
+2
3
+3
3
)*( 1
3
+2
3
+3
3
+------------n
3
) For
n=1 to 20.
C++ prrgram
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{int i,sum1,sum2,sum3,output,n; // variables
sum1 = 0; // initialize sum
sum2=0;// initialize sum
sum3=0;// initialize sum
cout<<"/n enter the value of n : ";
cin>>n;
/* recursive addition of cubes */
{for (i = 1; i <= 2; i++)
sum1 = sum1 + (i*i*i);
cout<<" \nSum1 is the sum of the first 2 cubes." <<setw(8)<< sum1 << endl<<endl;
/* recursive addition of cubes */
{for (i = 1; i <= 3; i++)
sum2 = sum2 + (i*i*i);
cout <<" Sum2 is the sum of the first 3 cubes."<<setw(8)<< sum2<< endl<<endl;
/* recursive addition of cubes */
{
for (i = 1; i <= n; i++)
sum3 = sum3 + (i*i*i);
cout<<" Sum3 is the sum of the first 20 cubes." <<setw(8)<< sum3 << endl<<endl;
/* The output 1^3*(1^3+2^3)*(1^3+2^3+3^3)*(1^3+2^3+3^3+-----------+n^3) */
output=1*sum1*sum2*sum3;
cout<<"\nOutput Of The Series (1*Sum1*Sum2*Sum3) Given As Y(n):= "<<setw(6)<<output<<endl<<endl;
return(0);
}}}}
Output
Problem Statement 7:- Write a program to find the trace of the matrix. Trace
of the matrix is the product of the diagonal element.
C++ program
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{int a[10][10],i,j,m,n,Trace=1;
cout<<"enter the number of row= ";
cin>>m;
cout<<"enter the number of column= ";
cin>>n;
cout<<"\n enter the elements of matrix a= \n ";
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{cout << "\nElement["<<i<<"]["<<j<<"]: ";
cin>>a[i][j];}}
cout << "\n Original Matrix is:"<<endl;
for(i=0;i<m;i++){
cout << "\n";
for(j=0;j<n;j++){
cout << "\t"<<a[i][j]<< " ";
}}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(i==j)
Trace=Trace*a[i][j];
cout<<"\n \n Trace of a matrix= "<<Trace<<endl<<endl;
return(0);
}
Output
Problem statement 8:- Write a program to find the Rank of the
matrix using Gauss elimination method.
C++ program
#include<iostream>
using namespace std;
double redefine_izero(int izero[],double matrix[][10],int n[]);
double arrange(double matrix[][10],int n[],int izero[]);
double read(double matrix[][10],int[]);
double display(double matrix[][10],int n[]);
double scalup(double matrix[][10],int n[],int izeros[]);
//Main function
int main()
{int i,j,group,p,r,izero[10],n[2],t,q,rank,re_test=1;
double matrix[10][10];
read(matrix,n);
cout<<"\nThe matrix entered is\n\n"<<endl;
display(matrix,n);
redefine_izero(izero,matrix,n);
arrange(matrix,n,izero);
if(matrix[0][0]==0)
{cout<<"\n\tError: Invalid Marix \n\n"<<endl;}
redefine_izero(izero,matrix,n);
scalup(matrix,n,izero);
while(re_test==1)
{ group=0;
for(i=0;i<n[0];++i)
{ p=0;
while(izero[i+p]==izero[i+p+1]&&(i+p+1)<n[0])
{ group=group+1;
p=p+1;}
if(group!=0)
{while(group!=0)
{for(j=0;j<n[1];++j)
{ matrix[i+group][j]=matrix[i+group][j]-matrix[i][j]; }
group=group-1;}
break;}}
redefine_izero(izero,matrix,n);
arrange(matrix,n,izero);
redefine_izero(izero,matrix,n);
scalup(matrix,n,izero);
re_test=0;
for(r=0;r<n[0];++r)
{ if(izero[r]==izero[r+1]&&r+1<n[0])
{ if(izero[r]!=n[1])
re_test=1;}}}
cout<<"\n\n Gauss eliminated matrix is shown below\n\n"<<endl;
display(matrix,n);
rank =0;
for (i=0;i<n[0];++i)
{if (izero[i]!=n[1])
{++rank; }}
cout<<"\n Rank Of The Matrix = " <<rank<<endl<<endl; }
// Func{} to read a matrix
double read (double matrix[][10],int n[])
{int i,j;
cout<<"Enter the number of rows in the matrix : ";
cin>>n[0];
cout<<"\n";
cout<<"Enter the number of columns in the matrix : ";
cin>>n[1];
cout<<"\n";
cout<<"Enter The Matrix Elements Row wise \n";
for(i=0;i<n[0];++i)
{
for(j=0;j<n[1];++j)
{
cout << "\nElement["<<i<<"]["<<j<<"]: ";
cin>>matrix[i][j];}}}
// Func{} to display a matrix
double display (double matrix[][10],int n[])
{int i,j;
for(i=0;i<n[0];++i)
{for(j=0;j<n[1];++j)
{cout<<"\t"<<matrix[i][j];}
cout<<("\n");}
cout<<("\n");}
//Func{}to scale a matrix
double scalup(double matrix[][10],int n[],int izero[])
{
int i,j;
double divisor;
for(i=0;i<n[0];++i)
{divisor=matrix[i][izero[i]];
for(j=izero[i];j<n[1];++j)
{matrix[i][j]=matrix[i][j]/divisor;}}}
// Func{} to Update initial zeros array
double redefine_izero ( int izero[],double matrix[][10],int n[])
{int zcount,i,j;
for(i=0;i<n[0];++i)
{zcount=0;
for(j=0; (matrix[i][j]==0) && (j<n[1]) ;++j)
{++zcount;}
izero[i]=zcount;}}
//Fun{} definition to arrange matrix
double arrange(double matrix[][10],int n[],int izero[])
{int l,reqrow,i,k,lastrow,tempvar,large;
double rowtemp[10];
lastrow=n[0]-1;
for(l=0;l<n[0];++l)
{ large=izero[0];
for(i=0;i<n[0];++i)
{ if(large<=izero[i])
{large=izero[i];
reqrow=i;}}
izero[reqrow]=-1;
tempvar=izero[reqrow];
izero[reqrow]=izero[lastrow];
izero[lastrow]=tempvar;
for(k=0;k<n[1];++k)
{ rowtemp[k]=matrix[lastrow][k];}
for(k=0;k<n[1];++k)
{ matrix[lastrow][k]=matrix[reqrow][k];}
for(k=0;k<n[1];++k)
{matrix[reqrow][k]=rowtemp[k];}
lastrow=lastrow-1;}}
Output
Program statement 9:- To find group of 1s in the 0s and 1s matrix.
C++ Program
#include<iostream>
using namespace std;
int main()
{int i, j, a,b,c,d,row, col;
int count=0;
cout<<"Enter the number of rows in matrix : ";
cin>>row;
cout<<"\n";
cout<<"Enter the number of columns in matrix: ";
cin>>col;
cout<<"\n";
a=row+1; b=row+2; c=col+1; d=col+2;
int matrix[row][col], newm[b][d];
cout<<"Enter the elements of matrix in 1 and 0\n";
for(i=0;i<row;i++)
{for(j=0;j<col;j++)
{cout << "Element["<<i<<"]["<<j<<"]: ";
cin>>matrix[i][j]; }}
cout<<"\n";
cout<<"Entered matrix is:\n";
for(i=0;i<row;i++)
{for(j=0;j<col;j++)
{cout<<matrix[i][j]<<"\t";}
cout<<"\n";
}
for(i=0;i<b;i++)
{for(j=0;j<d;j++)
newm[i][j]=0;}
for(i=1;i<a;i++)
{for(j=1;j<c;j++)
newm[i][j]=matrix[i-1][j-1];}
for(i=1;i<a;i++)
{for(j=1;j<c;j++)
{if(newm[i][j-1] == 1 || newm[i][j+1] == 1 || newm[i-1][j] == 1 || newm[i+1][j] == 1 && newm[i][j]==1)
{newm[i][j]=0;}
if(newm[i][j-1] == 0 && newm[i][j+1] == 0 && newm[i-1][j] == 0 && newm[i+1][j] == 0 && newm[i][j]==1)
{newm[i][j]=0;
count=count+1;}}}
cout<<"\n";
cout<<"The number of groups are "<<count<<"\n"<<endl<<endl;
return(0);
}
Output
Problem Statement 10:-Vector to matrix conversion.
C++ Program
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{ int i, j,k,row,col,vector[15],matrix[10][10];
int sum=0;
//show the vector
cout<<"enter the vector element"<<endl;
for(j=0;j<15;j++)
{ cin>>vector[j]; }
cout<<"vector A \n";
for(j=0;j<15;j++)
cout <<setw(4)<< vector[j];
cout<<"\n "<<endl;
cout<<"enter the no of rows"<<endl<<endl;
cin>>row;
cout<<\n;
col=15/row;
for(i=0;i<row;i++)
{for(j=0;j<col;j++)
{ matrix[i][j]=vector[sum];
sum++;}}
cout<<"\n \n The matrix is\n";
for(i=0;i<row;i++)
{for(j=0;j<col;j++)
{cout<<matrix[i][j]<<"\t"; }
cout<<"\n"<<endl; }
return(0);
}
Output