0% found this document useful (0 votes)
45 views

Practice Assignment 4: Name: Nitesh Kumar Yadav Reg. No.: 20195103 Group: B1

The document contains 5 programming problems and their solutions in C language: 1) Write a program to find the maximum and minimum element in an array. 2) Write a program to perform linear search in an array. 3) Write a program to perform binary search in an array. 4) Write a program to implement bubble sort algorithm to sort elements of an array. 5) Write a program to move all zero elements to the end of the array.

Uploaded by

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

Practice Assignment 4: Name: Nitesh Kumar Yadav Reg. No.: 20195103 Group: B1

The document contains 5 programming problems and their solutions in C language: 1) Write a program to find the maximum and minimum element in an array. 2) Write a program to perform linear search in an array. 3) Write a program to perform binary search in an array. 4) Write a program to implement bubble sort algorithm to sort elements of an array. 5) Write a program to move all zero elements to the end of the array.

Uploaded by

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

PRACTICE ASSIGNMENT 4

Name: Nitesh Kumar Yadav


Reg. No.: 20195103
Group: B1

1. WAP to find max and min element in an array.

#include <stdio.h>

int main()
{
int a[1000],i,n,min,max;

printf("Enter size of the array : ");


scanf("%d",&n);

printf("Enter elements in array : ");


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

min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);

2. WAP to perform linear search in array.

#include <stdio.h>

int main()
{ int num;
PRACTICE ASSIGNMENT 4
int i, keynum, found = 0;

printf("Enter the number of elements ");


scanf("%d", &num);
int array[num];
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}

printf("Enter the element to be searched ");


scanf("%d", &keynum);
for (i = 0; i < num ; i++)
{
if (keynum == array[i] )
{
found = 1;
break;
}
}
if (found == 1)
printf("Element is present in the array at position %d",i+1);
else
printf("Element is not present in the array\n");
}

3. WAP to perform Binary Search in an Array.

#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);

printf("Enter value to find\n");


scanf("%d", &search);
PRACTICE ASSIGNMENT 4
first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}

4. WAP to Perform Bubble Sort Algorithm.

#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
swap = array[d];
array[d] = array[d+1];
PRACTICE ASSIGNMENT 4
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

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


printf("%d\n", array[c]);

return 0;
}

5. WAP move all zeros at the end of array.


Example: Input {1,9,8,4,0,0,2,7,0,6,0}
Output: {1,9,8,4,2,7,6,0,0,0,0}

#include<stdio.h>

void reorder(int A[], int n)


{
int k = 0;
for (int i = 0; i < n; i++)
{
if (A[i] != 0)
A[k++] = A[i];
}

for (int i = k; i < n; i++)


A[i] = 0;
}

int main(void)
{
int A[] = { 6, 0, 8, 2, 3, 0, 4, 0, 1 };
int n = sizeof(A) / sizeof(A[0]);

reorder(A, n);

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


printf("%d ", A[i]);
}

You might also like