DSALab 02
DSALab 02
DSA Lab 02
#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;
}
#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;
}