Wednesday, December 29, 2010

decimal to binary and binary to 1's and 2's complement




/*****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for decinal to binanry and 1's and 2's complement
*/


#include<stdio.h>
#include <string.h>


void
main()
{


struct
binary
{

int
f : 2; // we use bit field here because it occupies less space

} b[100], c[100], d[100];

/**
* b[] will store binary, c[] will store 1's complement
* d[] will store 2's complement
*/
/* a will store decomal*/

long int
a;
long int
n, i, j; // n is the length of binary. i and j are for loops.

// now scanning pisitive number

do
{

printf("\nEnter a valid positive integer number: ");
scanf("%ld", &a);
}


while
(a < 0);
for
(i = 0; a != 0; i++)
{


b[i].f = a % 2;
a = a / 2;
}


n = i - 1;
printf("\nBinary of entered number is: ");

for
(i = n; i >= 0; i--)
{


printf("%d", b[i].f);
}

/**
* 2's complement of binary nyumber
* binary : 1 0 1 0 1 1 0
* 2's complement: 0 1 0 1 0 1 0
* while going right to left put 0 as it is untill you get 1. put 1 as it is
* and then change 1 to 0 and 0 to 1.
*/

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


if
(b[i].f == 1)
{

d[i].f = 1;

i++;
for
(j = i; j <= n; j++)
{


if
(b[j].f == 1)
{

d[j].f = 0;
}


else

{

d[j].f = 1;
}
}


break
;
}

else

{

d[i].f = 0;
}
}


printf("\n\n2's complement of number is: ");
for
(i = n; i >= 0; i--)
{


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


/**
* getting and printing 1's complement
* binary : 1 0 1 0 1 1 0
* 1's complement : 0 1 0 1 0 0 1
* this one is having simple logic. change all 0 to 1 and all
* 1 to 0.
*/


printf("\n\n1's complement of number is: ");
for
(i = n; i >= 0; i--)
{


if
(b[i].f == 0)
{

c[i].f = 1;

// if it is 0 then change to 1
}
else

{

c[i].f = 0;
}


printf("%d", c[i].f);
}

printf("\n");
}


general palindrome check using stack

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for general palindrome string
*/


#include<stdio.h>
#include<string.h>

#define MAX 100
#define TRUE 1
#define FALSE 0

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


void
main()
{


int
i;
char
a, b;
char
c[MAX];

int
valid = TRUE;
int
TOP = -1;

char
s[MAX];
printf("Enter string: ");
gets(c);

//remember that using gets() function is not safe.
for (i = 0; i < strlen(c) / 2; i++)
{


TOP = push(s, TOP, c[i]);
}


for
(; c[i] != '\0'; i++)
{

if
(TOP < 0)
{


valid = FALSE;
break
;
}

else

{


a = pop(s, &TOP);
if
(c[i] != a)
{


valid = FALSE;
}
}
}

if
(valid == TRUE)
{


printf("Valid palindrome string\n");
}

else

{

printf("Invalid palindrome string\n");
}
}


wcwr palindrome using stack

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for wcwr palindrome string
*/


#include<stdio.h>
#define MAX 100
#define TRUE 1
#define FALSE 0


int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}
else
{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}
else
{

printf("Stack underflow\n");

return
0;
}
}


void
main()
{

int
i;

char
a, b;
char
c[MAX];

int
valid = TRUE;
int
TOP = -1;

char
s[MAX];
//remember that declraing the stack as global is not a good practice (according to my openion)
printf("Enter string: ");
gets(c);

TOP = push(s, TOP, 'c');
for
(i = 0; c[i] != 'c'; i++) // you can modify this string as you want

{ // some people may modify to go upto n/2 etc
TOP = push(s, TOP, c[i]);
}



i++;
for
(; c[i] != '\0'; i++)
{


if
(TOP < 0)
{

valid = FALSE;

break
;
}
else
{

a = pop(s, &TOP);

if
(c[i] != a)
{

valid = FALSE;
}
}

}


if
(TOP == 0)
{

a = pop(s, &TOP);

if
(a != 'c')
{

valid = FALSE;
}
}
else

{

valid = FALSE;
}

if
(valid == TRUE)
{


printf("Valid palindrome string\n");
}
else
{

printf("Invalid palindrome string\n");
}
}


Tuesday, December 28, 2010

pattern without array






/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for pattern
*/

#include<stdio.h>
#define TRUE 0
#define FALSE 1
int n;

int
getno(int i, int j);
int
find_max(int);

void
main()
{

int
i, j, flag = 1;

int
space;
int
leave_sp;
int
k = 0, l;

printf("Enter no: ");
scanf("%d", &n);
space = find_max(n * n);

leave_sp = (n - 1);
for
(i = 1; i <= ((n - 1)*2) + 1; i++)
{


printf("\n");
for
(k = 0; k < leave_sp; k++)
{


for
(l = 0; l < space; l++)
{


printf(" ");
}
}

for
(j = 1; j <= flag && flag > 0; j++)
{


//printf("%0*d ",-space-1,getno(i,j) );
printf("%*d ", -space - 1, getno(i, j));
}


if
(i >= n)
{

leave_sp++;
flag--;
}
else

{

leave_sp--;
flag++;
}
}

//gethc();
//return 0;
printf("\n");
}


int
getno(int i, int j)
{

int
ti = 0, tj = 0, count = 0, temp;

void
checker(void);
int
main_;
int
br = FALSE;

main_ = n;
temp = n;
count = 0;

br = FALSE;
while
(i != ti && j != tj)
{


if
(br == TRUE)
{

break
;
}

while
(count < temp)
{


ti++;
tj++;
count++;
if
(ti == i && tj == j)
{


br = TRUE;
break
;
}
}

if
(br == TRUE)
{


continue
;
}

main_--;
temp += main_;
while
(count < temp)
{


ti++;
tj--;
count++;
if
(ti == i && tj == j)
{


br = TRUE;
break
;
}
}

if
(br == TRUE)
{


continue
;
}

temp += ((main_ - 1)*2) + 1;

while
(count < temp)
{

ti--;
count++;

if
(ti == i && tj == j)
{


br = TRUE;
break
;
}
}

main_--;
if
(br == TRUE)
{


continue
;
}

temp += (main_);

}

return
count;
}


int
find_max(int a)
{

int
i = 0;

while
(a > 0)
{

a /= 10;

i++;
}

return
i;
}

stack using array

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for stack using array
*/

#include<stdio.h>
#define SIZE 100


int
peep(int a[], int top, int i)
{


if
( i < 0 && i>= top)
{

printf("This is invalid operation. Index out of range.");
}


else

{

return
a[i];
}
}


int
push(int *a , int top, int ele)
{


if
(top >= SIZE)
{

printf("\nStack Overflow");
}

else


{

a[++top] = ele;
}

return
top;
}


int
pop(int *a,int *top)
{

if
((*top) >=0)
{
(*
top) = (*top) -1 ;

return
a[(*top)+1];
}

else

{

printf("Stack underflow\n");

return
0;
}
}

void
change(int *a,int top, int index, int element)
{


if
(index < 0 || index > top)
{

printf("The index is out of range");
}


else

{

a[index] = element;
}
}



void
main()
{

// void push(int [] ,int ,int );
int i= 0 ;

int
top = -1;
int
a[SIZE];

int
d = 1;
int
temp,temp1;

do

{

printf("\n\nSelect any option");
printf("\n1 Display stack");

printf("\n2 push");
printf("\n3 pop");
printf("\n4 peep");

printf("\n5 change");
printf("\n6 exit\n");
scanf("%d",&d);


switch
(d)
{

case
1:
if
(top>=0)
{


for
(i = 0 ; i <= top ; i++ )
{


printf("\nelement at %d is %d" , i , peep(a,top,i));
}
}


else

{

printf("\nThe stack is empty");
}

break
;
case
2:

printf("Enter number you want to insert in the stack: ");
scanf("%d",&temp);
top = push( a , top , temp);

break
;
case
3:
printf("The popped element is: %d" , pop(a,&top));

break
;
case
4:
printf("At which position you want to peep ? ");
scanf("%d",&temp);

printf("Peeped element is: %d" , peep(a,top,temp) );
break
;

case
5:
printf("Enter the index at which you want to change: ");
scanf("%d",&temp);

printf("Enter he actual element that you want to enter: ");
scanf("%d",&temp1);
change(a,top,temp,temp1);

break
;
default
:
printf("Please enter a valid choice.");
break
;

}
}


while
(d != 6);
// and yes, i think you will be able to writ getch here (on TC3)
// i am on linux so that thing is not available here
}

Addition of two matrices/matrix

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for addition of matrices
*/

#include<stdio.h>
void main()
{


int
ar = 0 , ac = 0 , i = 0 , j = 0 , br = 0 , bc = 0 ;

int
a[20][20] , b[20][20] , ans[20][20];

// scan the first array
printf("Enter no of rows in first matrix ? ");
scanf("%d",&ar);

printf("Enter no of columns in first matrix ? ");
scanf("%d",&ac);
if
(ar < 0 || ar>20 || ac < 0 || ac>20)
{


printf("Invalid number of rows or columns. It must be brtween 1 to 20");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < ar ;i++)
{


for
(j = 0 ; j < ac ; j++)
{


printf("Enter element [%d][%d]: ", i,j);
scanf("%d",&a[i][j]);
}
}


printf("Enter no of rows in second matrix ? ");
scanf("%d",&br);
printf("Enter no of columns in second matrix ? ");

scanf("%d",&bc);

// scan the second array
if(br < 0 || br>20 || bc < 0 || bc>20)
{


printf("Invalid number of rows or columns. It must be brtween 1 to 20");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < br ;i++)
{


for
(j = 0 ; j < bc ; j++)
{


printf("Enter element [%d][%d]: ", i,j);
scanf("%d",&b[i][j]);
}
}


if
((ar == br ) && (ac == bc))
{


for
( i = 0 ; i < ar ; i++)
{


for
(j = 0 ; j < ac ; j++)
{


ans[i][j] = a[i][j] + b[i][j];
}
}


printf("\nAddition of marices is:\n");
for
(i = 0 ; i < ar ; i++)
{


for
(j = 0 ; j < ac ; j++)
{


printf( "%d \t" , ans[i][j]);
}

printf("\n");
}
}


else

{

printf("\nNo of rows does not match no of columns. So addition is not possible");
}
}
}
}

set operatoin A - B | subtraction of two arrays

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for set operatoin A - B | subtraction of two arrays
*/

#include<stdio.h>
#define TRUE 0
#define FALSE 1
void main()
{


int
n1 = 0 , i =0 , n2=0 , j = 0 , flag =FALSE , no1[100] , no2[100];

int
result[200];
int
r1 = 0 ;

printf("How many numbers you want to scan in first array ? ");
scanf("%d",&n1);
if
(n1 < 0 || n1 > 100 )
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n1 ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no1[i]);
}
}


printf("How many numbers you want to scan in second array ? ");
scanf("%d",&n2);
if
(n2 < 0 || n2 > 100)
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n2 ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no2[i]);
}
}



for
(i = 0 ; i < n1 ; i++)
{


flag = FALSE ;
for
(j = 0 ; j< n2 ; j++ )
{


if
(no1[i] == no2[j] )
{

flag = TRUE;
}
}


if
(flag == FALSE)
{

result[r1++] = no1[i];
}
}




// now display the array
printf("\nTHe array (A-B) is : ");
for
(i = 0 ; i < r1 ; i++ )
{


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

printf("\b \n");
}

intersection of two arrays

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for intersection of two arrays
*/

#include<stdio.h>
void main()
{


// here for the program of union intersection and minus
// of arrays, we assume that there is no repetation of numbers in
// sinble array
int n1 = 0 , i =0 , n2=0 , j = 0 , no1[100] , no2[100];

int
result[200];
int
r1 = 0 ;

printf("How many numbers you want to scan in first array ? ");
scanf("%d",&n1);
if
(n1 < 0 || n1 > 100 )
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n1 ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no1[i]);
}
}


printf("How many numbers you want to scan in second array ? ");
scanf("%d",&n2);
if
(n2 < 0 || n2 > 100)
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n2 ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no2[i]);
}
}



// now the intersection of two arrays
// now we will copy the elements one by one
// if some element exist in both the arrays then only we will copy them
for(i = 0 ; i < n1 ; i++)
{


for
(j = 0 ; j< n2 ; j++ )
{


if
(no1[i] == no2[j] )
{

result[r1++] = no1[i];
}
}
}


// now display the array
printf("\nTHe array (intersection) is : ");
for
(i = 0 ; i < r1 ; i++ )
{


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

printf("\b \n");
}

union of two arrays

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for union of two arrays
*/

#include<stdio.h>
#define TRUE 0
#define FALSE 1
void main()
{


int
n1 = 0 , i =0 , n2=0 , j = 0 , flag =FALSE , no1[100] , no2[100];

int
result[200];
int
r1 = 0 ;

printf("How many numbers you want to scan in first array ? ");
scanf("%d",&n1);
if
(n1 < 0 || n1 > 100 )
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n1 ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no1[i]);
}
}


printf("How many numbers you want to scan in second array ? ");
scanf("%d",&n2);
if
(n2 < 0 || n2 > 100)
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n2 ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no2[i]);
}
}




// now the union of two arrays

// first we will copy first ayyar in result
printf("\n");
for
(i = 0 ; i < n1 ; i++)
{


result[r1++] = no1[i];
}


// now we will copy the elements one bt one
// but before that we will check if that element alaready exists in the array
// if it already exists then we will not copy otherwise we will copy
for(i = 0 ; i < n2 ; i++)
{


flag = FALSE ;
for
(j = 0 ; j< r1 ; j++ )
{


if
(no2[i] == result[j] )
{

flag = TRUE;
}
}


if
(flag == FALSE)
{

result[r1++] = no2[i];
}
}




// now display the array
printf("\nTHe array (union) is : ");
for
(i = 0 ; i < r1 ; i++ )
{


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

printf("\b \n");
}

Monday, December 27, 2010

find the smallest from array

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and finding the smallest from them
*/

#include<stdio.h>
void main()
{


int
n = 0 , i =0 , j = 0;

int
no[100];
int
smallest;
printf("How many numbers you want to scan ? ");

scanf("%d",&n);
if
(n < 0 || n>100)
{


// if the user enters invalid no of elemtns then we come here
printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// assign biggest the first element's value
biggest = no[0];
// the logic for finding the smallest number
for(i = 0 ; i < n ;i++)
{

if
(no[i] < smallest)
{


smallest = no[i];
}
}


printf("The smallest number among those numbers is: %d" , smallest);

}


printf("\n");
}

deleting an element from array using index

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and deleting an element from that
* using index
*/

#include<stdio.h>
void main()
{


int
n = 0 , i =0 ;

int
no[100];
char
c; // to ask the user whether he wants to continue or noe
int delindex= -1 ; // to store the index no at which the user wants to delete

printf("How many numbers you want to scan ? ");
scanf("%d",&n);
if
(n < 0 || n>100)
{


// if the user enters invalid no of elemtns then we come here
printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}


c = 'y';
do

{

printf("\nEnter the index no from which you want to delete: ");

scanf("%d",&delindex);

// for error checking
if(delindex < 0 || delindex >= n)
{


printf("Invalid number");
}

else

{

// we come here if the index is in valid range



// the logic for moving the elements
for(i = delindex ; i < n ;i++)
{

// move the elements by one

no[i] = no [i+1];
}


// n-- because one element is delted from it

n--;


printf("\nAfter delting the element at %d the array is:\n", delindex ) ;
// now print the array after deleting the number
for(i = 0 ; i < n ;i++)
{


printf("\nElement at index %d is : %d", i, no[i]);
}



// ask the user whether he/she wants to continue or not
printf("\nDO you want to continue ? (y/n) : ");

fflush(stdin);
scanf("%c",&c);
}
}

while
(c!='n' && c != 'N' );
}


printf("\n");
}

finding the biggest number from array

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and finding the biggest from them
*/

#include<stdio.h>
void main()
{


int
n = 0 , i =0 , j = 0;

int
no[100];
int
biggest; // for storing the biggest number
printf("How many numbers you want to scan ? ");

scanf("%d",&n);
if
(n < 0 || n>100)
{


// if the user enters invalid no of elemtns then we come here
printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// assign biggest the first element's value
biggest = no[0];
// the logic for finding the biggest number
for(i = 0 ; i < n ;i++)
{

if
(no[i] > biggest)
{


biggest = no[i];
}
}


printf("The biggest number among those numbers is: %d" , biggest);

}


printf("\n");
}

sorting array in ascending order

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and sorting them in ascending order
*/

#include<stdio.h>
void main()
{


int
n = 0 , i =0 , j = 0;

int
no[100];
int
temp; // for swapping the number
printf("How many numbers you want to scan ? ");

scanf("%d",&n);
if
(n < 0 || n>100)
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// the logic for sorting the number
for(i = 0 ; i < n ;i++)
{

for
(j = 0 ; j < n ; j++ )
{


if
(no[i] < no[j])
{

// if the number at index i is greater than the number at index j then swap the numbers
temp = no[i];

no[i] = no[j];
no[j] = temp;
}
}
}


// logic for displaying the sorted array
for(i = 0 ; i < n ;i++)
{

printf("\nnumber at index %d is %d ", i , no[i]);
}
}


printf("\n");
}