You are on page 1of 5

PSAP ASSIGNMENT NO.

6
Name: Owi Shamsundar Bartakke
Division : B
Roll no.:40

Q1}Write a program in c for binary


search using recursion.
#CODE
#include <stdio.h>

void binary_search(int [], int, int, int);


void bubble_sort(int [], int);

int main()
{
int key, size, i;
int list[25];

printf("Enter size of a list: ");


scanf("%d", &size);
printf("Enter elements\n");
for(i = 0; i < size; i++)
{
scanf("%d",&list[i]);
}
bubble_sort(list, size);
printf("\n");
printf("Enter key to search\n");
scanf("%d", &key);
binary_search(list, 0, size, key);

}
void bubble_sort(int list[], int size)
{
int temp, i, j;
for (i = 0; i < size; i++)
{
for (j = i; j < size; j++)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}

void binary_search(int list[], int lo, int hi, int key)


{
int mid;

if (lo > hi)


{
printf("Key not found\n");
return;
}
mid = (lo + hi) / 2;
if (list[mid] == key)
{
printf("Key found\n");
}
else if (list[mid] > key)
{
binary_search(list, lo, mid - 1, key);
}
else if (list[mid] < key)
{
binary_search(list, mid + 1, hi, key);
}
}
#OUTPUT

Q2} Write a program to implement


call by value and call by reference.

Task 1: Swap two numbers using call by


reference method

#CODE
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); //
printing the value of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The
values of actual parameters do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
Formal parameters, a = 20, b = 10
}

#OUTPUT

Task 2: Calculate average of given numbers


using call by value method.

#CODE
#include<stdio.h>
void avg(int,int);
void main()
{
int x,y;
printf("ENTER THE TWO NUMBERs\n");
scanf("%d%d",&x,&y);
avg(x,y);
}
void avg(int x,int y)
{
int c=(x+y)/2;
printf("\nThe Average is %d",c);
}
#OUTPUT

You might also like