0% found this document useful (0 votes)
65 views18 pages

DSA - Lab: Submitted To: Mam Rabia Arshad SUBMITTED BY: Muhammad Bilal

The document contains 4 tasks and 4 questions related to arrays in C++. Task 1 and 2 involve taking input from the user to populate an array and print the array. Task 3 performs binary search on a sorted array. Task 4 repeats Task 1 in a while loop 10 times. The 4 questions involve: 1) separating even and odd elements of an array into new arrays, 2) printing duplicate elements of an array, 3) finding common elements between two arrays, 4) finding maximum and minimum element of an array.

Uploaded by

Bilal Alone
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)
65 views18 pages

DSA - Lab: Submitted To: Mam Rabia Arshad SUBMITTED BY: Muhammad Bilal

The document contains 4 tasks and 4 questions related to arrays in C++. Task 1 and 2 involve taking input from the user to populate an array and print the array. Task 3 performs binary search on a sorted array. Task 4 repeats Task 1 in a while loop 10 times. The 4 questions involve: 1) separating even and odd elements of an array into new arrays, 2) printing duplicate elements of an array, 3) finding common elements between two arrays, 4) finding maximum and minimum element of an array.

Uploaded by

Bilal Alone
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/ 18

DSA_lab

Tasks lab2

SUBMITTED TO: Mam Rabia Arshad

SUBMITTED BY: Muhammad Bilal

Software Engineering Dept.


19-SE-18
[email protected]
Task No.1
#include<iostream>

using namespace std;

int main() {

cout<<"Enter The Size Of Array: ";

int size;

cin>>size;

int array[size], key,i;

// Taking Input In Array

for(int j=0;j<size;j++){

cout<<"Enter "<<j<<" Element: ";

cin>>array[j];

//Your Entered Array Is

cout<<"Array elements are :"<<endl;

for(int a=0;a<size;a++){

cout<<"array[ "<<a<<" ] = ";

cout<<array[a]<<endl;

}
Task No.2
#include<iostream>

using namespace std;

int main() {

cout<<"Enter The Size Of Array: ";

int size;

cin>>size;
int array[size], key,i;

// Taking Input In Array

for(int j=0;j<size;j++){

cout<<"Enter "<<j<<" Element: ";

cin>>array[j];

//Your Entered Array Is

cout<<"Array elements are : "<<endl;

for(int a=0;a<size;a++){

cout<<"array[ "<<a<<" ] = ";

cout<<array[a]<<endl;

cout<<"Enter Key To Search in Array : ";

cin>>key;

for(i=0;i<size;i++){

if(key==array[i]){

cout<<"Key Found At Index Number : "<<i<<endl;

break;

}
}

if(i != size){

cout<<"KEY FOUND at index : "<<i;

else{

cout<<"KEY NOT FOUND in Array ";

return 0;

}
Task No.3
#include<iostream>

using namespace std;

int main()

int i, arr[10], num, first, last, middle;

cout<<"Enter 10 Elements (in sorted form): ";

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

cin>>arr[i];

cout<<"\nEnter Element to be Search: ";

cin>>num;

first = 0;

last = 9;

middle = (first+last)/2;

while(first <= last)

if(arr[middle]<num)

first = middle+1;

else if(arr[middle]==num)

cout<<"\nThe number, "<<num<<" found at Position "<<middle+1;

break;

}
else

last = middle-1;

middle = (first+last)/2;

if(first>last)

cout<<"\nThe number, "<<num<<" is not found in given Array";

cout<<endl;

return 0;

}
Task No.4
#include<iostream>
using namespace std;

int main() {
int a=0;
while(a<10){
cout<<"Enter The Size Of Array: ";
int size;
cin>>size;
int array[size], key,i;
// Taking Input In Array
for(int j=0;j<size;j++){
cout<<"Enter "<<j<<" Element: ";
cin>>array[j];
}
//Your Entered Array Is
cout<<"Array elements are :"<<endl;
for(int a=0;a<size;a++){
cout<<"array[ "<<a<<" ] = ";
cout<<array[a]<<endl;
}
}
}
Exercise / Assignment
Question No.1

#include<iostream>

#include<conio.h>
using namespace std;
int main()
{
int arr[20],even[20],odd[20],i,s,p,j=0,k=0,no;
//clrscr();
cout<<"Enter Size of Array: ";
cin>>no;
cout<<"Enter any "<<no<<" elements in Array: ";
for(i=0; i<no;i++)
{
cin>>arr[i];
}
for(i=0; i<no;i++)
{
if(arr[i]%2==0)
{
even[j]=arr[i];
j++;
}
else
{
odd[k]=arr[i];
k++;
}
}
cout<<"\na) Even Elements in array are: ";
for(i=0; i<j ;i++)
{
cout<<even[i]<<" ";
}
cout<<"\nb) Odd Elements in array are: ";
for(i=0; i<k; i++)
{
cout<<odd[i]<<" ";
}

cout << "\nc) Prime numbers in array are :";

for (i = 0; i < no; i++) {


j = 2;
p = 1;
while (j < arr[i]) {
if (arr[i] % j == 0) {
p = 0;
break;
}
j++;
}
if (p == 1) {
cout << arr[i] << " ";
}
}
cout << "\nd) Complete squares in array are :";
// Traverse the array
for ( i = 0; i < no; i++) {

// Square of the element


int square = arr[i] * arr[i];

// Traverse the array


for ( j = 0; j < no; j++) {
// Check whether the value
// is equal to square
if (arr[j] == square) {
cout << arr[i] << " ";
}
}
}

getch();
}
Question No.2

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,arr[20],j,no;

cout<<"Enter Size of array: ";


cin>>no;
cout<<"Enter any "<<no<<" elements in array: ";
for(i=0;i<no;i++)
{
cin>>arr[i];
}
cout<<"Dublicate Values are: ";
for(i=0; i<no; i++)
{
for(j=i+1;j<no;j++)
{
if(arr[i]==arr[j])
{
cout<<"\n"<<arr[i];
}
}
}
getch();
}

Question No. 3

#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;

int main()
{
int p[4] ,p1[4], num = 0 , p2[] = {};
cout<<"Enter 4 numbers of the first array :";
for(int a=0;a<4;a++){
cin>>p[a];
}

cout<<"Enter 4 numbers of the second array :";


for(int a=0;a<4;a++){
cin>>p1[a];
}

for(int i = 0 ; i < 4; ++i) {


for (int j = 0 ; j < 4; ++j) {
if(p[i]==p1[j]) {
p2[num++] = p[j];
}
}
}

cout<<"The number of common elements :"<<num<<endl;


cout<<"These are the common numbers :";
for (int k=0;k<num;k++){
cout<<p2[k]<<" ";
}
getch();
return 0;
}
Question No. 4

#include<iostream>
using namespace std;
int main ()
{
int arr[10], n, i, max, min;
cout << "Enter the size of the array : ";
cin >> n;
cout << "Enter the elements of the array : ";
for (i = 0; i < n; i++)
cin >> arr[i];
max = arr[0];
for (i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
}
min = arr[0];
for (i = 0; i < n; i++)
{
if (min > arr[i])
min = arr[i];
}
cout << "Largest element : " << max;
cout << "\nSmallest element : " << min;
return 0;
}

You might also like