0% found this document useful (0 votes)
101 views22 pages

DSA Lab 02

The document contains the source code and output for 6 tasks completed as part of a lab assignment on getting started with C++ revisions. The tasks include programs to find the smallest of 3 integers, sort an array of 10 integers, create and display a structure for student information using structures and pointers, find pairs of elements in an array that sum to 25, analyze even/odd elements in an array, calculate the average of array elements, insert a new element into an array, and delete specific elements from an array. The final task involves matrix operations on 2 user-defined matrices.

Uploaded by

Atif Jalal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views22 pages

DSA Lab 02

The document contains the source code and output for 6 tasks completed as part of a lab assignment on getting started with C++ revisions. The tasks include programs to find the smallest of 3 integers, sort an array of 10 integers, create and display a structure for student information using structures and pointers, find pairs of elements in an array that sum to 25, analyze even/odd elements in an array, calculate the average of array elements, insert a new element into an array, and delete specific elements from an array. The final task involves matrix operations on 2 user-defined matrices.

Uploaded by

Atif Jalal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Atif Jalal

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;

cout << "Enter the first number: ";


cin>>x;
cout << "Enter the second number: ";
cin>>y;
cout << "Enter the third number: ";
cin>>z;

if(x<=y && x <=z){


cout<<"\n Smallest number is: "<<x<<endl;
}

if(y<=x && y <=z){


cout<<"\n Smallest number is: "<<y<<endl;
}

if(z<=x && z <=y){


cout<<"\nSmallest number is: "<<z<<endl;
}

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];
}

cout<<"\n\nUnsorted Array elements:"<<endl;


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

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;
}
}
}

cout<<"\nSorted Array elements:"<<endl;


for(i=0;i<n;i++)
cout<<arr[i]<<" ";
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 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<<"Enter your Name : ";


cin>>std.name;

cout<<"Enter your Age : ";


cin>>std.age;
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020

cout<<"\n!...Displaying Information...!\n";
cout<<"\nName : "<<std.name<<endl;
cout<<"Age : "<<std.age<<endl;
cout<<"ID : "<<std.id<<endl;
system ("pause");
}
Output:

Task 4: Perform above task using pointers to structure

Source Code:
#include<iostream>
#include <string>

using namespace std;


struct student{
int id;
string name;
int age;
};
int main()
{
int *ptr_id;
string *ptr_name;
int *ptr_age;
student std;

cout<<"!...Enter Details..!\n";
cout<<"\nEnter an ID : ";
cin>>std.id;
ptr_id=&std.id;

cout<<"Enter your Name : ";


cin>>std.name;
ptr_name=&std.name;

cout<<"Enter your Age : ";


cin>>std.age;
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020

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<<"\nODD Number Are Following "<<endl;


for (int k=0;k<10;k++)
{
if (arr[k]%2!=0)
{
cout<<arr[k]<<" , ";
}

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;

int* insertX(int n, int arr[], int x, int pos)


{
int i;

n++;

for (i = n; i >= pos; i--)


arr[i] = arr[i - 1];

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";

cout<<"Enter value you want to add : ";


cin>>x;

cout<<"\n\nAt which position you want to add (0-9) : ";


cin>>pos;

insertX(n, arr, x, pos);

cout<<"\nArray after inserting "<<x<< " at position "<<pos<<"\n";


cout<<"\n\nA[10]={ ";
for (i = 0; i < n + 1; i++)
cout << arr[i] << ",";
cout<<"\b }\n\n";
cout << endl;

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];

cout<<"Enter The Values of Array (10 elements are Allow)"<<endl;


for (int x=0;x<10;x++)
{
cout<<"Enter Element "<<x+1<<" : ";
cin>>arr[x];
if(arr[x]<0 || arr[x]>50)
{
cout<<"\nOnly Enter The Value Between 1 TO 50"<<endl;
break;
}

}
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 << "Enter number of rows : ";


cin >> r;

cout << "Enter number of columns : ";


cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

for(i = 0; i < r; ++i)


for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

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];
}

cout << endl << "Matrix A : " << endl;


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << a[i][j] << " ";
if(j == c - 1)
cout << endl;
}

cout << endl << "Matrix B : " << endl;


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << b[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

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 << "Enter number of rows : ";


cin >> r;

cout << "Enter number of columns : ";


cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

for(i = 0; i < r; ++i)


for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

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];
}

for(i = 0; i < r; ++i)


for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + 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 << "Enter number of rows : ";


cin >> r;

cout << "Enter number of columns : ";


cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

for(i = 0; i < r; ++i)


for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
cin >> a[i][j];
}

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];
}

for(i = 0; i < r; ++i)


for(j = 0; j < c; ++j)
subtraction[i][j] = a[i][j] - 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 << "Enter number of rows : ";


cin >> r;

cout << "Enter number of columns : ";


cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

for(i = 0; i < r; ++i)


for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
}

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];
}

for(i = 0; i < r; ++i)


for(j = 0; j < c; ++j)
Multiplication[i][j] = a[i][j] * 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>

using namespace std;


int determinant( int matrix[10][10], int n) {
int det = 0;
int submatrix[10][10];
if (n == 2)
return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
else {
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x)
continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 02: Getting Started (Revision of C++) Date: 10 July, 2020
subi++;
}
det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n - 1 ));
}
}
return det;
}
int main() {
int n, i, j;
int matrix[10][10];
cout << "Enter the size of the matrix : ";
cin >> n;
cout << "\nEnter the elements of the matrix... \n";
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> matrix[i][j];
}
cout<<"\n!...The entered matrix...! "<<endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
cout << matrix[i][j] <<" ";
cout<<endl;
}
cout<<"\nDeterminant of the matrix is : "<< determinant(matrix, n);
return 0;
}
Output:

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:

You might also like