Professional Documents
Culture Documents
Data Structures and Algorithms Lab Journal - Lab 1
Data Structures and Algorithms Lab Journal - Lab 1
Data Structures and Algorithms Lab Journal - Lab 1
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.
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;
y = 0;
}
float Addition(int a[], int b)
{
};
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;
}
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;
}
};
int main()
{
int* Arrayy, size;
Array A;
cout << "\nMax Array Elements is = " << A.MaxValue(Arrayy, size) << endl;
cout << "\nMin Array Elements is = " << A.MinValue(Arrayy, size) << endl;
system("pause");
return 0;
}
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 :
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()
{
A.BubbleSort(Arrayy, size);
cout << "\nAfter BubbleSorting:\n";
for (int i = 0; i < size; i++)
{
cout << Arrayy[i] << endl;
}
system("pause");
return 0;
}
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 :
Sol:
#include "stdafx.h"
#include<iostream>
using namespace std;
#include<iostream>
#include<string>
#include<iomanip>
#include <windows.h>
#include <tchar.h>
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)
{
return true;
break;
}
else
{
End = Mid - 1;
}
Mid = (Start + End) / 2;
}
if (Start > End)
{
return false;
}
}
};
int main()
{
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;
}
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)
{
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];
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.
2. Exercise 2
3. Exercise 3
4. Exercise 4
5. Exercise 5
+++++++++++++++++++++++++