Sunday, June 18, 2017
Monday, June 23, 2014
Useful Links
C
1> GCC Compilation Options :
http://www.thegeekstuff.com/2012/10/gcc-compiler-options/
2> 50 Linux Most Used Commands :
http://www.thegeekstuff.com/2010/11/50-linux-commands/
3>Compilation Steps :
http://www.thegeekstuff.com/2011/10/c-program-to-an-executable/
4>Make Utility :
http://www.thegeekstuff.com/2010/08/make-utility/
5>Memory Management ( Pointers) :
http://staff.um.edu.mt/csta1/courses/lectures/csa2060/c8a.html
6>Memory management :
http://shareprogrammingtips.com/c-language-programming-tips/structure-of-a-c-program-in-memory-how-heapstackdata-and-code-segments-are-stored-in-memory/
http://www.geeksforgeeks.org/memory-layout-of-c-program/
7> Dynamic Memory Allocation :
http://fresh2refresh.com/c/c-dynamic-memory-allocation/
7> Debugging in GCC -GDB- :
http://www.thegeekstuff.com/2010/03/debug-c-program-using-gdb/
1> GCC Compilation Options :
http://www.thegeekstuff.com/2012/10/gcc-compiler-options/
2> 50 Linux Most Used Commands :
http://www.thegeekstuff.com/2010/11/50-linux-commands/
3>Compilation Steps :
http://www.thegeekstuff.com/2011/10/c-program-to-an-executable/
4>Make Utility :
http://www.thegeekstuff.com/2010/08/make-utility/
5>Memory Management ( Pointers) :
http://staff.um.edu.mt/csta1/courses/lectures/csa2060/c8a.html
6>Memory management :
http://shareprogrammingtips.com/c-language-programming-tips/structure-of-a-c-program-in-memory-how-heapstackdata-and-code-segments-are-stored-in-memory/
http://www.geeksforgeeks.org/memory-layout-of-c-program/
7> Dynamic Memory Allocation :
http://fresh2refresh.com/c/c-dynamic-memory-allocation/
7> Debugging in GCC -GDB- :
http://www.thegeekstuff.com/2010/03/debug-c-program-using-gdb/
Ping-Pong Game in C
Game : Ping-Pong Game
Language : C ( TURBO C Libraries used )
/* Start of Code */
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <bios.h>
#include <math.h>
#include <time.h>
#include <dos.h>
void game_status (int state);
int move_pad(int key);
void erase(int obj );
void ball_dynamix(void );
void play(void );
#define R_RIGHT 77 // Right_Arrow
#define R_LEFT 75 // Left_Arrow
#define R_UP 72 // Up_Arrow
#define R_DOWN 80 // Down_Arrow
#define QUIT 1 // (ESC)
#define L_RIGHT 32 //(D)
#define L_LEFT 30 //(A)
#define L_UP 17 //(W)
#define L_DOWN 31 //(S)
#define RESET 19 //(R)
#define INIT 0
#define UPDATE 10
#define X 3
#define Y 3
#define R_INIT_X_TOP 480
#define R_INIT_Y_TOP 215
#define R_INIT_X_BTM 482
#define R_INIT_Y_BTM 265
#define L_INIT_X_TOP 156
#define L_INIT_Y_TOP 215
#define L_INIT_X_BTM 158
#define L_INIT_Y_BTM 265
#define BALL_X_POS 319
#define BALL_Y_POS 240
#define SPEED_X 3
#define SPEED_Y 3
struct ball
{
int x_new;
int x_old;
int x_speed;
int y_new;
int y_old;
int y_speed;
}b;
struct pad
{
int cur_x_top;
int cur_y_top;
int cur_x_btm;
int cur_y_btm;
}r,l;
int dirn,l_score=0,r_score=0;
char ballpos[20],r_skor[10],l_skor[10];
void main()
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
int mid_x = getmaxx()/2,mid_y = getmaxy()/2; // mid_x = 239.5 , mid_y = 319.5
printf("X = %d, Y = %d",mid_x,mid_y);
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
game_status(INIT);
play();
getch();
closegraph();
}
void game_status(int state)
{
srand(time(NULL)); // Seed ONCE
dirn = rand()%10;
if (state == INIT)
{
/* Draw the Net */
setfillstyle(XHATCH_FILL, BLUE);
bar(317,30,320,449);
/* Draw the Pitch Borders */
setcolor(WHITE);
rectangle(20,30,619,449);
/* Display Instructions & Other Texts */
outtextxy (25,20,"Press ESC to exit game");
outtextxy (25,452,"Player-1 :");
outtextxy (515,452,"Player-2 :");
/* Create Ball */
setfillstyle(SOLID_FILL, BLUE);
fillellipse(BALL_X_POS,BALL_Y_POS,3,3);
b.x_new = BALL_X_POS;
b.y_new = BALL_Y_POS;
b.x_old = BALL_X_POS;
b.y_old = BALL_Y_POS;
b.x_speed = SPEED_X;
b.y_speed = SPEED_Y;
if ( dirn == 2)
{
b.y_speed = 0;
printf("IN-2");
}
if (dirn == 7)
{
b.y_speed = 0;
printf("IN-7");
}
/* Draws Right & Left Pad*/
setfillstyle(SOLID_FILL, GREEN);
bar(R_INIT_X_TOP,R_INIT_Y_TOP,R_INIT_X_BTM,R_INIT_Y_BTM);
bar(L_INIT_X_TOP,L_INIT_Y_TOP,L_INIT_X_BTM,L_INIT_Y_BTM);
/* Initialise The Pads */
r.cur_x_top = R_INIT_X_TOP;
r.cur_y_top = R_INIT_Y_TOP;
r.cur_x_btm = R_INIT_X_BTM;
r.cur_y_btm = R_INIT_Y_BTM;
l.cur_x_top = L_INIT_X_TOP;
l.cur_y_top = L_INIT_Y_TOP;
l.cur_x_btm = L_INIT_X_BTM;
l.cur_y_btm = L_INIT_Y_BTM;
}
if ( state == RESET )
{
erase(0);
erase(1);
erase(2);
game_status(INIT);
}
if ( state == QUIT )
{
exit(0);
}
}
int move_pad(int key)
{
switch(key)
{
case R_LEFT:
if (r.cur_x_top > 325)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top-X,r.cur_y_top,r.cur_x_btm-X,r.cur_y_btm);
r.cur_x_top -= X;
r.cur_x_btm -= X;
}
break;
case R_RIGHT:
if (r.cur_x_btm > 315 && r.cur_x_btm <610)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top+X,r.cur_y_top,r.cur_x_btm+X,r.cur_y_btm);
r.cur_x_top += X;
r.cur_x_btm += X;
}
break;
case R_UP:
if (r.cur_y_top > 35)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top,r.cur_y_top-Y,r.cur_x_btm,r.cur_y_btm-Y);
r.cur_y_top -= Y;
r.cur_y_btm -= Y;
}
break;
case R_DOWN:
if (r.cur_y_btm < 445)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top,r.cur_y_top+Y,r.cur_x_btm,r.cur_y_btm+Y);
r.cur_y_top += Y;
r.cur_y_btm += Y;
}
break;
case L_LEFT:
if (l.cur_x_top > 23)
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top-X,l.cur_y_top,l.cur_x_btm-X,l.cur_y_btm);
l.cur_x_top -= X;
l.cur_x_btm -= X;
}
break;
case L_RIGHT:
if (l.cur_x_btm > 20 && l.cur_x_btm < 310 )
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top+X,l.cur_y_top,l.cur_x_btm+X,l.cur_y_btm);
l.cur_x_top += X;
l.cur_x_btm += X;
}
break;
case L_UP:
if (l.cur_y_top > 30)
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top,l.cur_y_top-Y,l.cur_x_btm,l.cur_y_btm-Y);
l.cur_y_top -= Y;
l.cur_y_btm -= Y;
}
break;
case L_DOWN:
if ( l.cur_y_btm < 445)
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top,l.cur_y_top+Y,l.cur_x_btm,l.cur_y_btm+Y);
l.cur_y_top += Y;
l.cur_y_btm += Y;
}
break;
}
return 1;
//printf("R:%d:%d:%d:%d\n",r.cur_x_top,r.cur_y_top,r.cur_x_btm,r.cur_y_btm);
}
void erase(int obj)
{
if ( obj== 0) // Erase Ball
{
b.x_old = b.x_new;
b.y_old = b.y_new;
setcolor (BLACK);
setfillstyle(SOLID_FILL, BLACK);
fillellipse(b.x_new,b.y_new,3,3);
}
if ( obj== 1) // Erase Right Paddle
{
setfillstyle(SOLID_FILL, BLACK);
bar(r.cur_x_top,r.cur_y_top,r.cur_x_btm,r.cur_y_btm);
}
if ( obj== 2) // Erase Left Paddle
{
setfillstyle(SOLID_FILL, BLACK);
bar(l.cur_x_top,l.cur_y_top,l.cur_x_btm,l.cur_y_btm);
}
}
void play(void )
{
int move;
while (1)
{
delay (20); // Reduce game speed to human playable level
if (inportb (0X60) == R_RIGHT) move_pad (R_RIGHT);
if (inportb (0X60) == R_LEFT) move_pad (R_LEFT);
if (inportb (0X60) == R_UP) move_pad (R_UP);
if (inportb (0X60) == R_DOWN) move_pad (R_DOWN);
if (inportb (0X60) == L_RIGHT) move_pad (L_RIGHT);
if (inportb (0X60) == L_LEFT) move_pad (L_LEFT);
if (inportb (0X60) == L_UP) move_pad (L_UP);
if (inportb (0X60) == L_DOWN) move_pad (L_DOWN);
if (inportb (0X60) == RESET) game_status(RESET);
if (inportb (0X60) == QUIT) game_status(QUIT);
ball_dynamix();
setfillstyle(XHATCH_FILL, BLUE);
bar(317,30,320,449);
}
}
void ball_dynamix()
{
erase(0);
switch (dirn)
{
case 0: case 1: // RIGHT-UP
b.x_new = b.x_new + b.x_speed;
b.y_new = b.y_new - b.y_speed;
break;
case 2: // RIGHT
b.x_new = b.x_new + b.x_speed;
b.y_new = b.y_new + b.y_speed;
break;
case 3: case 4: // RIGHT-DOWN
b.x_new = b.x_new + b.x_speed;
b.y_new = b.y_new + b.y_speed;
break;
case 5: case 6: // LEFT-UP
b.x_new = b.x_new - b.x_speed;
b.y_new = b.y_new - b.y_speed;
break;
case 7: // LEFT
b.x_new = b.x_new - b.x_speed;
b.y_new = b.y_new + b.y_speed;
break;
case 8: case 9: // LEFT-DOWN
b.x_new = b.x_new - b.x_speed;
b.y_new = b.y_new - b.y_speed;
break;
}
setfillstyle (SOLID_FILL,GREEN); // Display New postion.
fillellipse ( b.x_new, b.y_new , 3,3);
if ( b.y_new - 3 < 35 ) b.y_speed = -b.y_speed; // Reflect From Top
if ( b.y_new + 3 > 445 ) b.y_speed = -b.y_speed; // Reflect From Bottom
if ( b.x_new - 3 < 25 ) // Reflect From Left
{
b.x_speed = -b.x_speed;
l_score++;
}
if ( b.x_new + 3 > 615 ) // Reflect From Right
{
b.x_speed = -b.x_speed;
r_score++;
}
// Deflect Left UP
if ((b.y_new >= l.cur_y_top && b.y_new <= l.cur_y_btm-35 ) && (b.x_new >= l.cur_x_top - 6 && b.x_new <= l.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
b.y_speed = -2;
}
// Deflect Left Straight
if ((b.y_new >= l.cur_y_top+15 && b.y_new <= l.cur_y_btm-15 ) && (b.x_new >= l.cur_x_top - 6 && b.x_new <= l.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
}
// Deflect Left DOWN
if ((b.y_new >= l.cur_y_top+35 && b.y_new <= l.cur_y_btm ) && (b.x_new >= l.cur_x_top - 6 && b.x_new <= l.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
b.y_speed = 2;
}
// Deflect Right UP
if ((b.y_new >= r.cur_y_top && b.y_new <= r.cur_y_btm-35 ) && (b.x_new >= r.cur_x_top - 6 && b.x_new <= r.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
b.y_speed = -2;
}
// Deflect Right Straight
if ((b.y_new >= r.cur_y_top+15 && b.y_new <= r.cur_y_btm-15 ) && (b.x_new >= r.cur_x_top - 6 && b.x_new <= r.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
}
// Deflect Right DOWN
if ((b.y_new >= r.cur_y_top+35 && b.y_new <= r.cur_y_btm ) && (b.x_new >= r.cur_x_top - 6 && b.x_new <= r.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
b.y_speed = 2;
}
}
/* End of Code */
ENJOY!!!!!
Language : C ( TURBO C Libraries used )
/* Start of Code */
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <bios.h>
#include <math.h>
#include <time.h>
#include <dos.h>
void game_status (int state);
int move_pad(int key);
void erase(int obj );
void ball_dynamix(void );
void play(void );
#define R_RIGHT 77 // Right_Arrow
#define R_LEFT 75 // Left_Arrow
#define R_UP 72 // Up_Arrow
#define R_DOWN 80 // Down_Arrow
#define QUIT 1 // (ESC)
#define L_RIGHT 32 //(D)
#define L_LEFT 30 //(A)
#define L_UP 17 //(W)
#define L_DOWN 31 //(S)
#define RESET 19 //(R)
#define INIT 0
#define UPDATE 10
#define X 3
#define Y 3
#define R_INIT_X_TOP 480
#define R_INIT_Y_TOP 215
#define R_INIT_X_BTM 482
#define R_INIT_Y_BTM 265
#define L_INIT_X_TOP 156
#define L_INIT_Y_TOP 215
#define L_INIT_X_BTM 158
#define L_INIT_Y_BTM 265
#define BALL_X_POS 319
#define BALL_Y_POS 240
#define SPEED_X 3
#define SPEED_Y 3
struct ball
{
int x_new;
int x_old;
int x_speed;
int y_new;
int y_old;
int y_speed;
}b;
struct pad
{
int cur_x_top;
int cur_y_top;
int cur_x_btm;
int cur_y_btm;
}r,l;
int dirn,l_score=0,r_score=0;
char ballpos[20],r_skor[10],l_skor[10];
void main()
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
int mid_x = getmaxx()/2,mid_y = getmaxy()/2; // mid_x = 239.5 , mid_y = 319.5
printf("X = %d, Y = %d",mid_x,mid_y);
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
game_status(INIT);
play();
getch();
closegraph();
}
void game_status(int state)
{
srand(time(NULL)); // Seed ONCE
dirn = rand()%10;
if (state == INIT)
{
/* Draw the Net */
setfillstyle(XHATCH_FILL, BLUE);
bar(317,30,320,449);
/* Draw the Pitch Borders */
setcolor(WHITE);
rectangle(20,30,619,449);
/* Display Instructions & Other Texts */
outtextxy (25,20,"Press ESC to exit game");
outtextxy (25,452,"Player-1 :");
outtextxy (515,452,"Player-2 :");
/* Create Ball */
setfillstyle(SOLID_FILL, BLUE);
fillellipse(BALL_X_POS,BALL_Y_POS,3,3);
b.x_new = BALL_X_POS;
b.y_new = BALL_Y_POS;
b.x_old = BALL_X_POS;
b.y_old = BALL_Y_POS;
b.x_speed = SPEED_X;
b.y_speed = SPEED_Y;
if ( dirn == 2)
{
b.y_speed = 0;
printf("IN-2");
}
if (dirn == 7)
{
b.y_speed = 0;
printf("IN-7");
}
/* Draws Right & Left Pad*/
setfillstyle(SOLID_FILL, GREEN);
bar(R_INIT_X_TOP,R_INIT_Y_TOP,R_INIT_X_BTM,R_INIT_Y_BTM);
bar(L_INIT_X_TOP,L_INIT_Y_TOP,L_INIT_X_BTM,L_INIT_Y_BTM);
/* Initialise The Pads */
r.cur_x_top = R_INIT_X_TOP;
r.cur_y_top = R_INIT_Y_TOP;
r.cur_x_btm = R_INIT_X_BTM;
r.cur_y_btm = R_INIT_Y_BTM;
l.cur_x_top = L_INIT_X_TOP;
l.cur_y_top = L_INIT_Y_TOP;
l.cur_x_btm = L_INIT_X_BTM;
l.cur_y_btm = L_INIT_Y_BTM;
}
if ( state == RESET )
{
erase(0);
erase(1);
erase(2);
game_status(INIT);
}
if ( state == QUIT )
{
exit(0);
}
}
int move_pad(int key)
{
switch(key)
{
case R_LEFT:
if (r.cur_x_top > 325)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top-X,r.cur_y_top,r.cur_x_btm-X,r.cur_y_btm);
r.cur_x_top -= X;
r.cur_x_btm -= X;
}
break;
case R_RIGHT:
if (r.cur_x_btm > 315 && r.cur_x_btm <610)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top+X,r.cur_y_top,r.cur_x_btm+X,r.cur_y_btm);
r.cur_x_top += X;
r.cur_x_btm += X;
}
break;
case R_UP:
if (r.cur_y_top > 35)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top,r.cur_y_top-Y,r.cur_x_btm,r.cur_y_btm-Y);
r.cur_y_top -= Y;
r.cur_y_btm -= Y;
}
break;
case R_DOWN:
if (r.cur_y_btm < 445)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top,r.cur_y_top+Y,r.cur_x_btm,r.cur_y_btm+Y);
r.cur_y_top += Y;
r.cur_y_btm += Y;
}
break;
case L_LEFT:
if (l.cur_x_top > 23)
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top-X,l.cur_y_top,l.cur_x_btm-X,l.cur_y_btm);
l.cur_x_top -= X;
l.cur_x_btm -= X;
}
break;
case L_RIGHT:
if (l.cur_x_btm > 20 && l.cur_x_btm < 310 )
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top+X,l.cur_y_top,l.cur_x_btm+X,l.cur_y_btm);
l.cur_x_top += X;
l.cur_x_btm += X;
}
break;
case L_UP:
if (l.cur_y_top > 30)
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top,l.cur_y_top-Y,l.cur_x_btm,l.cur_y_btm-Y);
l.cur_y_top -= Y;
l.cur_y_btm -= Y;
}
break;
case L_DOWN:
if ( l.cur_y_btm < 445)
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top,l.cur_y_top+Y,l.cur_x_btm,l.cur_y_btm+Y);
l.cur_y_top += Y;
l.cur_y_btm += Y;
}
break;
}
return 1;
//printf("R:%d:%d:%d:%d\n",r.cur_x_top,r.cur_y_top,r.cur_x_btm,r.cur_y_btm);
}
void erase(int obj)
{
if ( obj== 0) // Erase Ball
{
b.x_old = b.x_new;
b.y_old = b.y_new;
setcolor (BLACK);
setfillstyle(SOLID_FILL, BLACK);
fillellipse(b.x_new,b.y_new,3,3);
}
if ( obj== 1) // Erase Right Paddle
{
setfillstyle(SOLID_FILL, BLACK);
bar(r.cur_x_top,r.cur_y_top,r.cur_x_btm,r.cur_y_btm);
}
if ( obj== 2) // Erase Left Paddle
{
setfillstyle(SOLID_FILL, BLACK);
bar(l.cur_x_top,l.cur_y_top,l.cur_x_btm,l.cur_y_btm);
}
}
void play(void )
{
int move;
while (1)
{
delay (20); // Reduce game speed to human playable level
if (inportb (0X60) == R_RIGHT) move_pad (R_RIGHT);
if (inportb (0X60) == R_LEFT) move_pad (R_LEFT);
if (inportb (0X60) == R_UP) move_pad (R_UP);
if (inportb (0X60) == R_DOWN) move_pad (R_DOWN);
if (inportb (0X60) == L_RIGHT) move_pad (L_RIGHT);
if (inportb (0X60) == L_LEFT) move_pad (L_LEFT);
if (inportb (0X60) == L_UP) move_pad (L_UP);
if (inportb (0X60) == L_DOWN) move_pad (L_DOWN);
if (inportb (0X60) == RESET) game_status(RESET);
if (inportb (0X60) == QUIT) game_status(QUIT);
ball_dynamix();
setfillstyle(XHATCH_FILL, BLUE);
bar(317,30,320,449);
}
}
void ball_dynamix()
{
erase(0);
switch (dirn)
{
case 0: case 1: // RIGHT-UP
b.x_new = b.x_new + b.x_speed;
b.y_new = b.y_new - b.y_speed;
break;
case 2: // RIGHT
b.x_new = b.x_new + b.x_speed;
b.y_new = b.y_new + b.y_speed;
break;
case 3: case 4: // RIGHT-DOWN
b.x_new = b.x_new + b.x_speed;
b.y_new = b.y_new + b.y_speed;
break;
case 5: case 6: // LEFT-UP
b.x_new = b.x_new - b.x_speed;
b.y_new = b.y_new - b.y_speed;
break;
case 7: // LEFT
b.x_new = b.x_new - b.x_speed;
b.y_new = b.y_new + b.y_speed;
break;
case 8: case 9: // LEFT-DOWN
b.x_new = b.x_new - b.x_speed;
b.y_new = b.y_new - b.y_speed;
break;
}
setfillstyle (SOLID_FILL,GREEN); // Display New postion.
fillellipse ( b.x_new, b.y_new , 3,3);
if ( b.y_new - 3 < 35 ) b.y_speed = -b.y_speed; // Reflect From Top
if ( b.y_new + 3 > 445 ) b.y_speed = -b.y_speed; // Reflect From Bottom
if ( b.x_new - 3 < 25 ) // Reflect From Left
{
b.x_speed = -b.x_speed;
l_score++;
}
if ( b.x_new + 3 > 615 ) // Reflect From Right
{
b.x_speed = -b.x_speed;
r_score++;
}
// Deflect Left UP
if ((b.y_new >= l.cur_y_top && b.y_new <= l.cur_y_btm-35 ) && (b.x_new >= l.cur_x_top - 6 && b.x_new <= l.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
b.y_speed = -2;
}
// Deflect Left Straight
if ((b.y_new >= l.cur_y_top+15 && b.y_new <= l.cur_y_btm-15 ) && (b.x_new >= l.cur_x_top - 6 && b.x_new <= l.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
}
// Deflect Left DOWN
if ((b.y_new >= l.cur_y_top+35 && b.y_new <= l.cur_y_btm ) && (b.x_new >= l.cur_x_top - 6 && b.x_new <= l.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
b.y_speed = 2;
}
// Deflect Right UP
if ((b.y_new >= r.cur_y_top && b.y_new <= r.cur_y_btm-35 ) && (b.x_new >= r.cur_x_top - 6 && b.x_new <= r.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
b.y_speed = -2;
}
// Deflect Right Straight
if ((b.y_new >= r.cur_y_top+15 && b.y_new <= r.cur_y_btm-15 ) && (b.x_new >= r.cur_x_top - 6 && b.x_new <= r.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
}
// Deflect Right DOWN
if ((b.y_new >= r.cur_y_top+35 && b.y_new <= r.cur_y_btm ) && (b.x_new >= r.cur_x_top - 6 && b.x_new <= r.cur_x_btm + 6))
{
b.x_speed = -b.x_speed;
b.y_speed = 2;
}
}
/* End of Code */
ENJOY!!!!!
Sunday, June 1, 2014
Matlab Useful Commands
| Commands | Purpose | |
| General | Ver | Gets Details of all the toolbox installed with version #'s |
| Version | MATLAB Version Running | |
| clc | Clear Command Window | |
| clear all | Clear complete workspace | |
| clear variable | Clears the variable specified | |
| Exit | Exits from matlab | |
| Quit | ||
| Ctrl + C | Stop the current execution ( in Command window ) | |
| lookfor | Search for similar commands | |
| Workspace | who | Shows variable names in the currently available workspace |
| whos | Shows variable names & Details in the currenty available workspace | |
| exist | Check for existence of the variable in the workspace | |
| save filename | Save as filename.mat to disk | |
| Load filename | Load filename.mat from disk | |
| Variables | Global | Declares variable as global |
| persistent | Defines a persistent variable ( static ) | |
| a = [ 1 , 2 ,3 ] | Create Row Matrix | |
| a = [ 1 2 3 ] | ||
| b = [ 1; 2; 3 ] | Creates Column Matrix | |
| c = [1 2 3 ; 4 5 6 ; 7 8 9] | Creates a 3*3 Matrix | |
| Directory / System | Dir | Lists contents of Current Directory |
| cd newdir | Change directory newdir | |
| cd .. | Goes to current directorie's parent directory | |
| mkdir newdir | creates a new directory newdir | |
| rmdir newdir | Deletes directory newdir | |
| rmdir ('newdir','s') | Deletes directory newdirand any folders inside recursively | |
| delete newfile | delete's the file newfile | |
| pwd | Displays the current working directory | |
| date | displays the current system date | |
| type newfile | Display the contents of newfile | |
| what | Lists all the matlab files in the current directory | |
| diary | Switches on/off the diary for recording contents of the command window | |
Wednesday, January 1, 2014
Bit Operations
Useful C Programs
1> Bit Operations
Set , Clear, Check bit.
#include<stdio.h>.
void showbit(int);
int setbit(int,int);
int clearbit(int,int);
int rtshift(int,int);
int lfshift(int,int);
int checkbit(int,int);
int dectohex(int);
int bintohex(int);
int main()
{
int a,b,value;
value =(10);
printf("%d:\n",value);
showbit(value);
a = rtshift(value ,2);
printf("%d:\n",a);
showbit(a);
b = lfshift(value ,2);
printf("%d:\n",b);
showbit(b);
checkbit(value,1)? printf("Bit Set"):printf("Bit Clear");
return 0;
}
void showbit(int a)
{
int i,d=0;
for(i=31;i>=0;i--)
{
if(!(a &(1<<i)))
{
printf("0");
d++;
}
else
{
printf("1");
d++;
}
if (d == 4)
{
printf(" ");
d=0;
}
}
printf("\n");
//Simpler way for above
//!(a &(1<<i))?printf("0"):printf("1");
}
int rtshift(int val,int rtshft)
{
return (val>>rtshft);
}
int lfshift(int val,int lfshft)
{
return (val<<lfshft);
}
int setbit(int val,int pos)
{
return (val|(1<<pos));
}
int clearbit(int val,int pos)
{
return (val&=~(1 << (pos-1)));
}
int checkbit(int val,int pos)
{
return (val & (1<<pos));
}
1> Bit Operations
Set , Clear, Check bit.
#include<stdio.h>.
void showbit(int);
int setbit(int,int);
int clearbit(int,int);
int rtshift(int,int);
int lfshift(int,int);
int checkbit(int,int);
int dectohex(int);
int bintohex(int);
int main()
{
int a,b,value;
value =(10);
printf("%d:\n",value);
showbit(value);
a = rtshift(value ,2);
printf("%d:\n",a);
showbit(a);
b = lfshift(value ,2);
printf("%d:\n",b);
showbit(b);
checkbit(value,1)? printf("Bit Set"):printf("Bit Clear");
return 0;
}
void showbit(int a)
{
int i,d=0;
for(i=31;i>=0;i--)
{
if(!(a &(1<<i)))
{
printf("0");
d++;
}
else
{
printf("1");
d++;
}
if (d == 4)
{
printf(" ");
d=0;
}
}
printf("\n");
//Simpler way for above
//!(a &(1<<i))?printf("0"):printf("1");
}
int rtshift(int val,int rtshft)
{
return (val>>rtshft);
}
int lfshift(int val,int lfshft)
{
return (val<<lfshft);
}
int setbit(int val,int pos)
{
return (val|(1<<pos));
}
int clearbit(int val,int pos)
{
return (val&=~(1 << (pos-1)));
}
int checkbit(int val,int pos)
{
return (val & (1<<pos));
}
Wednesday, January 16, 2013
C Reference Notes
Contents
1. Introduction to C..
2. C Fundamentals.
2.1. Program Structure.
2.2. Character Set
2.3. Identifiers
2.4. Keywords.
2.5. Constants.
2.6. Variables
2.7. Comments.
2.8. Expressions and Operators.
2.8.1. Arithmetic Operators.
2.8.2. Assignment Operators
2.8.3. Relational Operators.
2.8.4. Logical Operators.
2.8.5. Bitwise Operators.
2.8.6. Memory Access Operators.
2.8.7. Other Operators.
3. Data Types.
3.1. Predefined Data Types.
3.2. Derived Data Types.
3.2.1. Array
3.2.2. Structure
3.2.3. Union.
3.2.4. Enumerator
3.3. Range for Data Types.
4. Type Modifier
4.1. Signed.
4.2. Unsigned.
4.3. Long.
4.4. Short
5. C Memory Map..
5.1. Stack.
5.2. Heap.
5.3. Global Variables.
5.4. Program code.
6. Scope..
6.1. Function prototype scope.
6.2. Function scope.
6.3. Block scope.
6.4. File scope.
7. Storage Class Specifier.
7.1. Automatic.
7.2. Static.
7.3. Register
7.4. Extern.
8. Type Qualifier.
8.1. Const
8.2. Volatile.
9. Program Control Statements.
9.1. Decision/Selection Statements.
9.1.1. If Else.
9.1.2. If Else if ladder
9.2. Jump Statements.
9.2.1. Break;
9.2.2. Continue;
9.2.3. Exit()
9.2.4. Return.
9.2.5. Goto.
9.3. Looping/Iterative Statements.
9.3.1. For
9.3.2. While.
9.3.3. Do-While.
10. Functions.
10.1. Call by Reference.
10.2. Call by Value
10.3. Return types for a function.
10.4. Recursion.
11. I/O Console..
11.1. Printf()
11.2. Scanf()
11.3. Getchar()
11.4. Putchar()
12. Pointers.
12.1. Variable Pointer
12.2. Function Pointer
13. Preprocessor Directives.
13.1. #define :
13.2. #error
13.3. #include.
13.4. Conditional compilation.
13.4.1. #if …. #else…..#endif.
13.4.2. #ifdef.
13.4.3. #ifndef.
13.4.4. #pragma.
14. Dynamic Memory Allocation..
14.1. Malloc()
14.2. Calloc()
14.3. Free()
15. Command Line Arguments.
16. File I/O..
16.1. Fclose.
16.2. Feof.
16.3. Ferror
16.4. Fflush.
16.5. Fgetc.
16.6. Fgetpos.
16.7. Fgets.
16.8. Fopen.
16.9. Fprintf.
16.10. Fputs.
16.11. Fread.
16.12. Freopen.
16.13. Fscanf.
16.14. Fseek.
16.15. Fsetpos.
16.16. Ftell
16.17. Fwrite.
17. Library Functions.
17.1. Getc.
17.2. Getchar
17.3. Perror
17.4. Printf.
17.5. Putc.
17.6. Putchar
17.7. Puts.
17.8. Remove.
17.9. Rename.
17.10. Rewind.
17.11. Scanf.
17.12. Setbuf.
17.13. Setvbuf.
17.14. Snprintf.
17.15. Sprint
17.16. Sscanf.
17.17. Tmpfile.
17.18. Tmpnam...
17.19. Ungetc.
17.20. Vprintf.
17.21. Vfprintf.
17.22. Vsnpfintf.
17.23. Vscanf.
17.24. Vfscanf.
17.25. Vsscanf.
18. String & Character functions.
18.1. Isalnumt
18.2. Isalpha.
18.3. Isblank.
18.4. Iscntrl
18.5. Isdigit
18.6. Isgraph.
18.7. Islower
18.8. Isupper
18.9. Isprint
18.10. Ispunct
18.11. Isspace.
18.12. Isxdigit
18.13. memchr
18.14. memcmp.
18.15. memcpy.
18.16. memmove.
18.17. memset
18.18. strchr
18.19. strcat
18.20. strcmp.
18.21. strcoll
18.22. strcpy.
18.23. strcspn.
18.24. strerror
18.25. strlen.
18.26. strncat
18.27. strncmp.
18.28. strncpy.
18.29. strpbrk.
18.30. strrchr
18.31. strspn.
18.32. strstr
18.33. strtok.
18.34. strxfrm...
18.35. tolower
18.36. toupper
19. Data Structures.
19.1. Stack.
19.2. Queue.
19.3. Linked list
20. Algorithms.
20.1. Sorting.
20.1.1. Bubble sort
20.1.2. Insertion sort
20.1.3. Selection sort
20.2. Insertion.
20.3. Deletion
20.4. Searching.
20.4.1. Linear search.
20.4.2. Binary search.
21. Simple Applications.
21.1. Palindrome Number
21.2. Armstrong Number
21.3. Fibonacci Series.
21.4. Swapping.
21.5. Prime numbers.
21.6. Reverse string/numbers.
22. Compilation steps.
1. Introduction to C
The programming language C was developed in the 1970s by Dennis Ritchie at AT & T Bell Labs
C is a Middle level language
C is a Structured Language
C is Highly Portable
Designed for Compilation
2. C Fundamentals
2.1. Program Structure
/*Program to add 2 numbers*/ // Comments
#include <stdio.h> // Pre-Processor Directives
#include “add.h”
Int sum(int a,int b) // Function Definition/Prototype
Int main() // Main Function
{
Int a,b; // Variable Declaration
Int Res = 0 ; //Variable Declaration + Initialization
Printf(“Enter 2 Numbers:”);
Scanf(“%d%d”,&a,&b);
Res =sum( a,b); // Function Call
Printf(“\nThe Sum = %d”,Res);
Return 0; //Returns to Main Function
}
Int Res(int x,int y) // Function Definition
{
Return(x+y); //Return to Function res
}
2.2. Character Set
Alphabets
A-Z , a-z
26 Upper + 26 lower Case
Digits
0 - 9
10 Digits
Special Symbols
!@#$%^&*..
29 Special Characters
White Spaces
Space, Horizontal/vertical Tab, newline
5 white spaces
Escape Sequence
\n - Newline
\b - Baskspace
\r - Carriage Return
\t - Horizontal Tab
\v - Vertical Tab
Trigraph Sequence
‘??=’ for #
2.3. Identifiers
Identifiers are names of Variables, functions, macros, types etc .
Rules :
a) First Letter of an identifier should be a letter
b) Can use only _ in special character.
c) They are Case_Sensitive.
d) only first 31 character of an identifier is significant
e) Should not use Keywords as they are reserved
2.4. Keywords
Keywords are words that have special meaning to the compiler. 32 in number as defned in c89 Eg:
Auto enum restrict unsigned
break extern return void
case float short volatile
char for signed while
const goto sizeof _Bool(*)
continue if static _Complex(*)
default inline(*) struct _Imaginary(*)
do int switch union
double long typedef else register
2.5. Constants
Constants can be Primary or Secondary
Primary Constants can be (Integer Constants, Real Constants, Character Constants )
a) Integer Constant Can be represented as follows
(Base-10) Decimal Constants(Begins without a 0) eg: 1934
(Base-8) OctalConstants(Begins with 0) eg: 012
(Base-16) HexadecimalConstants(Begins with 0X/0x) eg: 0x12A/0x12a
b) Real Constants
eg: 2.35, -3.42, 0.128, 3.12e7, -0.25E24
c) Character Constant (Single digit/letter/symbol in single quotes)
eg: ‘A’, ‘e’ , ‘5’, ‘$’
Secondary Constants are (Array, Pointer, Enumerator, Union….)
2.6. Variables
Variables are names given to memory location. Rules are similar to identifier.
2.7. Comments
Comments are used to give readability/ understanding to the program eg:
// This is a single line comment <- used for single line comment
/* This is a block
or multiline comment */ <- Multiline comment
2.8. Expressions and Operators
An expression is the combination of operators and operands. Operators are of the following type. It can be classified as number of operands used
Unary Operator: One Operand only Required eg: -5, &a
Binary Operator: Two Operands required eg: a+b, c%2
Ternary Operator: Three Operands required ?: eg (a>2)?c= 1:c=2;
They can also be classified as follows
2.8.1. Arithmetic Operators
Used for arithmetic operations
+ (Addition) 2+5 = 7
- (Subtraction) 8-3 =5
* (Multiplication) 4*3 = 12
/ (Division) 7/2 = 3 (int) , 7/2 = 3.5 (Float) [Quotient]
% (Modulus) 7/2 = 1 , 6/3=0 [Remainder]
Precedence of operators are as follows BODMAS(Brackets of Division Multiplication Additon Subtraction)
2.8.2. Assignment Operators
Used to assign some value to lvalue (can be simple and compound)
c = x Simple Assignment Assign the value x to c
c+=x Compound Assignment c = c+x (Instead of + we can use -,* etc)
2.8.3. Relational Operators
Used for comparison which yields 1 if true and 0 if false eg:
< Less Than
> Greater Than
<= Less Than OR Equal to
>= Greater Than OR Equal to
!= Not Equal to
== Equivalent
2.8.4. Logical Operators
The Logical operators can be used to combine the results of several comparison expressions into one logical expression.
OPERATOR
|
SYMBOL
|
EXPRESSION
|
RESULT
|
AND
|
&&
|
x &&y
|
0 if x or y is 0
|
OR
|
||
|
x||y
|
1 if x or y is 1
|
NOT
|
!
|
!x
|
1 if x=0, 0 if x=1
|
The Left side of logical operators are always evaluated first.
2.8.5. Bitwise Operators
Used for bitwise operation
And x & y 1, if both x & y are 1
Or x | y 1,If any x or y are 1
Xor x ^ y 1, if x and y are different
Not ~ x 1,if x =0, 0 , if x=1
Shift Left x << y Each bit in x is shifted y positions to the left (Multiplication)
Shift Right x>>y Each bit in x is shifted y positions to the right (Division)
2.8.6. Memory Access Operators
& Address of &x
* Indirection *x
[ ] Array Element x[i]
. Member of a
Structure/Union s.x x Member of structure s
Structure/Union s->x x Member of structure s (When s is a pointer to a structure)
2.8.7. Other Operators
( ) Function call
(type) Cast
(size of) Size in Bytes
?: Conditional Evaluation
, Sequence Operator
3. Data Types
3.1. Predefined Data Types
Data Types define how much space does a variable take in memory, the range of data it can handle and the type of data. The Predefined Data types are as follows
Integer (int) – Used to store integer type data (15,80…). Storage dependent on the microprocessor user (16 Bit, 32 Bit).
Character(char) – Single character stored. 1 Byte
Floating Point (float) – Decimal Numbers (12.23) . use #include<float.h> . 4 Bytes
Double Precision floating point (double)- 8 Bytes
Void – Used for functions that does not
1) Take in any value
2) Does not return any value
Subscribe to:
Posts (Atom)