Data Structures and Algorithm Experiment - 1: Name: Ajeetha Reg No: 20BEC1192
Data Structures and Algorithm Experiment - 1: Name: Ajeetha Reg No: 20BEC1192
EXPERIMENT – 1
Name: Ajeetha
Reg no: 20BEC1192
TASK 1:
Aim:
To write a C program, which outputs all local maximums of a given data of elements. A
number xi is a local maximum if it is more than both xi-1 and xi+1. If the elements are 25, 19,
22, 23, 21, 12, 10, 17, 11, 13, 10 then 23, 17 and 13 are local maximums.
Algorithm:
BEGIN
Add header files
Declaring and defining variables n,i,j,m,k as int and p&q as arrays.
Get inputs of number of elements and the numbers from the user and store it in n and p
respectively.
Check if(p[j]>p[j+1] && p[j]>p[j-1])
Print the local maximas which is is more than both xi-1 and xi+1
EXIT.
Code:
#include <stdio.h>
int main() {
int n,i,j,m=0,k;
printf("Enter the number of elements \n");
scanf("%d",&n);
int p[n];
int q[n-2];
printf("Enter %d numbers \n",n);
for (i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
for(j=1;j<n-1;j++)
{
if(p[j]>p[j+1] && p[j]>p[j-1])
{
q[m]=p[j];
m++;
}
}
printf("The Local maximas are \n");
for (i=0;i<m;i++)
{
printf("%d \n",q[i]);
}
}
Output:
TASK 2:
Aim:
To write a program to check if the sum of any two numbers in 𝐴 is equal to 𝑘. Let 𝐴[1…𝑛] be
an array of integers and let 𝑘 be a number.
Algorithm:
BEGIN
Declare and Define the variables sum,i,Size as int and A as array.
Get the inputs of size of array,elements of array and number to be checked.
Check if (A[i] + A[j] == sum)
If the condition is true, print Yes
If the condition is false, else statement is executed and print No.
EXIT
Code:
#include <stdio.h>
int main(void)
{
int sum;
int i;
int A[10];
int Size;
int flag=0;
printf("Enter the Size\n");
scanf("%d", &Size);
printf("Enter k(sum of nos to be checked) \n");
scanf("%d", &sum);
printf("Enter the Elements\n");
for(i = 0; i < Size; i++)
{
scanf("%d", &A[i]);
}
size_t size = sizeof(arr) / sizeof(int);
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (A[i] + A[j] == sum)
flag=1;
break;
}
}
if(flag==1)
printf("Yes");
else
printf("No");
return 0;
}
Output:
TASK 3:
Aim:
Given an number 𝑘, to write a program to find the number of ‘0’s in 𝑘.
Algorithm:
BEGIN
Declare and define the variables k,i and count
Get the input of number k from the user and store it.
Check whether zeros are present in k if(k[i] == '0')
Print the number of zeros present in k
EXIT.
Code:
#include<stdio.h>
#include<string.h>
int main(){
char k[10];
printf("Enter the value of k: \n");
scanf("%s",&k);
int i;
int count=0;
for(i=0;i<strlen(k);i++)
{
if(k[i] == '0')
{
count = count + 1;
}
}
printf("The number of 0's is : %d",count);
return 0;
}
Output:
TASK 4:
Aim:
a. check if 𝑑 occurs in 𝑘
Output:
TASK 5:
Aim:
Write program to print out the diagonal elements. Optimize on the code to use lesser loop
statements, lesser conditional statements.
Algorithm:
BEGIN
Declare and Define the variables rows,cols,i,j as int
Get input of number of rows and columns of matrix from the user and store it in rows and
cols respectively.
Get input of each element of matrix from the user.
Print the diagonal elements b[0][i]=a[i][i]; b[1][i]=a[i][rows-i-1];
EXIT
Code:
#include <stdio.h>
int main() {
int rows,cols,i,j;
printf("Enter number of rows and columns \n");
scanf("%d %d",&rows ,&cols);
int a[rows][cols];
int b[2][rows];
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("Enter a%d%d \n",i,j);
scanf("%d",&a[i][j]);
}
}
for (i=0;i<rows;i++)
{
b[0][i]=a[i][i];
}
for (i=0;i<rows;i++)
{
b[1][i]=a[i][rows-i-1];
}
for(i=0;i<2;i++)
{
for(j=0;j<cols;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
}
Output: