Skip to main content

Posts

Showing posts from January, 2012

Sample C Program To Implement 8 Queens Problem Using Arrays, Pointers & 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 < 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 SHOW…

Sample C Program To Implement Quick Sort Using Pointers, Functions & Arrays.

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 < n; i++)
STEP 4: Call the function sort(0, n - 1).
STEP 5: Print the sorted array a.
STEP 6: Stop the program


FUNCTION SORT (int first, int last)

STEP 1: Declare the local variable.
STEP 2: Check if first is less than last.
first < last


STEP 3: If so then assign the following
pivot = a[first]
i = first
j = last


STEP 4: Assign a while loop till the condition.
I < j

STEP 5: Assign a while loop to increment i till
a[i] < pivot and i < last

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)


SAMPLE PROGRAM:


#in…

Sample C Program To Implement Binary Search Without Using Recursive Function.

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 < 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 k.

STEP 11: Assign low = 0 and high = n – 1.
STEP 12: Call the function binsearch(a, k, low, high)
STEP 13: Check if ans is not equal to 1, if so print the position b + i. else print that element is not found.
STEP 14: Stop the program.


FUNCTION BINARY SEARCH (int *a[ ], int x, int low, int high)

STEP 1: Check if low > high 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]
S…

Sample C Program To Implement Binary Search Using Recursive Function.

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 < 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 k.

STEP 11: Assign low = 0 and high = n – 1.
STEP 12: Call the function binsearch(a, k, low, high)
STEP 13: Check if ans is not equal to 1, if so print the position b + i. Else print that element is not found.
STEP 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 high.
STEP 2: Assign mean value of low and high to mid.
mid = (high + low) / 2
STEP 3: Assign the value of x[mid] to p.
p = x[mid…

Sample C Program To Implement Merge Sort Using Divide & 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 < n; i++)
STEP 4: Call the function split ( a, 0, n – 1 )
STEP 5: Print the sorted array a.
STEP 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 variables.
STEP 2: Set the while loop till the condition i <= mid && j <= high is failed.
STEP 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]

Sample C Program To Implement Selection Sort Using Pointers, Arrays & 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 < n; i++ )
STEP 4: Call the function sel (a, 0, n – 1 )
STEP 5: Print the sorted array a.
STEP 6: Stop the program.


FUNCTION SEL (int *x, int start, int stop )

STEP 1: Declare the local variable.
STEP 2: Assign begin = start and small = begin and check if
start < stop.
STEP 3: If so then set a for loop
for(i = begin + 1 ; i <= stop; i++ )
STEP 4: Check the condition
x[i] < x[small]
STEP 6: If so then assign the value of i to small
small = i
STEP 7: Then swap the values of x[begin] and x[small] using temp
temp = x[begin]
x[begin] = x[small]
x[small] = temp
STEP 8: Call another function sel ( x, start + 1, stop )


SAMPLE PROGRAM:


#include<stdio.h>
#include<conio.h>

void sel(int *[],int,int);

int main()
{

int a[100], i, n;
clrscr();

printf(" \n Enter the number of elements: " );
scanf(" %d ", &n);
printf(" \n Enter the…

Sample C Program With Algorithm To Implement Bubble Sort Using Pointers.

ALGOTITHM

STEP 1: Start the program.
STEP 2: Read the value of n.
STEP 3: Set for loop to read the elements of array for( i = 0; i < n; i++ ).
STEP 4: Call the function bubblesort ( a , n ).
STEP 5: Print the sorted array a.
STEP 6: Stop the program.


FUNCTION BUBBBLESORT ( int *b [], int n )

STEP 1: Declare the local variable.
STEP 2: Set a for loop for( i = 0; i < n; i++ )
STEP 3: Nest another for loop for( j = 1; j < n; j++ )
STEP 4: Check the condition b[i] > b[j]
STEP 5: If so swap the two values using temporary y variable t as
t = a[i]
b[i] = b[j]
b[j] = t
STEP 6: Else go back to step3.


SAMPLE PROGRAM:

#include<stdio.h>
#include<conio.h>

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; i < n; i++)
scanf( " %d ", &a[i] );

printf( " \n UNSORTED ARRAY ELEMENTS " )…

Sample C Program To Implement 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: Use 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) - FIBONACCI SERIES.

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) - FACTORIAL OF A NUMBER.

STEP 1: Check whether (p == 0), if so return '1' .
STEP 2: Else return (p * fact( p – 1 ) ).


FUNCTION SUM (int p) - SUMMATION OF A NUMBER.

STEP 1: Check whether p == 0, if so return '0'.
STEP 2: Else return (p + sum( p – 1 ) ).



SAMPLE PROGRAM:


#include<stdio.h>
#include<conio.…