Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Thursday, December 11, 2008

Drawing Single Cube

#include
#include
#include
#include "glut.h"
GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};
GLfloat normals[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};
GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},
{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},
{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};
void
polygon(int a, int b, int c , int d)
{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glNormal3fv(normals[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glNormal3fv(normals[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glNormal3fv(normals[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glNormal3fv(normals[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void
colorcube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}
static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;
void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glFlush();
}
void spinCube()
{
theta[axis] += 2.0;
if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
display();
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON & state == GLUT_DOWN) axis = 0;
if(btn==GLUT_MIDDLE_BUTTON & state == GLUT_DOWN) axis = 1;
if(btn==GLUT_RIGHT_BUTTON & state == GLUT_DOWN) axis = 2;
}
void
myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
void
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE GLUT_RGB GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("colorcube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}

calculates the user's pay

// This progam calculates the user's pay.
#include
using namespace std;
int main()
{
double hours, rate, pay;
// Get the number of hours worked.
cout << "How many hours did you work? ";
cin >> hours;

// Get the hourly pay rate.
cout << "How much do you get paid per hour? ";
cin >> rate;

// Calculate the pay.
pay = hours * rate;

// Display the pay.
cout << "You have earned $" << pay << endl;
return 0;
}

Tuesday, August 5, 2008

C source code for 2 Linked List Implementation Of Stack

#define TRUE 1

# define FALSE 0

typedef struct stack_element {

int stack_data;

struct stack_element *next;

}stack_inf;

typedef struct stack_element_top{

stack_inf *stack_top;

}stack;

stack *create()

{

stack *s;

s=(stack *)malloc(sizeof(stack));

s->stack_top=NULL;

return(s);

}

int isempty (stack *s)

{

if (s->stack_top == NULL)

return TRUE;

else

return FALSE;

}

void push (stack *s,int x)

{

stack_inf *p;

p=(stack_inf *)malloc(sizeof(stack_inf));

p->stack_data=x;

p->next=NULL;

if(isempty(s))

s->stack_top=p;

else

{

p->next=s->stack_top;

s->stack_top=p;

}

return;

}

int pop(stack *s)

{

stack_inf *p;

int x;

if(isempty(s))

printf(“\nStack is empty !\n”);

else

{

p=s->stack_top;

x=p->stack_data;

s->stack_top=p->next;

free(p);

return(x);

}

}

C source code for An array implementation of the operations of a stack ADT

#define MAX 1000 /* only 1000 elements can be stored in the stack*/

# define TRUE 1

# define FALSE 0

typedef int stack_data;

struct stack_inf{

int top;

stack_data stack_elements[MAX];

};

typedef struct stack_inf stack;

stack s;

void create(stack *s)

{

s->top=-1;

return;

}

int isempty (stack *s)

{

if (s->top == -1)

return(TRUE);

else

return(FALSE);

}

int isfull (stack *s)

{

if (s->top == (MAX -1))

return(TRUE);

else

return(FALSE);

}

stack_data pop (stack *s)

{

int t;

stack_data ret;

if (isempty(s))

printf(“The stack is empty\n”);

else

{

t = s->top;

ret=s->stack_elements[t];

s->top - -;

}

return(ret);

}

void push (stack *s, stack_data x)

{

if (isfull(s))

printf(“Stack is FULL ! No insertion possible\n”);

else

{

++s->top;

s->stack_elements[s->top]=x;

}

}

Sunday, August 3, 2008

C Source code for Implementation of queue using Linked Lists

#define TRUE 1

#define FALSE 0

struct queue_element{

int data;

struct queue_element *next;

};

typedef struct queue_element queue_info;

typedef struct qpointer{

queue_info *front, *rear;

}queue;

queue *q;

queue *create()

{

queue *p;

p=(queue *)malloc(sizeof(queue));

p->front=NULL;

p->rear=NULL;

return(p);

}

int isempty(queue *q)

{

if(q->front == NULL)

return(TRUE);

else

return(FALSE);

}

void insert_queue (queue *q,int item)

{

queue_info *new;

new=(queue_info *) malloc (sizeof (queue_info));

new->next = NULL;

new->data = item;

if (isempty(q))

{

q->front = new;

q->rear = new;

}

else

{

q->rear->next=new;

q->rear=new;

}

return;

}

int delete_queue(queue *q)

{

int item;

queue_info *del;

if (isempty(q))

{

printf(“Queue is empty ! \n”);

return(-1);

}

else

{

del=q->front;

q->front=q->front->next;

item=del->data;

free (del);

if(q->front == NULL)

q->rear=NULL;

return(item);

}

}

C Source code for Implementation of queue using Stacks

# include

#define MAX 5

#define TRUE 1

#define FALSE 0

#define ERROR -1

struct qinfo{

int front,rear;

int noele;

int qelement[MAX];

};

typedef struct qinfo queue;

void create(),insert_queue();

int isfull(),isfull(),delete_queue();

void create(queue *q)

{

q->front=-1;

q->rear=-1;

q->noele=0;

return;

}

int isempty(queue *q)

{

if(q->noele == 0)

return(TRUE);

else

return(FALSE);

}

int isfull(queue *q)

{

if(q->noele == MAX)

return(TRUE);

else

return(FALSE);

}

void insert_queue(queue *q,int item)

{

char stop;

if(isfull(q))

printf("Queue is full \n");

else

{

if(isempty(q))

{

create(q);

q->front++;

q->rear++;

}

else

if(q->rear < (MAX - 1) || q->rear < (q->front - 1))

q->rear++;

else

if(q->rear == (MAX - 1) && q->noele <>

q->rear=0;

q->qelement[q->rear]=item;

q->noele++;

}

return;

}

int delete_queue(queue *q)

{

int data;

if(isempty(q))

{

printf("Queue is empty!!!\n");

return(ERROR);

}

else

{

data=q->qelement[q->front];

if(q->front == (MAX - 1))

q->front=0;

else

q->front++;

q->noele--;

if(isempty(q))

create(q);

return(data);

}

}

Saturday, August 2, 2008

C Code for Removing duplicate nodes from linked list

void RemoveDuplicates ( NODE**head) {

NODE *pCurrent = *head;

if(pCurrent ==NULL)
return;
else {
while(pCurrent ->next !=NULL) {

if(pCurrent ->data == pCurrent ->next->data) {

NODE *nnext = pCurrent ->next->next;
free(pCurrent ->next);
pCurrent ->next = nnext;
}
else
pCurrent =pCurrent ->next;
}
}
}

C Code for Mirror Image Tree

void mirror(struct node* node) {

if (node==NULL) {

return;
}
else {

struct node* temp; // do the subtrees
mirror(node->left); mirror(node->right); // swap the pointers in this node
temp = node->left;
node->left = node->right;
node->right = temp;
}
}

Thursday, July 31, 2008

C Code for Heapsort algorithm

heapsort(x,n)

int x[],n;

{

int i,data,s,f,ivalue;

/* create initial heap */

for(i=1;i <>

{

data=x[i];

/* insert_heap (x,i,data); */

s=i;

f=(s - 1) / 2; /* f is the father of s */

while(s > 0 && x[f] <>

{

x[s]=x[f]; /* move up the tree */

s=f;

f=(s - 1) / 2; /* f is the father of s */

}/* end while */

x[s]=data;

}/* end for i.e. a heap has been created */

/* begin repeatedly removing the maximum element i.e. x[0], */

/* place it at the end of the array.*/

/* Convert the remaining part to a heap */

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

{

/* delete the root */

ivalue=x[i];

x[i]=x[0]; /* the root is placed at the bottom */

f=0;

/* begin adjusting the heap by pushing the element x[i] i.e. ivalue */

if(i == 1)

s=-1;/* no further processing required */

else

s=1;

if(i > 2 && x[2] > x[1])

s=2;

while(s >=0 && ivalue <>

{

/* push ivalue down the heap */

x[f]=x[s];

f=s;

/* s=largeson(f, i-1) */

s=2*f +1; /* s is the child of f */

if(s+1 <= i-1 && x[s] <>

s=s+1; /* select the larger of the two children of f i.e. 2j + 1 or 2j + 2 */

if(s > i-1) /* when bounds have been reached */

s=-1;

}/* end while */

/* insert ivalue at fth location */

x[f]=ivalue;

}/* end for */

}/* end heap sort */

C code for NONRECURSIVE IMPLEMENTATION OF QUICKSORT

# define STACKSIZE 50

void quicksort(int x[], int n)

{

int i,j;

struct stack_info{

int l;

int u;

}bounds;

struct stack_tag{

int stacktop;

struct stack_info limits[STACKSIZE];

}STACK;

STACK.stacktop=-1;

bounds.l=0;

bounds.u=n - 1;

push(&STACK,&bounds);

while(!empty(&STACK))

{

pop(&STACK,&bounds);

while(bounds.u > bounds.l)

{

partition(x,bounds.l,bounds.u,&j);

if((j - bounds.l) > (bounds.u - j))

{

i=bounds.u;

bounds.u=j - 1;

push(&STACK,&bounds);

bounds.l=j + 1;

bounds.u=i;

}

else

{

i=bounds.l;

bounds.l=j + 1;

push(&STACK,&bounds);

bounds.l=i;

bounds.u=j - 1;

} /* end if */

} /* end while */

}/* end while */

return;

} /* end quicksort */

int emtpy (struct stack_tag * stackptr)

{

if( stackptr->stack.top == -1)

return (1);

else

return (0);

} /* end empty*

void pop (struct stack_tag * stackptr, struct stack_info * data)

{

if empty (stackptr)

{

printf (“%s\n”, “stack underflow”);

}

data=&(stackptr-> limits [stackptr->stacktop]);

-- (stackptr->stacktop);

return;

}

void push (struct stack_tag * stackptr, struct stack_info *data)

{

int p;

p=++(stackptr->stacktop);

stackptr->limits[p].l = data->l;

stackptr->limits [p].u = data->u;

return;

} /*end push.; note no checking for stack overflow */

C code for A recursive implementation of the quicksort algorithm.


void quicksort(int x[], int l, int u)

{

int * j; int k;

if (l > =u)

return;

else

{

partition (x, l,u,&j);

quicksort (x, l, j -1);

quicksort (x, j+1, u);

return;

}

}

void partition (int x [], int l, int u,int *j)

{

int k, m,n, i, temp, int * j;

k = x [l]; /* k is the element whose final */

n = u; /* position is required */

m =l;

while (m <>

{

while (x[m] <= k && m <>

m++ ; /* move the lower index up */

while (x[n] > k)

n --; /* reduce the upper index */

if (m

{

/*interchange x[m] & x[n] */

temp=x[m];

x[m]=x[n];

x[n]=temp;

} /* end if */

} * end while */

x[l]= x[n];

x[n]=k;

* j = n;

return;

}

Your Title