DS File
DS File
1. Program for using Dynamic Functions (malloc(), calloc(), realloc() and free())
functions.
(a) malloc()
#include <iostream>
#include <cstdlib> // For malloc(), exit()
using namespace std;
int main()
{
int *ptr, num, i;
cout << "Enter number of elements: ";
cin >> num;
ptr = (int*) malloc(num * sizeof(int));
if (ptr == NULL) {
cout << "\nMemory not allocated." << endl;
exit(0);
}
else {
cout << "\nMemory successfully allocated using malloc.\n\n";
for (i = 0; i < num; ++i) {
cout << "Enter element " << i + 1 << ": ";
cin >> *(ptr + i);
}
cout << "\nYou've entered: ";
for (i = 0; i < num; ++i) {
cout << *(ptr + i) << " ";
}
cout << endl;
}
free(ptr);
return 0;
}
Output:
(b) calloc()
#include <iostream>
#include <cstdlib> // For calloc() and exit()
using namespace std;
int main()
P a g e 1 | 13
Data Structures Laboratory (UGCA 1918)
{
int *ptr, num;
cout << "Enter number of elements: ";
cin >> num;
// Dynamic memory allocation using calloc()
ptr = (int*) calloc(num, sizeof(int)); // type casting is needed in C++
if (ptr == nullptr) {
cout << "\nMemory not allocated." << endl;
exit(0);
}
else {
cout << "\nMemory successfully allocated using calloc.\n\n";
for (int i = 0; i < num; ++i) {
cout << "Enter element " << i + 1 << ": ";
cin >> ptr[i];
}
cout << "\nThe elements entered are: ";
for (int i = 0; i < num; ++i) {
cout << ptr[i] << " ";
}
cout << endl;
}
free(ptr);
return 0;
}
Output:
(c) realloc()
#include <iostream>
#include <cstdlib> // For malloc(), realloc(), free()
using namespace std;
int main()
{
int *ptr, num, i;
cout << "Enter number of elements: ";
cin >> num;
ptr = (int*) calloc(num, sizeof(int));
if (ptr == NULL) {
cout << "\nMemory not allocated." << endl;
exit(0);
P a g e 2 | 13
Data Structures Laboratory (UGCA 1918)
}
else {
cout << "Memory successfully allocated using calloc.\n\n";
for (i = 0; i < num; ++i) {
cout << "Enter the element " << i + 1 << ": ";
cin >> *(ptr + i);
}
cout << "\nYou've entered: ";
for (i = 0; i < num; ++i) {
cout << *(ptr + i) << ", ";
}
cout << "\n\nEnter the new size of the array: ";
cin >> num;
ptr = (int*) realloc(ptr, num * sizeof(int));
if (ptr == NULL) {
cout << "\nMemory reallocation failed." << endl;
exit(0);
}
else {
cout << "\nMemory successfully re-allocated using realloc.\n\n";
for (i = 0; i < num; ++i) {
cout << "Enter the element " << i + 1 << ": ";
cin >> *(ptr + i);
}
cout << "\nYou've entered: ";
for (i = 0; i < num; ++i) {
cout << *(ptr + i) << ", ";
}
free(ptr); // Freeing the allocated memory
}
}
return 0;
}
Output:
P a g e 3 | 13
Data Structures Laboratory (UGCA 1918)
(d) free()
#include <iostream>
#include <cstdlib> // For malloc, calloc, free
using namespace std;
int main()
{
int *ptrMalloc, *ptrCalloc, num;
cout << "Enter number of elements: ";
cin >> num;
ptrMalloc = (int*) malloc(num * sizeof(int));
ptrCalloc = (int*) calloc(num, sizeof(int));
if (ptrMalloc == nullptr || ptrCalloc == nullptr) {
cout << "Memory not allocated." << endl;
free(ptrMalloc); // Free if allocated
free(ptrCalloc); // Free if allocated
return 1;
}
else {
cout << "Memory successfully allocated using malloc." << endl;
free(ptrMalloc);
cout << "Malloc memory successfully freed." << endl;
cout << "\n\nMemory successfully allocated using calloc." << endl;
free(ptrCalloc);
cout << "Calloc memory successfully freed." << endl;
}
return 0;
}
Output:
P a g e 4 | 13
Data Structures Laboratory (UGCA 1918)
2. Program to insert, delete and traverse an element from an array.
P a g e 6 | 13
Data Structures Laboratory (UGCA 1918)
Output:
P a g e 7 | 13
Data Structures Laboratory (UGCA 1918)
3. Program to merge one dimensional arrays.
# include <iostream>
using namespace std;
int main()
{
int arr1[20], arr2[20], n1, n2, i;
cout<<"Enter total number of elements in 1st array: ";
cin>>n1;
cout<<"Enter the elements: \n";
for(i=0; i<n1; i++){
cin>>arr1[i];
}
cout<<"\nEnter total number of elements in 2nd array: ";
cin>>n2;
for(i=0; i<n2; i++){
cin>>arr2[i];
}
int merged[n1 + n2];
for (int i = 0; i < n1; i++){
merged[i] = arr1[i];
}
for (int i = 0; i < n2; i++){
merged[n1 + i] = arr2[i];
}
cout<<"\nMerged Array: ";
for (int i = 0; i < n1 + n2; i++){
cout<<merged[i]<<" ";
}
return 0;
}
Output:
P a g e 8 | 13
Data Structures Laboratory (UGCA 1918)
4. Program for addition and subtraction of two matrices.
#include<iostream>
using namespace std;
int main()
{
int a[2][3], b[2][3], sum[2][3], diff[2][3]; // Declare matrices
// Input for the first matrix
cout << "ENTER THE VALUES OF THE 1ST MATRIX: " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cin >> a[i][j];
}
}
// Input for the second matrix
cout << "ENTER THE VALUES OF THE 2ND MATRIX: " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cin >> b[i][j];
}
}
// Display the first matrix
cout << "THE 1ST MATRIX:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << a[i][j] << "\t";
}
cout << endl;
}
// Display the second matrix
cout << "THE 2ND MATRIX:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << b[i][j] << "\t";
}
cout << endl;
}
// Adding the matrices and storing in matrix 'sum'
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
// Subtracting the matrices and storing in matrix 'diff'
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
diff[i][j] = a[i][j] - b[i][j];
}
P a g e 9 | 13
Data Structures Laboratory (UGCA 1918)
}
// Display the resultant matrix after addition
cout << "THE RESULTANT MATRIX AFTER ADDITION:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << sum[i][j] << "\t";
}
cout << endl;
}
// Display the resultant matrix after subtraction
cout << "THE RESULTANT MATRIX AFTER SUBTRACTION:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << diff[i][j] << "\t";
}
cout << endl;
}
return 0;
}
Output:
P a g e 10 | 13
Data Structures Laboratory (UGCA 1918)
5. Program for implementing multiplication of two matrices.
#include<iostream>
using namespace std;
int main() {
int a[2][3], b[3][2], product[2][2]; // Matrices: a (2x3), b (3x2), and product (2x2)
// Input for the first matrix
cout << "ENTER THE VALUES OF THE 1ST MATRIX (2x3): " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cin >> a[i][j];
}
}
// Input for the second matrix
cout << "ENTER THE VALUES OF THE 2ND MATRIX (3x2): " << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
cin >> b[i][j];
}
}
// Initialize the product matrix with 0
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
product[i][j] = 0;
}
}
// Multiplying the matrices and storing the result in matrix 'product'
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
product[i][j] += a[i][k] * b[k][j];
}
}
}
// Display the first matrix
cout << "THE 1ST MATRIX (2x3):" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << a[i][j] << "\t";
}
cout << endl;
}
// Display the second matrix
cout << "THE 2ND MATRIX (3x2):" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
cout << b[i][j] << "\t";
}
P a g e 11 | 13
Data Structures Laboratory (UGCA 1918)
cout << endl;
}
// Display the resultant matrix after multiplication
cout << "THE RESULTANT MATRIX AFTER MULTIPLICATION (2x2):" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << product[i][j] << "\t";
}
cout << endl;
}
return 0;
}
Output:
P a g e 12 | 13
Data Structures Laboratory (UGCA 1918)
6. Implement linear search using one and two dimensional arrays.
#include <iostream>
using namespace std;
int linearSearch(int arr[], int size, int key)
{
for (int i = 0; i < size; ++i) {
if (arr[i] == key) {
return i; // Return the index where the key is found
}
}
return -1; // Return -1 if the key is not found
};
int main()
{
int arr[20], n, key, search;
cout<<"Enter total number of elements in an array: ";
cin>>n;
cout<<"Enter the elements: ";
for(int i = 0; i < n; i++){
cin>>arr[i];
}
cout<<"\nEnter the value you want to search in the array: ";
cin>>key;
search = linearSearch(arr, n, key);
if (search != -1){
cout<<"\nElement found at index: "<<search;
}
else{
cout<<"\nElement not found !";
}
return 0;
}
Output:
P a g e 13 | 13