You are on page 1of 11

Lab Journal – Lab 1

Data Structures and Algorithms

Lab Journal - Lab 1


Name: Nishan E Haider

Enrollment #: 01-134192-070

Class/Section: BS-CS(3A)

Objective

This lab is intended to provide a recap of arrays in C++. In addition, the lab will also serve to
provide a review of the some of the features of object oriented programming covered in the
previous course. The concepts reviewed during this session will be frequently used in the
subsequent lab sessions.

Task 1: Exercises
Implement the following exercises.

Exercise 1 (Traversing an Array)

Write a C++ program with user-defined functions that perform the following:

a. Accept an array and its size from the main and return the sum of the elements of the array
b. Accept an array and its size from the main and return the average of the array elements

In the main program, declare an array of integers using dynamic memory allocation and call the
aforementioned functions one by one.
Sol :

#include<iostream>
#include<string>
using namespace std;
class Array
{
private:
float x;

float y;
public:
Array()
{
x = 0;

Data Structures and Algorithms Page 1


Lab Journal – Lab 1

y = 0;
}
float Addition(int a[], int b)
{

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


{
x += a[i];
}
return x;
}

float Average(int a[], int b)


{
for (int i = 0; i < b; i++)
{
y += a[i];
}
y = y / b;
return y;
}

};

int main()
{
int* ArrayOp, size;
Array A;
cout << "\nFor Addition: \n";
cout << "Enter the size of Array:";
cin >> size;
ArrayOp = new int[size];
cout << "Enter the values of Array:";
for (int i = 0; i < size; i++)
{
cin >> ArrayOp[i];
}
cout << "\nAddition of Array Elements is = " << A.Addition(ArrayOp, size) << endl;

cout << "\nAverage of Array Elements is = " << A.Average(ArrayOp, size) << endl;
system("pause");
return 0;
}

Data Structures and Algorithms Page 2


Lab Journal – Lab 1

Exercise 2 (Linear Search)

Write a C++ program with user-defined functions that perform the following:

a. Accept an array and its size from the main and return the minimum value in the array
b. Accept an array and its size from the main and return the maximum value in the array

In the main program, declare an array of integers using dynamic memory allocation and call the
aforementioned functions one by one.

Sol :

#include<iostream>
using namespace std;

class Array
{
private:
float x;

float y;
public:
Array()
{
x = 0;
y = 0;
}
float MaxValue(int a[], int b)
{
float x = a[0];
for (int i = 0; i < b; i++)
{
if (x < a[i])
{
x = a[i];
}
}
return x;
}
float MinValue(int a[], int b)
{
float y = a[0];
for (int i = 0; i < b; i++)
{
if (y > a[i])
{
y = a[i];
}
}
return y;
}

Data Structures and Algorithms Page 3


Lab Journal – Lab 1

};

int main()
{
int* Arrayy, size;
Array A;

cout << "\nEnter the size of Array:";


cin >> size;
Arrayy = new int[size];
cout << "Enter the values of Array:";
for (int i = 0; i < size; i++)
{
cin >> Arrayy[i];
}

cout << "\nMax Array Elements is = " << A.MaxValue(Arrayy, size) << endl;
cout << "\nMin Array Elements is = " << A.MinValue(Arrayy, size) << endl;
system("pause");
return 0;
}

Exercise 3 (Sorting an Array)

Write a C++ program that prompts the user to randomly store integers in an array and then calls
a function that sorts the array in an ascending order. Display the contents of the sorted array in
the main. *Use : Bubble Sort Algorithm given below :

Data Structures and Algorithms Page 4


Lab Journal – Lab 1

Sol :

class Array
{
private:
float x;

float y;
public:
Array()
{
x = 0;
y = 0;
}
void BubbleSort(int* A, int length)
{
int temp;
for (int i = 0; i <= length; i++)
{
for (int j = 0; j < length - 1; j++)
{
if (A[j + 1] < A[j])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
return;
}

};

int main()
{

int* Arrayy, size;


Array A;

cout << "\nEnter the size of Array:";


cin >> size;
Arrayy = new int[size];
cout << "Enter the values of Array:";

Data Structures and Algorithms Page 5


Lab Journal – Lab 1

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


{
cin >> Arrayy[i];
}

A.BubbleSort(Arrayy, size);
cout << "\nAfter BubbleSorting:\n";
for (int i = 0; i < size; i++)
{
cout << Arrayy[i] << endl;
}

system("pause");
return 0;
}

Exercise 4 (Binary Search)

Write a C++ program that employs binary search algorithm to search for a key value in an array.
The binary search function takes a sorted array, its size and a key value as an argument and
returns true if the key value is present in the array and false if it is not. The algorithm of binary
search is given below :

*Remember Binary Search works only on sorted arrays.

Data Structures and Algorithms Page 6


Lab Journal – Lab 1

Sol:
#include "stdafx.h"
#include<iostream>
using namespace std;
#include<iostream>
#include<string>
#include<iomanip>
#include <windows.h>
#include <tchar.h>

using namespace std;

class Array
{
private:
float x;

float y;
public:
Array()
{
x = 0;
y = 0;
}
void BubbleSort(int* x, int length)
{
int temp;
for (int i = 0; i <= length; i++)
{
for (int j = 0; j < length - 1; j++)
{
if (x[j + 1] < x[j])
{
temp = x[j];
x[j] = x[j + 1];
x[j + 1] = temp;
}
}
}
return;
}
bool search(int array[], int size, int search)
{
int Start = 0;
int End = size - 1;
int Mid = (Start + End) / 2;
while (Start <= End)
{
if (array[Mid] < search)
{
Start = Mid + 1;
}
else if (array[Mid] == search)
{

Data Structures and Algorithms Page 7


Lab Journal – Lab 1

return true;
break;
}
else
{
End = Mid - 1;
}
Mid = (Start + End) / 2;
}
if (Start > End)
{
return false;
}
}

};

int main()
{

int* Arrayy, size;


Array A;
bool check;
int Search;

cout << "\nEnter the size of Array:";


cin >> size;
Arrayy = new int[size];
cout << "Enter the values of Array:";
for (int i = 0; i < size; i++)
{
cin >> Arrayy[i];
}

A.BubbleSort(Arrayy, size);
cout << "\nAfter BubbleSorting:\n";
for (int i = 0; i < size; i++)
{
cout << Arrayy[i] << endl;
}
cout << "\nEnter the number to search: ";
cin >> Search;
check = A.search(Arrayy, size, Search);
if (check == true)
{
cout << "\nNumber Exists in the Array ";
}
else
{
cout << "\nNumber Not Found";
}
cout << endl;
system("pause");
return 0;
}

Data Structures and Algorithms Page 8


Lab Journal – Lab 1

Exercise 5 (File Handling)

Write a program that writes elements of an array (one by one ) to a file using an ofstream object
and ‘write()’ function. In the same program, declare another array (of same size) and read the
values written in the file using the ‘read()’ function of an ifstream object. Display the values read
from the file.

Sol:
#include<iostream>
#include<string>
#include <fstream>
using namespace std;
class Arrr
{
private:
float x;

float y;
public:
Arrr()
{
x = 0;
y = 0;
}
void write(int arr[], int size)
{
ofstream obj;
obj.open("file.txt");
for (int i = 0; i < size; i++)
{
obj << arr[i];
}
obj.close();
}
void read(int* arr, int size)

Data Structures and Algorithms Page 9


Lab Journal – Lab 1

{
ifstream obj;
int line;

obj.open("file.txt");
for (int i = 0; i < size; i++)
{
obj >> arr[i];
}
}
};

int main()
{
int* Arr, * Arr2, size;
Arrr A;
bool check;
int Search;
int index;
cout << "\nEnter the size of Array:";
cin >> size;
Arr = new int[size];

cout << "Enter the values of Array:";


for (int i = 0; i < size; i++)
{
cin >> Arr[i];
}
A.write(Arr, size);

Arr2 = new int[size];


A.read(Arr2, size);
cout << "Array From File: \n";
cout << *Arr2;

cout << endl;


system("pause");
return 0;
}

Data Structures and Algorithms Page 10


Lab Journal – Lab 1

Implement the given exercises and get them checked by your instructor. If you are unable to
complete the tasks in the lab session, deposit this journal alongwith your programs (printed
or handwritten) before the start of the next lab session.

S No. Exercise Checked By:


1. Exercise 1

2. Exercise 2

3. Exercise 3

4. Exercise 4

5. Exercise 5

+++++++++++++++++++++++++

Data Structures and Algorithms Page 11

You might also like