0% found this document useful (0 votes)
39 views

LT02

The document contains code snippets and output screenshots from 5 lab tasks completed by the student for their DSA lab course. The tasks include: 1) Finding repeating elements in an array, 2) Taking integer inputs and counting positive, negative, even, odd and zero numbers, 3) Finding the second largest element in an array, 4) Adding two 2x2 matrices, and 5) Multiplying two 2x2 matrices. The code and outputs are documented for each task.

Uploaded by

Jiya Ali
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)
39 views

LT02

The document contains code snippets and output screenshots from 5 lab tasks completed by the student for their DSA lab course. The tasks include: 1) Finding repeating elements in an array, 2) Taking integer inputs and counting positive, negative, even, odd and zero numbers, 3) Finding the second largest element in an array, 4) Adding two 2x2 matrices, and 5) Multiplying two 2x2 matrices. The code and outputs are documented for each task.

Uploaded by

Jiya Ali
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/ 11

S haheed Zulfikar Ali Bhutto Institute of Science &

Technology
COMPUTER SCIENCE DEPARTMENT

DSA (Lab)
Lab Task #02

Submitted To: Adil Majeed

Student Name: Syed Iqbal Hussain

Section: BS-SE (3B)

Reg. Number: 1980143

DSA-Lab BSSE-3B SZABIST-ISB


S haheed Zulfikar Ali Bhutto Institute of Science &
Technology

DSA-Lab BSSE-3B SZABIST-ISB


S haheed Zulfikar Ali Bhutto Institute of Science &
Technology
COMPUTER SCIENCE DEPARTMENT

 Lab Task # 1
Write a C++ program to find the two repeating elements in a given array of integers.

Code:
#include <iostream>

using namespace std;

int main()

int arr[5];

cout<<"Enter 05 integers to Find Repeating Numbers: "<<endl;

for(int i=0 ; i<5 ; i++)

cin>>arr[i];

cout<<"Repeating Elements: ";

for(int i = 0; i <5 ; i++)

for (int j = i+1;j<5 ; j++)

if(arr[i]==arr[j])

cout<<" "<<arr[i];

DSA-Lab BSSE-3B SZABIST-ISB


S haheed Zulfikar Ali Bhutto Institute of Science &
}
Technology
}

return 0;

Output:

(Screenshot of your output screen)

 Lab Task # 2
Take 20 integer inputs from user and print the following:

Number of positive numbers

Number of negative numbers

Number of odd numbers

Number of even numbers

Number of 0.

Code:
#include <iostream>
DSA-Lab BSSE-3B SZABIST-ISB
S haheed Zulfikar Ali Bhutto Institute of Science &
usingTechnology
namespace std;
int main()
{
int integer[10];
cout<<"Enter 10 Integers "<<endl;

for (int i = 0; i < 10; i++)


{
cin>>integer[i];
if(integer[i] == 0)
{
cout<<"The Number is "<<integer[i]<<endl;
}

else if(integer[i] > 0)


{
cout<<"The Number is Positive "<<integer[i]<<endl;
}
else
{
cout<<"The Number is Negative "<<integer[i]<<endl;
}

if (integer[i] % 2 == 0)
{
cout<<"The Number is Even "<<integer[i]<<endl;
}
else
{
cout<<"The Number is odd "<<integer[i]<<endl;
}
}
return 0;
}

Output:

DSA-Lab BSSE-3B SZABIST-ISB


S haheed Zulfikar Ali Bhutto Institute of Science &
Technology

(Screenshot of your output screen)

 Lab Task # 3
Write a C++ program to find second largest element in a given array of integers.

Code:
#include <iostream>
using namespace std;
int main()
{
int arr[5];
int largest,smallest;

cout<<"Enter 5 Numbers to find the Second Largest Number "<<endl;


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

if(arr[0]<arr[1])
{
largest = arr[1];
smallest = arr[0];

}
else
DSA-Lab BSSE-3B SZABIST-ISB
S haheed Zulfikar Ali Bhutto Institute of Science &
{
Technology
largest = arr[0];
smallest = arr[1];

for(int i = 2 ; i <5 ; i++)


{
if(arr[i]>largest)
{
smallest = largest;
largest = arr[i];
}
else if(arr[i]>smallest )
{

smallest = arr[i];
}

cout<<"The Second Largest Number is "<<smallest;


}

Output:

(Screenshot of your output screen)

DSA-Lab BSSE-3B SZABIST-ISB


S haheed Zulfikar Ali Bhutto Institute of Science &
Technology
 Lab Task # 4
Write a program Add Two Matrices using Multi-dimensional Arrays.

Code:
#include <iostream>
using namespace std;
int main()
{
int mat1[2][2];
int mat2[2][2];
int sum[2][2];

cout<<"Enter 1st Matix of 2 by 2"<<endl;

for(int i = 0 ; i < 2 ; i++)


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

cout<<"Enter 2nd Matix of 2 by 2"<<endl;

for(int i = 0 ; i < 2 ; i++)


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

cout<<"Sum of the Matix"<<endl;

for(int i = 0 ; i < 2 ; i++)


{
for(int j = 0 ; j < 2 ; j++)
{
sum[i][j] = mat1[i][j]+mat2[i][j];
cout<<" "<<sum[i][j];
}
cout<<"\n";
}

DSA-Lab BSSE-3B SZABIST-ISB


S haheed Zulfikar Ali Bhutto Institute of Science &
} Technology
Output:

(Screenshot of your output screen)

 Lab Task # 5
Write a program Multiply Two Matrices using Multi-dimensional Arrays.

Code:
#include <iostream>
using namespace std;
int main()
{
int mat1[2][2];
int mat2[2][2];
int c[2][2];
cout<<"Enter 1st Matix of 2x2 "<<endl;

for(int i = 0 ; i < 2 ; i++)


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

}
DSA-Lab BSSE-3B SZABIST-ISB
S haheed Zulfikar Ali Bhutto Institute of Science &
Technology
cout<<"Enter 2nd Matix of 2x2 "<<endl;

for(int i = 0 ; i < 2 ; i++)


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

int sum = 0;
for(int i = 0 ; i < 2 ; i++)
{
for(int j = 0 ; j < 2 ; j++)
{
for(int k = 0 ; k < 2 ; k++)
{

sum = sum + mat1[i][k]*mat2[k][j];

}
c[i][j] = sum;
sum = 0;
}
}
cout<<"Multiplication of matrix is: \n";
for(int i = 0 ; i < 2 ; i++)
{
for(int j = 0 ; j < 2 ; j++)
{
cout<<" "<<c[i][j];
}
cout<<"\n";
}
}

Output:

DSA-Lab BSSE-3B SZABIST-ISB


S haheed Zulfikar Ali Bhutto Institute of Science &
Technology

(Screenshot of your output screen)

DSA-Lab BSSE-3B SZABIST-ISB

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy