C Programming Using Pointers

  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C Programming Using Pointers as PDF for free.

More details

  • Words: 3,749
  • Pages: 48
EX NO: DATE:

IMPLEMENTATION OF RECURSIVE ALGORITHM 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))

PROGRAM: #include<stdio.h> #include 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(); } int fib(int p) { if(p==0) return(0); if(p>=1&&p<=2) return(1); else return(fib(p-1)+fib(p-2)); } int fact(int p) { if(p==0) return(1); else return(p*fact(p-1)); } int sum(int p) { if(p==0) return(0); else return(p+sum(p-1)); }

OUTPUT: Enter the Number: 5 The Fibonacci series: 0 1 1 2 3 The factorial of 5: 120 The summation of 5: 15

RESULT: Thus the C-Program was written to implement a recursive algorithm using pointers and the output was verified

`EX.NO: DATE:

IMPLEMENTATION OF BUBBLE SORT

AIM: To write a C-Program to implement bubble sort using pointers and functions ALGORITHM: STEP 1: Start the program STEP 2: Read the value of n STEP 3: Set a for loop to read the elements of array for(i=0;ib[j] STEP 5: If so swap the two values using temporary variable t as t=a[i] b[i]=b[j] b[j]=t STEP 6: Else go back to step3.

PROGRAM: #include<stdio.h> #include void bubblesort(int*[],int); void main() { int i,n,a[100]; clrscr(); printf("\n Enter the number of elements:"); scanf("%d",&n); printf("\n Enter the array elements"); for(i=0;ib[j]) { t=b[i]; b[i]=b[j]; b[j]=t; } } } }

OUTPUT:

Enter the number of elements:6 Enter the array elements 34 32 12 456 43 56 UNSORTED ARRAY ELEMENTS 34

32

12

456

SORTED ARRAY

43

56

456

12

32

34

43

56

RESULT: Thus the C-Program was written to implement bubble sort using pointers and functions and the output was verified successfully.

EX.NO: DATE:

IMPLEMENTATION OF SELECTION SORT

AIM: To write a C-Program to implement selection sort using pointers and functions ALGORITHM: STEP 1: Start the program STEP 2: Read the value of n STEP 3: Set a for loop to read the elements of array for(i=0;i
PROGRAM: #include<stdio.h> #include void sel(int *[],int,int); int main() { int a[100],i,n; clrscr(); printf("\nEnter The number Of elements:"); scanf("%d",&n); printf("\nEnter the array elements one by one\n"); for(i=0;i
OUTPUT:

Enter the number of elements:6 Enter the array elements one by one 23 45 89 98 09 65 UNSORTED ARRAY ELEMENTS 23

45

89

98

SORTED ARRAY

65

89

98

09

23

45

09

65

RESULT: Thus the C-Program was written to implement selection sort using pointers and functions and the output was verified successfully.

EX.NO: DATE:

IMPLEMENTATION OF MERGE SORT

AIM: To write a C-Program to implement merge sort using divide and conquer strategy ALGORITHM: STEP 1: Start the program STEP 2: Read the value of n STEP 3: Set a for loop to read the elements of array for(i=0;i
PROGRAM: #include<stdio.h rel="nofollow"> #include void split(int *,int,int); void merge(int *,int,int,int,int); int a[25],b[25]; void main() { int i,n; clrscr(); printf("Enter the limit"); scanf("%d",&n); printf("\n Enter the elements"); for(i=0;i
k++; } while(i<=l1) b[k++]=a[i++]; while(j<=l2) i=f1; j=0; while(i<=l2&&j
OUTPUT:

Enter the number of elements:6 Enter the array elements 23 45 89 98 09 65 UNSORTED ARRAY ELEMENTS 23

45

89

98

SORTED ARRAY

65

89

98

09

23

45

09

65

RESULT: Thus the C-Program was written to implement merge sort using pointers and functions and the output was verified successfully.

EX.NO: DATE:

IMPLEMENTATION OF BINARY SEARCH WITH RECURSION

AIM: To write a C-Program to implement binary search using recursive functions ALGORITHM: STEP 1: Start the program STEP 2: Read the value of n STEP 3: Set a for loop to read the elements of array for(i=0;ia[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;ip if so then assign low=mid+1 STEP 6: Else check whether x= =p, if so return mid STEP 7: Else return -1

PROGRAM:

#include<stdio.h> #include 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;ia[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } for(i=0;ihigh) return-1; mid=(low+high)/2; p=a[mid]; if(x==p) return(mid); else if(x
else return binarysearch(a,x,mid+1,high); }

OUTPUT:

Enter the number of elements:6 Enter the array elements 23 45 89 98 09 65 SORTED ARRAY

09

23

45

65

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

89

98

RESULT: Thus the C-Program was written to implement binary search using recursive functions and the output was verified successfully.

EX.NO: DATE:

IMPLEMENTATION OF BINARY SEARCH WITHOUT RECURSION

AIM: To write a C-Program to implement binary search using non-recursive functions ALGORITHM: STEP 1: Start the program STEP 2: Read the value of n STEP 3: Set a for loop to read the elements of array for(i=0;ia[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;ihigh if so return -1 STEP 2: Else assign mean value of low and high to mid mid=(high+low)/2 STEP 3: Assign the value of a[mid] to p p=a[mid] STEP 4: Check if x= =p if so then return mid STEP 5: Else check whether x


PROGRAM:

#include<stdio.h> #include binarysearch(int *[],int,int); void main() { int i,j,n,a[10],t,k,b; clrscr(); printf(" ENTER THE NUMBER\n "); scanf("%d",&n); printf("Enter array elements\n"); for(i=0;ia[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } for(i=0;ik) high=mid-1; else if(p
return mid; } return-1; }

OUTPUT:

Enter the number of elements:6 Enter the array elements 23 45 89 98 09 65 SORTED ARRAY

09

23

45

65

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

89

98

RESULT: Thus the C-Program was written to implement binary search without using recursive functions and the output was verified successfully.

EX.NO: DATE:

IMPLEMENTATION OF QUICK SORT

AIM: To write a C-Program to implement quick sort using pointers and functions ALGORITHM: STEP 1: Start the program STEP 2: Assign the pointer array *a[100] as global, read the value of n STEP 3: Set a for loop to read the elements of array for(i=0;i pivot and j > first STEP 7: Check whether i is than j if so then swap the values of a[i] and a[j] temp=a[i] a[j]=a[j] a[j]=temp STEP 8: Then swap the values of a[j] and a[first] temp=a[j] a[j]=a[first] a[first]=temp STEP 9: Call another functions sort(first, j-1) and sort(j+1, last)

PROGRAM:

#include<stdio.h> #include int *a[50],n,i; void sort(int,int); void main() { clrscr(); printf("Enter the No of Elements:"); scanf("%d",&n); printf("Enter The Elements;"); for(i=0;i=pivot)&&(j>first)) j--; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[first]; a[first]=a[j]; a[j]=temp; sort(first,j-1);

sort(j+1,last); } }

OUTPUT:

Enter the number of elements:6 Enter the array elements one by one 23 45 89 98 09 65 UNSORTED ARRAY ELEMENTS 23

45

89

98

SORTED ARRAY

65

89

98

09

23

45

09

65

RESULT: Thus the C-Program was written to implement quick sort using functions and pointers and the output was verified successfully.

EX.NO: DATE:

IMPLEMENTATION OF INSERTION SORT

AIM: To write a C-Program to implement insertion sort using pointers and functions ALGORITHM: STEP 1: Start the program STEP 2: Read the value of n STEP 3: Set a for loop to read the elements of array for(i=0;i0&&b[j-1]>temp;j--) STEP 5: Assign the value of b[j-1] to b[j] and temp to b[j] b[j]=b[j-1]; b[j]=temp; STEP 6: Assign a while loop to decrement j till a[j] > pivot and j > first STEP 7: Check whether i is than j if so then swap the values of a[i] and a[j] temp=a[i] a[j]=a[j] a[j]=temp STEP 8: Then swap the values of a[j] and a[first] temp=a[j] a[j]=a[first] a[first]=temp STEP 9: Call another functions sort(first, j-1) and sort(j+1, last)

PROGRAM:

#include<stdio.h> #include void ins_sort(int *a[], int n); void main() { int i,n,*a[50]; clrscr(); printf("Enter the number of Elements"); scanf("%d",&n); printf("\nEnter the array elements \n"); for(i=0;i0&&b[j-1]>temp;j--) b[j]=b[j-1]; b[j]=temp; }

OUTPUT:

Enter the number of elements:6 Enter the array elements one by one 23 45 89 98 09 65 UNSORTED ARRAY ELEMENTS 23

45

89

98

SORTED ARRAY

65

89

98

09

23

45

09

65

RESULT: Thus the C-Program was written to implement insertion sort using pointers and functions and the output was verified successfully.

EX.NO: DATE:

IMPLEMENTATION OF 8 QUEEN PROBLEMS

AIM: To write a C-Program to implement a 8 queen program using functions ALGORITHM: STEP 1: Define the functions that are to be used STEP 2: Assign a constant value of 8 to QUEENNO STEP 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 variables STEP 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 user STEP 7: If the value is equal to n or N then exit STEP 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 variables STEP 2: Set a for loop for j for(j=0;j
#include<stdio.h> #include #define QUEENNO 8 void placequeen(int,int*); int canbeplaced(int,int,int*); void showboard(int*); void main() { int x[QUEENNO],i; clrscr(); printf("the 8 queens problem"); placequeen(0,x); printf("end"); getch(); } void placequeen(int k,int *x) { int i,j; char ch; for(i=0;i<8;i++) { if(canbeplaced(k,i,x)) { x[k]=i; if(k==7) { showboard(x); printf("want to see more?[n->stop, any-> continue]:"); scanf("%c",&ch); if(ch=='n' || ch=='N') exit(0); } if(k<7) placequeen(k+1,x); } } } int canbeplaced(int k,int i,int *x) { int j; for(j=0;j
} void showboard(int *x) { int i,j; printf("\n----------------------------------------------\n"); printf(" "); for(i=0;i<8;i++) { printf("%d",(i+1)); printf(" "); } for(i=0;i<8;i++) { printf("\n\n%d",(i+1)); for(j=0;j<8;j++) { if(j==x[i]) printf("Q"); else printf("-"); printf(" "); } printf(""); } printf("\n----------------------------------------------"); }

OUTPUT:

The 8 queens’ problem -----------------1 2 3 4 5 6 7 8 1 Q - - - - - - 2 - - Q - - - - 3 - - - - Q - - 4 - - - - - - Q 5 – Q - - - - - 6 - - - Q - - - 7 - - - - - Q - 8 - - - - - - - Q ------------------ want to see more?[n->stop, any-> continue]: n

RESULT: Thus the C-Program was written to implement an 8 queen program using functions and the output was verified successfully.

EX.NO:

IMPLEMENTATION OF MINIMUM SPANNING TREE

DATE: AIM: To write the C-Program to implement minimum spanning tree using structures, pointers and functions ALGORITHM: STEP 1: Start the program STEP 2: Assign MAX a constant value of 20 STEP 3: Declare a structure edge with structure variable *front STEP 4: Define the functions and variables required in the program globally STEP 5: Call the function create_graph() inside the main function STEP 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].v STEP 9: Stop the program FUNCTION OF CREATE_GRAPH ( ) STEP 1: Declare the local variables STEP 2: Read the number of nodes n STEP 3: Calculate the value of max_edge as max_edge=n*(n-1)/2 STEP 4: Set a for loop using i for(i=0;i<=max_edge;i++) STEP 5: Read the values of origin and destin STEP 6: Check whether origin and destin are equal to 0 STEP 7: If so exit the loop using break statement STEP 8: Read the weight of the current edge wt STEP 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 1 STEP 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,node2 STEP 3: Assign the values for node1, node2 STEP 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_tree STEP 6: Assign the value of root_n1 to father[root_n2]

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 tmp STEP 3: Assign values to tmp->u, tmp->v, tmp->weight. STEP 4: Check for the following condition. if(front==NULL||tmp->weightweight) STEP 5: If any of the conditions are true assign the value of front to tmp->link and assign tmp to front STEP 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 variable STEP 2: Assign the value of front to temp STEP 3: print the values of processed edge and return the value of tmp

PROGRAM: #include<stdio.h> #include #define MAX 20 struct 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++) { printf("%d->",tree[i].u); printf("%d\n",tree[i].v); } printf("weight of this minimum spanning tree is: %d\n",wt_tree); getch(); } create_graph() { int i,wt,max_edge,origin,destin; printf("enter no. of nodes"); scanf("%d",&n); max_edge=n*(n-1)/2; for(i=0;i<=max_edge;i++)

{ printf("enter edge %d(0 0 to quit):",i); scanf("%d%d",&origin,&destin); if((origin==0)&&(destin==0)) break; printf("enter weight for this ecge"); scanf("%d",&wt); if(origin>n||destin>n||origin<=0||destin <=0) { printf("invalid edge!"); i--; } else insert_pque(origin,destin,wt); } if(iu; node2=tmp->v; printf("n1=%d",node1); printf("n2=%d",node2); while(node1>0) { root_n1=node1; node1=father[node1]; } while(node2>0) { root_n2=node2; node2=father[node2]; } printf("rootn1=%d\n",root_n1); printf("rootn2=%d\n",root_n2); if(root_n1!=root_n2) {

insert_tree(tmp->u,tmp->v,tmp->weight); wt_tree=wt_tree+tmp->weight; father[root_n2]=root_n1; } } } void insert_tree(int i, int j, int wt) { printf("This Edge inserted in the spanning tree\n"); count++; tree[count].u=i; tree[count].v=j; tree[count].weight=wt; } void insert_pque(int i, int j, int wt) { struct edge *tmp, *q; tmp=(struct edge *)malloc(sizeof(struct edge)); tmp->u=i; tmp->v=j; tmp->weight=wt; if(front==NULL||tmp->weightweight) { tmp->link=front; front=tmp; } else { q=front; while(q->link!=NULL&&q->link->weight<=tmp->weight) q=q->link; tmp->link=q->link; q->link=tmp; if(q->link==NULL) tmp->link=NULL; } } struct edge *del_pque() { struct edge *tmp; tmp=front; printf("Edge Processed is %d->%d%d\n",tmp->u,tmp->v,tmp->weight); front=front->link;return tmp; }

OUTPUT: NUMBER OF NODES: 3 Enter the Edge 1:2 2 Enter the weight: 1 Enter the Edge 2:2 3 Enter the weight: 2 Enter the Edge 3:2 1 Enter the weight: 5 THE MINIMUM SPANNING TREE WEIGHT IS 7

RESULT: Thus the C-Program was written to implement minimum spanning tree using structures, pointers and functions and the output was verified successfully.


Related Documents