0% found this document useful (0 votes)
50 views11 pages

Data Structures and Algorithm Experiment - 1: Name: Ajeetha Reg No: 20BEC1192

The document contains 5 tasks related to C programming problems and their solutions. Each task provides the aim, algorithm and code for problems related to finding local maximums in an array, checking if the sum of two numbers in an array equals a given value, counting the number of zeros in a number, checking occurrences of a digit in a number, and printing the diagonal elements of a matrix.

Uploaded by

Abdul Rauf
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)
50 views11 pages

Data Structures and Algorithm Experiment - 1: Name: Ajeetha Reg No: 20BEC1192

The document contains 5 tasks related to C programming problems and their solutions. Each task provides the aim, algorithm and code for problems related to finding local maximums in an array, checking if the sum of two numbers in an array equals a given value, counting the number of zeros in a number, checking occurrences of a digit in a number, and printing the diagonal elements of a matrix.

Uploaded by

Abdul Rauf
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/ 11

DATA STRUCTURES AND ALGORITHM

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:

Given a number 𝑘 and a digit 𝑑, to write a C program to

a. check if 𝑑 occurs in 𝑘

b. output the number of times 𝑑 occurs in 𝑘

c. output the exact position(s) at which 𝑑occur in 𝑘.


Algorithm:
BEGIN
Declare and define the variables k,d,a,j,n1 as int and arr as array.
Get the input of number k and element to be checked d from the user and store it.
Calculate k/10 and update the value of k as k/10
Check if entered d value matches with any element of k
Print the location of d and number of times d occurred.
EXIT
Code:
#include<stdio.h>
int main()
{
int k,d;
printf("Enter k: \n");
scanf("%d",&k);
printf("Enter d: \n");
int a;
scanf("%d",&d);
int arr[10];
int j=0;
while(k!=0)
{
a=k%10;
arr[j]=a;
k=k/10;
j=j+1;
}
int n1=j;
int arr1[10];
for(int i=0;i<n1;i++)
{
arr1[j]=arr[i];
j=j-1;
}
printf("\n");
int sum=0;
for(int i=0;i<=n1;i++)
{
if(arr1[i]==d)
{
sum=sum+1;
printf("d occurs at: %d\n",i);
}
}
if(sum>0)
{
printf("d occured %d times ",sum);
}
else
{
printf("d occured 0 times");
}
}

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:

You might also like