0% found this document useful (0 votes)
10 views5 pages

DSALab 02

Uploaded by

itshunainkhan121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

DSALab 02

Uploaded by

itshunainkhan121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name :

Class (SE/ AI/ CS):


Roll No. :

DSA Lab 02

Activity 1 Accessing and printing array elements

int a[5] = {2, 3, 5, 7, 11};


// Accessing and printing array elements
cout << a[0] << endl;
cout << a[1] << endl;
cout << a[2] << endl;
cout << a[3] << endl;
cout << a[4] << endl;

Activity 2 Insertion at Start of Array

int array[10], n, i, item;


cout << "Enter the size of the array: ";
cin >> n;
cout << "\nEnter elements in the array: ";
for(i = 0; i < n; i++) {
cin >> array[i];
}
cout << "\nEnter the element to insert at the beginning: ";
cin >> item;
// Increment the array size
n++;
// Shift elements to the right to make space at the beginning
for(i = n; i > 1; i--) {
array[i-1] = array[i-2];
}
// Insert the new item at the beginning
array[0] = item;
// Output the resultant array
cout << "Resultant array elements: ";
for(i = 0; i < n; i++) {
cout << "\n" << array[i];
}

Activity 3 Insertion at End of Array

#include <iostream>
using namespace std;
int main() {
int array[10], i, values;
// Input array elements
cout << "Enter 5 Array Elements: ";
for(i = 0; i < 5; i++) {
cin >> array[i];
}
// Input element to insert
cout << "\nEnter Element to Insert: ";
cin >> values;
// Inserting the new element at the end of the array
array[i] = values;
// Output the new array
cout << "\nThe New Array is:\n";
for(i = 0; i < 6; i++) {
cout << array[i] << " ";
}
return 0;
}

Activity 4 Deletion at Beginning of Array

#include <iostream>
using namespace std;
int main() {
int n, array[10];
// Input the size of the array
cout << "Enter the size of an array: ";
cin >> n;
// Input elements in the array
cout << "Enter elements in the array: ";
for(int i = 0; i < n; i++) {
cin >> array[i];
}
// Decrease the size to simulate deletion (removing the first element)
n--;
// Shift elements to the left (deleting the first element)
for(int i = 0; i < n; i++) {
array[i] = array[i + 1];
}
// Output the array after deletion
cout << "\nAfter deletion: ";
for(int i = 0; i < n; i++) {
cout << array[i] << " ";
}
return 0;
}

Activity 5

Searching in Array
#include <stdio.h>
int linear(int a[], int n, int x)
{
for (int i = 0; i < n; i++)
if (a[i] == x)
return i;
return -1;
}
int main(void)
{
int a[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(a) / sizeof(a[0]);
// Function call
int result = linear(a, n, x);
if(result == -1)
{
printf("Element is not present in array");
}
else
{
printf("Element found at index: %d", result);
}
return 0;
}

Activity 6 Sorting of Element


#include <stdio.h>
int main() // Use int main() instead of void main()
{
int temp, size, array[100];
printf("Enter the size of the array: \n");
scanf("%d", &size);
printf("Enter the numbers: \n");
for (int i = 0; i < size; ++i) {
scanf("%d", &array[i]);
}
// Sorting the array using bubble sort
for (int i = 0; i < size; ++i) {
for (int j = i + 1; j < size; ++j) {
if (array[i] > array[j]) {
// Swap the elements
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("Sorted array:\n");
for (int i = 0; i < size; ++i) {
printf("%d\n", array[i]);
}
return 0; // Return an integer from main
}
Lab Graded Deletion at End of Array

You might also like