0% found this document useful (0 votes)
4 views14 pages

Dsa Lab Assignment 01

The document contains a series of programming exercises related to data structures and algorithms, specifically focusing on arrays in C. Each exercise includes a problem statement, corresponding code implementation, and expected output. Topics covered include array creation, sum of elements, finding largest and smallest elements, sorting, searching, removing elements, inserting elements, and handling duplicates.

Uploaded by

Rohan Nag
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)
4 views14 pages

Dsa Lab Assignment 01

The document contains a series of programming exercises related to data structures and algorithms, specifically focusing on arrays in C. Each exercise includes a problem statement, corresponding code implementation, and expected output. Topics covered include array creation, sum of elements, finding largest and smallest elements, sorting, searching, removing elements, inserting elements, and handling duplicates.

Uploaded by

Rohan Nag
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/ 14

SCHOOL OF COMPUTER ENGINEERING

DATA STRUCTURE AND ALGORITHM LAB

Submitted By: Rohan Nag


Roll No.: 2105738
Branch: CSE
Section: CSE-32
Q1. WAP to create an array that can store max. 50 integers and display the contents of the
array.
Code:
#include <stdio.h>
void main()
{
int arr[50];
int i, n;
printf("Enter the number of elements you want to enter:");
scanf("%d", &n);

printf("Input elements in the array :\n");


for(i=0; i<n; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}

printf("\nElements in array are: ");


for(i=0; i<n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

Output:
Q2. WAP to find out the sum of the numbers stored in an array of integers.
Code:

#include <stdio.h>
#include <stdio.h>
void main()
{
int arr[50];
int i, n, sum=0;
printf("Enter the number of elements you want to enter:");
scanf("%d", &n);

printf("Input elements in the array :\n");


for(i=0; i<n; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}

for(i = 0; i < n; i++)


sum = sum + arr[i];

printf("Sum of the array = %d\n",sum);

return 0;
}

Output:
Q3. WAP to find largest and smallest element stored in an array.
Code:
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
printf("\nInput the array elements : ");
for(i=0;i<n;++i)
scanf("%d",&a[i]);

large=small=a[0];

for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];

if(a[i]<small)
small=a[i];
}
printf("\nThe smallest element is %d\n",small);
printf("\nThe largest element is %d\n",large);

return 0;
}

Output:
Q4. WAP to display the array elements in ascending order.
Code:
#include <stdio.h>
void main (){
int num[20];
int i, j, a, n;
printf("enter number of elements in an array\n");
scanf("%d", &n);
printf("Enter the elements\n");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){
if (num[i] > num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in ascending order is:\n");
for (i = 0; i < n; ++i){
printf("%d\n", num[i]);
}
}

Output:
Q5. WAP Input N element into an array.find out sum of all even number and multiply all
odd number.
Code:
#include <stdio.h>
void main()
{
int i,n,oddmultiplication=1,evenSum=0;
printf("Enter the number of elements you want to input: ");
scanf("%d",&n);

int a[n];
for(int i=0; i<n; i++) {
printf("Enter an integer for index %d: ", i);
scanf("%d",&a[i]);
}

printf("\n\nThe integers entered are: \n");

for(i=0; i<n; i++)


{
if(a[i]%2==0)
{
evenSum=evenSum+a[i];
}
else{
oddmultiplication=oddmultiplication*a[i];
}
}
printf("The multiplication of all odd numbers are: %d",oddmultiplication);
printf("\nThe sum of even numbers are: %d",evenSum);
}

Output:
Q6. WAP to search a particular number from the array.
Code:
#include <stdio.h>
int main() {
int a, i, r, arr[50];
printf("Enter the length of the array: ");
scanf("%d", &a);

printf("Enter the array elements: ");


for (i = 0; i < a; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the item to be searched: ");


scanf("%d", &r);

i = 0;
while (i < a && r != arr[i]) {
i++;
}
if (i < a) {
printf("The element is found in the position = %d", i + 1);
} else {
printf("Element not found!");
}
return 0;
}

Output:
Q7. WAP to remove a specific element from the array.
Code:
#include<stdio.h>
void main()
{
int key,n, i, index = -1;
printf("Enter number of element in array:");
scanf("%d",&n);
int a[n];
for(int i=0; i<n; i++)
{
printf("Enter an integer for index %d: ", i);
scanf("%d",&a[i]);
}
printf("Enter element to delete\n");
scanf("%d",&key);

for(i = 0; i < n; i++)


{
if(a[i] == key)
{
index = i;
break;
}
}
if(index != -1)
{

for(i = index; i < n - 1; i++)


a[i] = a[i+1];

printf("New Array : ");


for(i = 0; i < n - 1; i++)
printf("%d ",a[i]);
}
else
printf("Element Not Found\n");

}
Output:
Q8. WAP to insert a new element in a specified position in the array.
Code:
#include<stdio.h>
int main()
{
int n, i, pos, num;
printf("Enter number of elements:");
scanf("%d",&n);
int a[n];
printf("Enter integer numbers\n", (n));
for(i = 0; i < (n); i++)
scanf("%d", &a[i]);

printf("Enter the position where new number has to be inserted\n");


scanf("%d", &pos);

if(pos < n)
{
printf("Enter a new number to be inserted at position %d\n", pos);
scanf("%d", &num);
for(i = n; i > pos; i--)
a[i] = a[i - 1];

a[pos] = num;

printf("Array after inserting %d at position %d\n", num, pos);


for(i = 0; i < n; i++)
printf("%d\n", a[i]);
}
else
{
printf("cannot be inserted ");
}

printf("\n");

return 0;
}
Output:
Q9. WAP to remove duplicates from the Array.
Code:
#include <stdio.h>
int main ()
{
int i, j, k,n;
printf ("Enter the number of elements in an array: ");
scanf (" %d", &n);
int a[n];
printf (" \n Enter elements of an array: \n ",n);
for ( i = 0; i <n; i++)
{
scanf (" %d", &a[i]);
}
for ( i = 0; i <n; i ++)
{
for ( j = i + 1; j <n; j++)
{
if ( a[i] == a[j])
{
for ( k = j; k <- 1; k++)
{
a[k] = a [k + 1];
}
n--;
j--;
}
}
}
printf (" \n Array elements after deletion of the duplicate elements: ");
for ( i = 0; i <n; i++)
{
printf (" %d \t", a[i]);
}
return 0;
}
Output:
Q10. WAP to store numbers into an array of n integers, where the array must contain some
duplicates. Find out the most repeating element in the array.
Code:
#include <stdio.h>

int main()
{
int arr[] = {23, 42, 54, 28, 22, 78, 23, 87, 54};

int length = sizeof(arr) / sizeof(arr[0]);

printf("Duplicate elements in given array: \n");


for (int i = 0; i < length; i++)
{
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
{
printf("%d\n", arr[j]);
}
}
}
return 0;
}

Output:

Submitted By: Rohan Nag


Roll No.: 2105738
Branch: CSE
Section: CSE-32

You might also like