Friday, July 28, 2006

C FAQ - III

FAQ - III

Deleting a node from single linked list without knowing previous pointer :

You need to make a copy of the next node on the current node and then free the next node ;)


Detecting loops in linked list:

Use two pointers one that moves node by node and the other moves two nodes at a time, if these
pointers meet then a loop is detected.

Thursday, July 27, 2006

C FAQ - II

C FAQ – II

Bitwise operations

#include<stdio.h>

unsigned int swap( unsigned int p, int xp, int xy);
unsigned int setbit (unsigned int p, int xp);
unsigned int resetbit (unsigned int p, int xp);
unsigned int countbits (unsigned int p);

int main()
{
printf ("The values after swapping of 0xf001 is %x\n", swap (0xf001, 1, 15));
printf ("The values after setting of 0xf001 is %x\n", setbit (0xf001, 4));
printf ("The values after resetting of 0xf001 is %x\n", resetbit (0xf001, 4));
printf ("The values after counting of 0xf001 is %x\n", countbits (0xf001));
}

unsigned int swap( unsigned int p, int xp, int yp)
{
unsigned int x = p & (1 << xp);
unsigned int y = p & (1 << yp);

p = p ^ x ^ y; /* Reset those bits to zero */
if (yp > xp)
{
y = y >> (yp -xp);
x = x << (yp - xp);
y = x | y;
}
else
{
y = y >> (xp -yp);
x = x << (xp - yp);
y = x | y;
}
return (p | y);
}

unsigned int setbit (unsigned int p, int xp)
{
xp = 1 << xp;
return (p | xp);
}

unsigned int resetbit (unsigned int p, int xp)
{
xp = 1<< xp;
return (p & ~xp);
}

unsigned int countbits (unsigned int p)
{
int cnt=0;
while (p)
{
if (p & 1)
cnt++;
p = p>>1;
}
return cnt;
}

Output:

The values after swapping of 0xf001 is 7003
The values after setting of 0xf001 is f011
The values after resetting of 0xf001 is f001
The values after counting of 0xf001 is 5


FIFO Implementation

#include<stdio.h>

#define MAX 30

int FIFO[MAX];
int wptr,rptr;
int bcross;
int readfifo (int data);
int writefifo (int data);

int main()
{
int i;
int p;
for (i=0;i<MAX+3;i++)
writefifo (i);
for (i=0;i<MAX+3;i++)
printf ("data read from the fifo is %d\n", readfifo (i));
for (i=0;i<5;i++)
writefifo (i);
for (i=0;i<6;i++)
printf ("data read from the fifo is %d\n", readfifo (i));

}

int writefifo (int data)
{
if (wptr == rptr && (bcross == 1))
{
printf("Fifo Overflow detected \n");
return -1;
}
else if (wptr >= rptr && (bcross == 0))
{
FIFO[wptr++] = data;
}
else if (wptr < rptr && (bcross == 1))
{
FIFO[wptr++] = data;
}
else {
printf("FIFO corruption detected \n");
}
if (wptr == MAX)
{
wptr=0;
bcross=1;
}
return wptr;
}

int readfifo (int data)
{
if (wptr == rptr && (bcross == 0))
{
printf("Fifo Underrun detected \n");
return -1;
}
else if (rptr > wptr && (bcross == 1))
{
data= FIFO[rptr++];
}
else if (rptr <= wptr && (bcross == 0))
{
data=FIFO[rptr++];
}
else if (rptr == wptr && bcross == 1) {
data=FIFO[rptr++];
}
else {
printf( "Read corrupt \n");
printf("FIFO corruption detected \n");
}
if (rptr == MAX)
{
rptr=0;
bcross=0;
}
return data;
}

Output:
Fifo Overflow detected
Fifo Overflow detected
Fifo Overflow detected
data read from the fifo is 0
data read from the fifo is 1
data read from the fifo is 2
data read from the fifo is 3
data read from the fifo is 4
data read from the fifo is 5
data read from the fifo is 6
data read from the fifo is 7
data read from the fifo is 8
data read from the fifo is 9
data read from the fifo is 10
data read from the fifo is 11
data read from the fifo is 12
data read from the fifo is 13
data read from the fifo is 14
data read from the fifo is 15
data read from the fifo is 16
data read from the fifo is 17
data read from the fifo is 18
data read from the fifo is 19
data read from the fifo is 20
data read from the fifo is 21
data read from the fifo is 22
data read from the fifo is 23
data read from the fifo is 24
data read from the fifo is 25
data read from the fifo is 26
data read from the fifo is 27
data read from the fifo is 28
data read from the fifo is 29
Fifo Underrun detected
data read from the fifo is -1
Fifo Underrun detected
data read from the fifo is -1
Fifo Underrun detected
data read from the fifo is -1
data read from the fifo is 0
data read from the fifo is 1
data read from the fifo is 2
data read from the fifo is 3
data read from the fifo is 4
Fifo Underrun detected
data read from the fifo is -1

C++ basic programs

C++ - basics through simple programs

1) Program to illustrate memory allocation for objects

This is a program to illustrate, member functions [non virtual ] member functions are allocated in the code segment and not relative to the object.

#include <iostream.h>
using namespace std;

class corrupt{
int data;
public:
corrupt()
{
data=0;
}
void print()
{
cout<<"Print Function called \n";
// cout<<data;
}
};

int main ()
{
corrupt *p;
p =new corrupt;
p =NULL;
p->print();
}

Uncommenting the bold line would lead to segmentation violation, otherwise the program works fine though we have made object p to NULL. When the line in bold is uncommented obviously the function tries to dereference the this pointer and thus leads to segmentation fault.

Output:
Print Function called

2) Program to illustrate copy constructors

#include <iostream.h>
#include <string>
using namespace std;


class car {

friend ostream & operator<<(ostream &os, car &c);

int wheels;
string model;
int price;
public:
car(int w, char *m, int p)
{
wheels=w;
model=m;
price=p;
}
car (car &c)
{
cout<< "Copy constructor called here\n";
model= "copy of " + c.model;
wheels= c.wheels;
price= c.price;
}
};

ostream & operator<<(ostream &os, car &c)
{
os <<"Car model name :"<<c.model<<endl;
os <<"Car price :"<<c.price<<endl;
os <<"Car wheels :"<<c.wheels<<endl;
return os;
}

void function (car c1, car c2)
{
cout<<c1;
cout<<c2;
}

int main()
{
car c1(4,"VW passat",14000);
car c2(4,"Toyota Yaaris",14000);
cout<<c1<<c2;
function (c1,c2);
}

Output:
Car model name :VW passat
Car price :14000
Car wheels :4
Car model name :Toyota Yaaris
Car price :14000
Car wheels :4
Copy constructor called here
Copy constructor called here
Car model name :copy of VW passat
Car price :14000
Car wheels :4
Car model name :copy of Toyota Yaaris
Car price :14000
Car wheels :4

3) Operator overloading

#include <iostream.h>

class vehicle {
int manucost;
int regn;
int tax;
public:
vehicle (int a, int b, int c)
{
manucost=a;
regn=b;
tax=c;
}
int operator+( vehicle &v1)
{
return (manucost + regn + tax + v1.manucost + v1.regn + v1.tax );
}
};

int main()
{
vehicle v1(10,1,1);
vehicle v2(20,2,2);
int tc= v1+v2;
cout<< "Total cost of the vehicle is :"<<tc<<endl;
}

Output:
Total cost of the vehicle is :36

4) Templates

#include <iostream.h>
#include <string>
using namespace std;

template <class T> class array {
int currIndex;
int MaxElem;
int Size;
class T *p;
public:
array (int c, int m, int s)
{
currIndex=c;
MaxElem=m;
Size=s;
cout << "Constructor template \n";
p = new T [s];
}
arrayprint()
{
for (int i=0;i<=Size;i++)
{
cout<<p[i];
}
}
};

class myClass{
friend ostream & operator << (ostream &, myClass &);
string *p;
public:
myClass()
{
p = new string ("Hello World\n");
cout<<*p;
}
};

ostream& operator << (ostream &os, myClass &my){
os << my.p;
return os;
}

int main( int argc, char * argv[])
{
array<myClass> p(4,5,7);
return 1;
}

Output:

Constructor template
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

5) Using STL containers

#include <iostream.h>
#include <vector>
using namespace std;


class Names{
friend ostream & operator << (ostream &, Names &);
int number;
public:
Names(int a)
{
number=a;
}
int display()
{
return number;
}
};

vector <Names>v;

ostream & operator << (ostream &os, Names &name){
os<<name.number;
return os;
}

int main ()
{
// append elements with values 1 to 6
for (int i=1; i<=6; ++i) {
v.push_back(i);
}

for (vector<Names>::iterator it=v.begin(); it!=v.end(); ++it) {
cout<<"Object value :"<<*it<< endl;
}

}

Output:

Object value :1
Object value :2
Object value :3
Object value :4
Object value :5
Object value :6

Tuesday, July 25, 2006

About me: Typical software engineer working from US. I like to visit places and spend time with friends. Posted by Picasa