Monday, May 28, 2012

Pointers in C

A pointer is the address of a variable.For understanding pointers let us consider the following declarations:-

There are only two operators in C regarding pointers.
& is the addressing operator, and when applied to a variable gives it's address.

* is the value or indirection operator. When applied to an address it gives the value at the address. 

1) 
int x=10;
int *p=&x;
int **q=&p;

x is a variable of type int. It's value is 10. Let us assume it's address is 1000.

int *p; *p is an int, p is the address of an int.The value of p is &x i,e address of x which is 1000.

int **q; **q is an int, *q is a pointer to int, q is a pointer to pointer to int.




x is thus equivalent to *p and both are 10.
&x is equivalent to p and is 1000


What is &*p?
replace *p by x, we get &x which is p .
Therefore we can say that &* cancel out.

The same is true for *&x.


Consider this program :-
*******************************ptr.c**************************
#include<stdio.h>
#include<conio.h>
void main()
{
int x=10;
int *p=&x;
clrscr();
printf("%u,%d\n",&x,x);
printf("%u,%d\n",p,*p);
getch();
}



The output:-


64948,10
64948,10

The equivalence of Arrays & Pointers.

The following program should be self explanatory:-


#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={1,2,3,4,5},n=5,i;
int *p=a;
clrscr();
for(i=0;i<=n-1;i++)
printf("%d,",*(p+i));
printf("\n");
for(p=a;p<=&a[n-1];p++)
printf("%d,",*p);
printf("\n");
for(p=a;p<=&a[n-1];)
printf("%d,",*p++);
printf("\n");
//This reads the array using array syntax
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
for(p=a;p<=&a[n-1];)
printf("%d,",*p++);
printf("\n");
//Since &a[i] translates to p+i, therefore we have
for(p=a;p<=&a[n-1];)
scanf("%d",p++);
printf("\n");

for(p=a;p<=&a[n-1];)
printf("%d,",*p++);
printf("\n");
getch();
}
The output:-







Parameter passing using pointers

#include<stdio.h>
#include<conio.h>
void valueswap (int a,int b);
void ptrswap(int* a,int* b);
void main()
{
int x=5,y=10;
clrscr();
printf("Value before value swap a = %d, b = %d\n",x,y);
valueswap(x,y);
printf("Value after value swap a = %d, b = %d\n",x,y);
printf("Value before ptr swap a = %d, b = %d\n",x,y);
ptrswap(&x,&y);
printf("Value after ptr swap a = %d, b = %d\n",x,y);
getch();
}
void valueswap (int a,int b)
{
int t=a;
a=b;
b=t;
}
void ptrswap(int* a,int* b)
{
int t=*a;
*a=*b;
*b=t;
}





The output:-

To understand this assume the following.

Variable    |    Value    |    Address
x                    5               1000
y                    10             1002



for valueswap

void valueswap (int a,int b)
{
int t=a;
a=b;
b=t;
}


after parameter copy
a=5
b=10

int t=a; t = 5
a=b; a=10;
b=t; b=5

The values of x and y are not changed. Therefore the output in main remains unaffected .




For ptrswap


void ptrswap(int* a,int* b)
{
int t=*a;
*a=*b;
*b=t;
}


It is called as ptrswap(&x,&y);=ptrswap(1000,1002);
Therefore after  parameter passing a=1000, and b=1002


int t=*a which translates as t=*1000 which means value at address 1000 which is the value of x ans is 5.
so t=5;


*a=*b, which translates as *1000 = * 1002. Replace value at address 1000 with value at address 1002.
This means that x becomes 10.


*b=t; which translates as *1002=5. Therefore value at address 1002, i,e y becomes 5.


Therefore , x and y are swapped.





Saturday, May 19, 2012

Implementing 1's Complement & 2's Complement in C

1's complement and 2's complement arithmetic is used in Computer Science for implementing the basic operations of addition and subtraction. In this program , I have endeavored to provide a simple menu based example in C of these basic operations. 

To implement these operations I have developed the following functions:-

void printMenu()
{
clrscr();
gotoxy(5,5);
printf("Read\tPrint\t1'sComplement\t2'sComplement\tAdd\tSub\teXit\n");
printf("A = ");
printNumber(a);
printf("  B = ");
printNumber(b);
printf("  C = ");
printNumber(c);
printf("\n");
}
This is the Menu. It works by pressing R,P,1,2,A,S,X
The menu is not case sensitive.

void copyArray(int dest[],int src[])
{
 int i;
 for(i=0;i<=8;i++)
 dest[i]=src[i];
}
copies the src array into dest.


int getCarry(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(0);
 case 2:return (1);
 default:return(1);
}
}
adds 3 bits and returns the carry.



int getSum(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(1);
 case 2:return (0);
 default:return(1);
}
}
Adds three bits and returns the sum


void TwosComplement(int x[])
{
int temp[]={1,0,0,0,0,0,0,0,0};
int out[9];
copyArray(out,x);
OnesComplement(out);
addNumber(out,temp,x);
}
finds the 2's complement of a number and stores it in the same array.


void OnesComplement(int x[])
{
int i;
for(i=8;i>=0;i--)
x[i]=1-x[i];
}
finds the 1's complement of a number.

void printNumber(int x[])
{
int i;
for(i=8;i>=0;i--)
printf("%d",x[i]);
}
prints the number

void readNumber(int x[])
{
int i;
char str[10];
scanf("%s",str);
for(i=8;i>=0;i--)
x[i]=(str[8-i]-'0'); //0,1,2,3,4 '0'-'0'=0,'1'-'0'=1
 }

reads a number as a string of 9 characters and stores it in a int array.

void subNumber(int x[],int y[],int sub[])
{
int temp[9];
copyArray(temp,y);
TwosComplement(temp);
addNumber(x,temp,sub);
}
subtracts 2 numbers and stores it in the 3rd parameter

Important

Each array is 9 elements, 8 for the byte and the ninth for the sine bit.
Therefore values must be entered as such

Here are some snapshots:-









The Complete Program

Binary.c

#include<stdio.h>                                                  
#include<conio.h>
#include<math.h>
int a[9]={0,0,0,0,0,0,0,0,0},b[9]={0,0,0,0,0,0,0,0,0},c[9]={0,0,0,0,0,0,0,0,0};
void readNumber(int x[]);
void printNumber(int x[]);
void OnesComplement(int x[]);
void addNumber(int x[],int y[],int sum[]);
void TwosComplement(int x[]);
int getSum(int a,int b,int c);
int getCarry(int a,int b,int c);
void copyArray(int dest[],int src[]);
void subNumber(int x[],int y[],int sub[]);
void printMenu();
void main()
{

int option,ch;
clrscr();
while(1)
{
printMenu();
option=getch();
switch(option)
{
case'X':
case'x':
return;
case 'r':
case 'R':
printf("\nEnter a or b: ");
ch=getche();
printf(" = ");
if(ch=='a' || ch=='A')
readNumber(a);
else
readNumber(b);
break;
case '1':
printf("\nEnter a or b: ");
ch=getche();
printf(" = )");
if(ch=='a' || ch=='A')
OnesComplement(a);
else
OnesComplement(b);
break;

case '2':
printf("\nEnter a or b: ");
ch=getche();
printf(" = ");
if(ch=='a' || ch=='A')
TwosComplement(a);
else
TwosComplement(b);
break;
case 'a':
case 'A':
addNumber(a,b,c);
break;
case 's':
case 'S':
subNumber(a,b,c);
break;
}
}
}
void printMenu()
{
clrscr();
gotoxy(5,5);
printf("Read\tPrint\t1'sComplement\t2'sComplement\tAdd\tSub\teXit\n");
printf("A = ");
printNumber(a);
printf("  B = ");
printNumber(b);
printf("  C = ");
printNumber(c);
printf("\n");
}
void copyArray(int dest[],int src[])
{
 int i;
 for(i=0;i<=8;i++)
 dest[i]=src[i];
}
void addNumber(int x[],int y[],int sum[])
{
 int i,s,carry=0;
 for(i=0;i<=8;i++)
{
   s=getSum(x[i] ,y[i],carry);
   carry=getCarry(x[i],y[i],carry);
   sum[i]=s;
}
}
int getCarry(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(0);
 case 2:return (1);
 default:return(1);
}
}
int getSum(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(1);
 case 2:return (0);
 default:return(1);
}
}
void TwosComplement(int x[])
{
int temp[]={1,0,0,0,0,0,0,0,0};
int out[9];
copyArray(out,x);
OnesComplement(out);
addNumber(out,temp,x);
}
void OnesComplement(int x[])
{
int i;
for(i=8;i>=0;i--)
x[i]=1-x[i];
}
void printNumber(int x[])
{
int i;
for(i=8;i>=0;i--)
printf("%d",x[i]);
}

void readNumber(int x[])
{
int i;
char str[10];
scanf("%s",str);
for(i=8;i>=0;i--)
x[i]=(str[8-i]-'0'); //0,1,2,3,4 '0'-'0'=0,'1'-'0'=1
 }
void subNumber(int x[],int y[],int sub[])
{
int temp[9];
copyArray(temp,y);
TwosComplement(temp);
addNumber(x,temp,sub);
}
Next, we shall use it to implement the Booth's algorithm

Thursday, May 10, 2012

Joins in MS SQL Server,Oracle & MY SQL

Joins are a way of connecting data from more than one table.We can also have joins on the same table by creating aliases. There are four basic joins 
1) Outer join
2) Left outer join
3) Right outer join
4) inner join.

In this post, we shall learn the syntax of joins on MS SQL Server, Oracle & MY SQL.

First, the tables:-

1) create table medicalcharlatans(CharlatanName varchar(100) primary key,CharlatanRank int,CharlatanDescrption varchar(200))

Then insert some data.

insert into medicalcharlatans values('Jesus Christ',1,'Used to treat diseases by driving out demons')
insert into medicalcharlatans values('Mother Teresa',2,'Cured cancer via Light Emitting Photo')
insert into medicalcharlatans values('Benny Hinn',3,'Cured diseases via Touch Therapy')


Here is a view of the table:-
Benny Hinn    3    Cured diseases via Touch Therapy
Jesus Christ    1    Used to treat diseases by driving out demons
Mother Teresa    2    Cured cancer via Light Emitting Photo




2) create table PedofileProtectors(ProtectorName varchar(100) primary key,ProtectorDescrption varchar(200))


insert into PedofileProtectors values('John Paul 2','Protected Pedofiles and promoted Pedofile protectors like Mother Teresa and Ratzinger')

insert into PedofileProtectors values('Ratzinger','Protected Pedofiles and promoted Pedofile protectors like Mother Teresa and John Paul 2')

insert into PedofileProtectors values('Mother Teresa','Protected Pedofiles and promoted Pedofile protectors like John Paul 2 and Ratzinger, wrote letters of support for recognized Pedofiles')

The table as it stands now :-


John Paul 2    Protected Pedofiles and promoted Pedofile protectors like Mother Teresa and Ratzinger

Mother Teresa    Protected Pedofiles and promoted Pedofile protectors like John Paul 2 and Ratzinger, wrote letters of support for recognized Pedofiles


Ratzinger    Protected Pedofiles and promoted Pedofile protectors like Mother Teresa and John Paul 2




The various Joins :-


Cross Join


The cross join is a cartesian product. Here is the output in MS SQL Server. Incidentally , it's the same in all three databases.


select * from medicalcharlatans cross join  PedofileProtectors
This is supported by all three databases









SELECT     CharlatanName,ProtectorName FROM         medicalcharlatans  full outer JOIN PedofileProtectors ON CharlatanName = ProtectorName
Not supported in my SQL. We shall learn in a subsequent post to simulate it using Union.

SELECT     CharlatanName,ProtectorName FROM         medicalcharlatans  right outer JOIN PedofileProtectors ON CharlatanName = ProtectorName
It's supported in Oracle.
In My SQL :- SELECT     CharlatanName,ProtectorName FROM         medicalcharlatans  right JOIN PedofileProtectors ON CharlatanName = ProtectorName

SELECT     CharlatanName,ProtectorName FROM         medicalcharlatans  left outer JOIN PedofileProtectors ON CharlatanName = ProtectorName


It's supported in Oracle.
In My SQL :- SELECT     CharlatanName,ProtectorName FROM         medicalcharlatans  left JOIN PedofileProtectors ON CharlatanName = ProtectorName



SELECT     CharlatanName,ProtectorName FROM         medicalcharlatans  inner JOIN PedofileProtectors ON CharlatanName = ProtectorName

 SELECT     CharlatanName,ProtectorName FROM         medicalcharlatans JOIN PedofileProtectors ON CharlatanName = ProtectorName
It's supported in all three databases.





In a subsequent post, we shall discuss unions, intersections, minus, and the top and rownum