DSA Lab 02
DSA Lab 02
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
Task 1: Write a Program to enter three integers and output the smallest integer using IF.
Source Code:
#include <iostream>
using namespace std;
int main()
{
int x,y,z;
system("pause");
return 0;
}
Output:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
Task 2: Write a Program to enter 10 integers in a single-dimension array and then print out the array
in ascending order.
Source Code:
#include <iostream>
using namespace std;
int main()
{
int arr[10];
int n=10,i,j;
int temp;
if(n<0 || n>10)
{
cout<<"Input valid range!!!"<<endl;
return -1;
}
for(i=0;i<n;i++)
{
cout<<"Enter element ["<<i+1<<"] ";
cin>>arr[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
return 0;
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
Output:
Task 3: Write a program to create structure named student. Take information of student from user
as input (StdID, StdName, StdAge etc.) Display the output.
Source Code:
#include<iostream>
using namespace std;
struct student{
int id;
string name;
int age;
};
int main()
{
student std;
cout<<"!...Enter Details..!\n";
cout<<"\nEnter an ID : ";
cin>>std.id;
cout<<"\n!...Displaying Information...!\n";
cout<<"\nName : "<<std.name<<endl;
cout<<"Age : "<<std.age<<endl;
cout<<"ID : "<<std.id<<endl;
system ("pause");
}
Output:
Source Code:
#include<iostream>
#include <string>
cout<<"!...Enter Details..!\n";
cout<<"\nEnter an ID : ";
cin>>std.id;
ptr_id=&std.id;
ptr_age=&std.age;
cout<<"\n!...Displaying Information...!\n";
cout<<"\nName : "<<*ptr_name<<endl;
cout<<"Age : "<<*ptr_age<<endl;
cout<<"ID : "<<*ptr_id<<endl;
system ("pause");
}
Output:
Task 5(a): Create an array of length 10 of integers. Values ranging from 1 to 50.
Find all pair of elements whose sum is 25.
Source Code:
#include <iostream>
using namespace std;
int main()
{
int arr[10];
int AVG=0,z=0;
cout<<"Enter The Values of Array (10 elements are Allow)\n";
for (int x=0;x<10;x++)
{
cout<<"Enter Element "<<x+1<<" : ";
cin>>arr[x];
if(arr[x]<0 || arr[x]>50)
{
cout<<"Only Enter The Value Between 1 TO 50";
cin>>arr[x];
}
}
cout<<"A[10]={ ";
for(int i2=0;i2<10;i2++)
{
cout<<arr[i2]<<",";
}
cout<<"\b }\n\n";
for (int k2=0;k2<10;k2++)
{
for (int j=0;j<10;j++)
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
{
int temp;
temp=arr[k2]+arr[j];
if (temp==25)
{
cout<<arr[k2]<<" And "<<arr[j]<<" = 25 ";
cout<<endl;
}
}
}
}
Output:
Task 5(b): Find the number of elements of A which are even, and the number of elements of A which
are odd.
Source Code:
#include <iostream>
using namespace std;
int main()
{
int x;
int arr[10];
for(int i=0;i<10;i++)
{
cout<<"Enter Element "<<i+1<<" : ";
cin>>arr[i];
if(arr[i]>50 || arr[i]<=0)
{
cout<<"\n\n!Error in Value"<<endl;
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
x=1;
break;
}
}
if (x==1)
{
cout<<"Your Put The Wrong Value"<<endl;
}
else
{
cout<<"\nEven Number Are Following "<<endl;
for (int j=0;j<10;j++)
{
if (arr[j]%2==0)
{
cout<<arr[j]<<" , ";
}
}
cout<<endl;
cout<<endl;
}
return 0;
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
Output:
Task 5(c): Write a procedure, which finds the average of the value of A.
Source Code:
#include <iostream>
using namespace std;
int main()
{
int temp;
int const n=10;
int arr[n];
temp=0;
cout<<"Enter The Values of Array (10 elements are Allow)"<<endl;
for (int x=0;x<n;x++)
{
cout<<"Enter Element "<<x+1<<" : ";
cin>>arr[x];
if(arr[x]<0 || arr[x]>50)
{
cout<<"Only Enter The Value Between 1 TO 50"<<endl;
cin>>arr[x];
}
}
for(int i=0;i<n;i++)
{
temp +=arr[i];
}
double y=temp/(n+0.00);
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
cout<<"\n\nSum Of Array Is :"<<temp<<endl;
cout<<"Avarage Of Array Is :"<<y<<endl;
return 0;
}
Output:
Task 5(d) Write a procedure, which adds an element in an array at a given index. Take the value to
add and the index from the user by using Shift down technique.
Source Code:
#include <iostream>
using namespace std;
n++;
arr[pos - 1] =x;
return arr;
}
int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;
{
cout<<"Enter The Values of Array (10 elements are Allow)"<<endl;
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
for (int x=0;x<10;x++)
{
cout<<"Enter Element "<<x+1<<" : ";
cin>>arr[x];
}
cout<<"\n\nA[10]={ ";
for (i = 0; i < n; i++)
cout << arr[i] << ",";
cout<<"\b }\n\n";
return 0;
}
}
Output:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
Task 5(e) Write a procedure which looks for 2 numbers 45 and 14 in an array and delete them if they
are present in the array by using Shift up technique.
Source Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int arr[10];
}
for(int i=0;i<10;i++)
if (arr[i]==(45) || arr[i]==14)
{
arr[i]='\0';
}
}
cout<<"\n\nArray Elements after deleting :";
cout<<"A[10]={ ";
for(int k=0;k<10;k++)
{
if(arr[k]!=0)
{
cout<<arr[k]<<",";
}
}
cout<<"\b }\n\n";
return 0;
}
Output:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
Task 6(a): Write a program which input 2 matrix of user defined rows and columns and perform
following operation .
Display/Print as a Matrix
Source Code:
#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << endl << "Enter elements of 1st matrix: " << endl;
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
return 0;
}
Output:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
b. Addition of Matrix
Source Code:
#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << endl << "Enter elements of 1st matrix: " << endl;
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}
return 0;
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
Output:
c. Subtraction of Matrix
Source Code:
#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], subtraction[100][100], i, j;
cout << endl << "Enter elements of 1st matrix: " << endl;
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
cout << endl << "Subtraction of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << subtraction[i][j] << " ";
if(j == c - 1)
cout << endl;
}
return 0;
}
Output:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
d. matrix multiplication
Source Code:
#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], Multiplication[100][100], i, j;
cout << endl << "Enter elements of 1st matrix: " << endl;
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
cout << endl << "Multiplication of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << Multiplication[i][j] << " ";
if(j == c - 1)
cout << endl;
}
return 0;
}
Output:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
e. Determinant
Source Code:
#include<iostream>
#include<math.h>
f. Inverse
Source Code:
#include<iostream>
using namespace std;
int main()
{
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
int i,j,k,n;
float a[100][200],t;
cout<<"Enter order of matrix-";
cin>>n;
cout<<"Enter elements of matrix"<<endl;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
for(i=0;i<n;i++)
{
for(j=n;j<2*n;j++)
{
if(i==j-n)
a[i][j]=1;
else
a[i][j]=0;
}
}
for(i=0;i<n;i++)
{
t=a[i][i];
for(j=i;j<2*n;j++)
a[i][j]=a[i][j]/t;
for(j=0;j<n;j++)
{
if(i!=j)
{
t=a[j][i];
for(k=0;k<2*n;k++)
a[j][k]=a[j][k]-t*a[i][k];
}
}
}
cout<<"\n\nInverse matrix\n\n";
for(i=0;i<n;i++)
{
for(j=n;j<2*n;j++)
cout<<"\t"<<a[i][j];
cout<<"\n";
}
return 0;
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
Output: