Programming made easy EX NO: IMPLEMENTATION OF RECURSIVE ALGORITHM DATE: USING POINTER AIM: To write a C-Program to implement a recursive algorithm using pointers ALGORITHM: STEP 1: Start the program STEP 2: Declare the variable, *no, factorial, sum, p, i, and the function fact(int p), sum(int p), fib(int p) STEP 3: Read the value of no. STEP 4: Call the function fact(*no), sum(*no) STEP 5: Using a for loop call the function fib(int p) and display the Fibonacci series & also display factorial & summation. STEP 6: Stop the program FUNCTION FIB (int p) STEP 1: Check whether the value of n is equal to ‘0’ if so return ‘0’ STEP 2: Else check whether (p>=1 && p<=2), if so return the value ‘1’ STEP 3: Else return ( fib(p-1)+ fib(p-2)) FUNCTION FACT (int p) STEP 1: Check whether (p==0), if so return ‘1’. STEP 2: Else return (p*fact(p-1)) FUNCTION SUM (int p) STEP 1: Check whether p==0, if so return ‘0’ STEP 2: Else return (p+sum(p-1))
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Progra
mming m
ade e
asy
EX NO: IMPLEMENTATION OF RECURSIVE ALGORITHMDATE: USING POINTER
AIM: To write a C-Program to implement a recursive algorithm using pointers
ALGORITHM:
STEP 1: Start the program STEP 2: Declare the variable, *no, factorial, sum, p, i, and the function fact(int p), sum(int p), fib(int p)STEP 3: Read the value of no.STEP 4: Call the function fact(*no), sum(*no)STEP 5: Using a for loop call the function fib(int p) and display the Fibonacci series &
also display factorial & summation.STEP 6: Stop the program
FUNCTION FIB (int p)
STEP 1: Check whether the value of n is equal to ‘0’ if so return ‘0’STEP 2: Else check whether (p>=1 && p<=2), if so return the value ‘1’STEP 3: Else return ( fib(p-1)+ fib(p-2))
FUNCTION FACT (int p)
STEP 1: Check whether (p==0), if so return ‘1’.STEP 2: Else return (p*fact(p-1))
FUNCTION SUM (int p)
STEP 1: Check whether p==0, if so return ‘0’STEP 2: Else return (p+sum(p-1))
Progra
mming m
ade e
asy
PROGRAM:
#include<stdio.h>#include<conio.h>void main(){
int i,p, *no,factorial,summ;int fact(int p);int sum(int p);int fib(int p);clrscr();printf("\n Enter The Number:");scanf("%d",no);printf("\n The Fibonnacci series: \n");for(i=0;i<*no;i++)
printf("%d\n",fib(i));factorial=fact(*no);printf("\n The factorial of %d: %d\n", *no,factorial);summ=sum(*no);printf("\nThe summation of %d: %d\n", *no,summ);getch();
Thus the C-Program was written to implement selection sort using pointers and functionsand the output was verified successfully.
Progra
mming m
ade e
asy
EX.NO: IMPLEMENTATION OF MERGE SORTDATE:
AIM: To write a C-Program to implement merge sort using divide and conquer strategy
ALGORITHM:
STEP 1: Start the programSTEP 2: Read the value of nSTEP 3: Set a for loop to read the elements of array
for(i=0;i<n;i++)STEP 4: Call the function split(a,0,n-1) STEP 5: Print the sorted array aSTEP 6: Stop the program
FUNCTION MERGE_SORT (int *a, int low, int high)
STEP 1: Declare the local variable.STEP 2: If low is less than high then assign the mean value of low
and high to mid mid =(low+high)/2
STEP 3: Call the function merge_sort(a,low,mid)STEP 4: Call the another function merge_sort(a,mid+1,high)STEP 5: Call the function combine(a,low,mid,high) FUNCTION SPLIT(int *c, int first, int last)
STEP 1: Declare the local variablesSTEP 2: Set the while loop till the condition i<=mid && j<=high is failedSTEP 3: Check whether a[i]<a[j] STEP 4: If so the assign the value of a[j] to temp[k] and increment j and k
temp[k]=a[i] j++ k++
STEP 5: Else assign a[j] to temp[k] and then increment j and k temp[k]=a[j] & j++ k++
STEP 6: Set another while loop till i is less than midSTEP 7: Assign the value of a[i] to temp[k]
temp[k]=a[i] & j++ k++STEP 8: Set another while loop till j is greater than midSTEP 9: Assign the value of a[j] to temp[k]
temp[k]=a[j] j++ k++
STEP 10: Construct a for loop for k for(k=low; k<=high; k++)
STEP 11: Assign the value of temp[k] to a[k] a[k]=temp[k]
Thus the C-Program was written to implement merge sort using pointers and functionsand the output was verified successfully.
EX.NO: IMPLEMENTATION OF BINARY SEARCH WITH RECURSIONDATE:
Progra
mming m
ade e
asy
AIM: To write a C-Program to implement binary search using recursive functions
ALGORITHM:
STEP 1: Start the programSTEP 2: Read the value of nSTEP 3: Set a for loop to read the elements of array
for(i=0;i<n;i++)STEP 4: Set a for loop
for(i=0;i<n;i++)STEP 5: Nest another for loop
for(j=i+1;j<n;j++)STEP 6: Check the condition a[i]>a[j]STEP 7: If so swap the two values using temporary variable t as
t=a[i] a[i]=a[j] a[j]=t
STEP 8: Else go back to step 6.STEP 9: Set a for loop to print the value of array a
For(i=0;i<n;i++)STEP 10: Read the search key as kSTEP 11: Assign low=0 and high=n-1STEP 12: Call the function binsearch(a,k,low,high) STEP 13: Check if ans is not equal to1 if so print the position b+i
Else print that element is not foundSTEP 14: Stop the program
FUNCTION BINARY SEARCH (int *x[ ], int x, int low, int high)
STEP 1: Set a while loop till low is greater than highSTEP 2: Assign mean value of low and high to mid
mid=(high+low)/2STEP 3: Assign the value of x[mid] to p
p=x[mid]STEP 4: Check if x<p if so assign
high=mid-1STEP 5: Else check whether x>p if so then assign
low=mid+1STEP 6: Else check whether x= =p, if so return midSTEP 7: Else return -1
PROGRAM:
Progra
mming m
ade e
asy
#include<stdio.h>#include<conio.h>binarysearch(int *[],int,int,int);void main(){ int i,j,k,t,low,high,n,a[50],ans; clrscr(); printf("\n enter the N:");
scanf("%d",&n); printf("\n enter the array element one by one\n"); for(i=0;i<n;i++) scanf("%d",&a[i]);
printf("\n sorted array \n"); for(i=0;i<n;i++)
for(j=i+1;j<n;j++) if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } for(i=0;i<n;i++) printf("\t a[%d]=%d\n",i,a[i]); printf("\t enter the element to search:"); scanf("%d",&k); low=0; high=n-1; ans=binarysearch(a,k,low,high); if(ans!=-1) printf("\nthe number %d is present in the list at location %d",k,ans); else printf(" the number is not present in the list"); getch(); } int binarysearch(int *a[],int x,int low,int high) { int mid,p; if(low>high) return-1; mid=(low+high)/2; p=a[mid]; if(x==p) return(mid); else if(x<p) return binarysearch(a,x,low,mid-1);
Progra
mming m
ade e
asy
else return binarysearch(a,x,mid+1,high); }
OUTPUT:
Progra
mming m
ade e
asy
Enter the number of elements:6
Enter the array elements 234589980965
SORTED ARRAY 09 23 45 65 89 98
Enter the element to search 23
The number 23 is present in the list at location 2
Enter the element to search 50
The number is not present in the list
Progra
mming m
ade e
asy
RESULT:
Thus the C-Program was written to implement binary search using recursive functionsand the output was verified successfully.
EX.NO: IMPLEMENTATION OF BINARY SEARCH WITHOUT RECURSIONDATE:
Progra
mming m
ade e
asy
AIM: To write a C-Program to implement binary search using non-recursive functions
ALGORITHM:
STEP 1: Start the programSTEP 2: Read the value of nSTEP 3: Set a for loop to read the elements of array
for(i=0;i<n;i++)STEP 4: Set a for loop
for(i=0;i<n;i++)STEP 5: Nest another for loop
for(j=i+1;j<n;j++)STEP 6: Check the condition a[i]>a[j]STEP 7: If so swap the two values using temporary variable t as
t=a[i] a[i]=a[j] a[j]=t
STEP 8: Else go back to step 6.STEP 9: Set a for loop to print the value of array a
for(i=0;i<n;i++)STEP 10: Read the search key as kSTEP 11: Assign low=0 and high=n-1STEP 12: Call the function binsearch(a,k,low,high) STEP 13: Check if ans is not equal to1 if so print the position b+i
Else print that element is not foundSTEP 14: Stop the program
FUNCTION BINARY SEARCH (int *a[ ], int x, int low, int high)
STEP 1: Check if low>high if so return -1STEP 2: Else assign mean value of low and high to mid
mid=(high+low)/2STEP 3: Assign the value of a[mid] to p
p=a[mid]STEP 4: Check if x= =p if so then return midSTEP 5: Else check whether x<p if so then return
clrscr();printf("Enter the No of Elements:");scanf("%d",&n);printf("Enter The Elements;");for(i=0;i<n;i++)scanf("%d",&a[i]);printf("\nUNSORTED ARRAY ELEMENTS");for(i=0;i<n;i++)printf("\t%d",a[i]);sort(0,n-1);printf("\nSORTED ARRAY");for(i=0;i<n;i++)
Thus the C-Program was written to implement insertion sort using pointers and functionsand the output was verified successfully.
EX.NO: IMPLEMENTATION OF 8 QUEEN PROBLEMSDATE:
Progra
mming m
ade e
asy
AIM: To write a C-Program to implement a 8 queen program using functions
ALGORITHM:
STEP 1: Define the functions that are to be usedSTEP 2: Assign a constant value of 8 to QUEENNOSTEP 3: Call the function placequeen(0,x)STEP 4: Print the message “end”STEP 5: Stop the program
FUNCTION VOID PLACEQUEEN(int k, int *x)
STEP 1: Declare local variablesSTEP 2: Set a for loop for i
for(i=0;i<8;i++)STEP 3: Check for result of function canbeplaced(k,i,x) STEP 4: If it is 1 then assign the value of i to x[k]
x[k]=i;STEP 5: Check if k is equal to 7 if show call the function showboard(x)STEP 6: Read the value of ch from the userSTEP 7: If the value is equal to n or N then exitSTEP 8: Check whether k is less than 7 if so then call the function placequeen(k+1,x)
FUNCTION INT CANBEPLACED(int k, int i, int *x)
STEP 1: Declare the local variablesSTEP 2: Set a for loop for j
for(j=0;j<k;j++)STEP 3: Check for the following condition
if((abs(j-k)==abs(x[j]-i)||(x[j]==i)))STEP 4: If its true return 0 else return 1
FUNCTION VOID SHOWBOARD(int *x)
STEP 1: Declare the local variablesSTEP 2: Set all the display style in printf functionSTEP 3: Set a for loop for i to print 1,2,..8 vertically and horizontally
for(i=0;i<8;i++)STEP 4: Set another for loop for j and check whether j is equal to x[i]
for(j=0;j<8;j++)STEP 5: If so then print Q else print -
Thus the C-Program was written to implement an 8 queen program using functionsand the output was verified successfully.
EX.NO: IMPLEMENTATION OF MINIMUM SPANNING TREE
Progra
mming m
ade e
asy
DATE:
AIM: To write the C-Program to implement minimum spanning tree using structures, pointers and functions
ALGORITHM:
STEP 1: Start the programSTEP 2: Assign MAX a constant value of 20STEP 3: Declare a structure edge with structure variable *frontSTEP 4: Define the functions and variables required in the program globallySTEP 5: Call the function create_graph() inside the main functionSTEP 6: Call the function make_tree()STEP 7: Set a for loop using i
for(i=1;i<=count;i++)STEP 8: Print the values of tree[i].u and tree[i].vSTEP 9: Stop the program
FUNCTION OF CREATE_GRAPH ( )
STEP 1: Declare the local variablesSTEP 2: Read the number of nodes nSTEP 3: Calculate the value of max_edge as
max_edge=n*(n-1)/2STEP 4: Set a for loop using i
for(i=0;i<=max_edge;i++)STEP 5: Read the values of origin and destinSTEP 6: Check whether origin and destin are equal to 0STEP 7: If so exit the loop using break statementSTEP 8: Read the weight of the current edge wtSTEP 9: Check the following condition
if(origin>n||destin>n||origin<=0||destin <=0)STEP 10: If any of the condition is true, print “invalid edge!” and decrement the value of i by 1STEP 11: Else call the function insert_pque(origin,destin,wt)STEP 12:Check whether i is less than n-1, if so then exit with an error message
FUNCTION OF MAKE_TREE ( )
STEP 1: Declare the local variables STEP 2: Initialize the variable tmp,node1,node2STEP 3: Assign the values for node1, node2STEP 4: Calculate and print the values of root_n1 and root_n2.STEP 5: If the two roots are not equal, call the function inset_treeSTEP 6: Assign the value of root_n1 to father[root_n2]
Progra
mming m
ade e
asy
FUNCTION OF INSERT_TREE (int i, int j, int wt )
STEP 1: Declare the local variables STEP 2: Increment the value of count STEP 3: Assign values to tree[count].u, tree[count].v, tree[count].weight.
FUNCTION OF INSERT_PQUE (int i, int j, int wt )
STEP 1: Declare the local variables STEP 2: Allocate the memory space of size, struct edge for tmpSTEP 3: Assign values to tmp->u, tmp->v, tmp->weight.STEP 4: Check for the following condition.
if(front==NULL||tmp->weight<front->weight)STEP 5: If any of the conditions are true assign the value of front to tmp->link and assign tmp to
frontSTEP 6: else set a while loop and declare following
q=q->link; tmp->link=q->link;
q->link=tmp; if(q->link==NULL)
tmp->link=NULL;
FUNCTION OF STRUCT EDGE *DEL_PQUE( )
STEP 1: Declare the local variableSTEP 2: Assign the value of front to tempSTEP 3: print the values of processed edge and return the value of tmp
Progra
mming m
ade e
asy
PROGRAM:
#include<stdio.h>#include<conio.h>#define MAX 20struct edge{
int u;int v;int weight;struct edge *link;
}*front=NULL;int father[MAX];struct edge tree[MAX];int n;int wt_tree=0;int count=0;void make_tree();void insert_tree(int i, int j, int wt);void insert_pque(int i, int j, int wt);struct edge *del_pque();void main(){
int i;create_graph();make_tree();clrscr();printf("edge to be included in spanning tree are:\n");for(i=1;i<=count;i++){