Program to copy one file to another

#include "stdio.h"
#include "stdlib.h"

#define BUFFER_SIZE 1024

long fcopy(char *dest, char *source)
{
FILE *d, *s;
char *buffer;
size_t incount;
long totcount = 0L;

s = fopen(source, "rb");
if(s == NULL)
return -1L;

d = fopen(dest, "wb");
if(d == NULL)
{
fclose(s);
return -1L;
}

buffer = malloc(BUFFER_SIZE);
if(buffer == NULL)
{
fclose(s);
fclose(d);
return -1L;
}

incount = fread(buffer, sizeof(char), BUFFER_SIZE, s);

while(!feof(s))
{
totcount += (long)incount;
fwrite(buffer, sizeof(char), incount, d);
incount = fread(buffer, sizeof(char), BUFFER_SIZE, s);
}

totcount += (long)incount;
fwrite(buffer, sizeof(char), incount, d);

free(buffer);
fclose(s);
fclose(d);

return totcount;
}

Progran to formatting disks

#include
#include
#include "snpdskio.h"

int CDECL absdisk(unsigned char function,
unsigned short drive,

/* 0 = A:, etc. */
size_t number_of_sectors,
size_t starting_sector,
void * sector_buffer);

int AbsDiskRead(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr)
{
return absdisk(0x25, drive, num_of_sectors, (unsigned)sector, ptr);
}

int AbsDiskWrite(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr)
{
return absdisk(0x26, drive, num_of_sectors, (unsigned)sector, ptr);
}

Program to read and write absolute disk sectors

#include "stddef.h"
#include "dos.h"
#include "snpdskio.h"

int CDECL absdisk(unsigned char function,
unsigned short drive,
/* 0 = A:, etc. */
size_t number_of_sectors,
size_t starting_sector,
void * sector_buffer);

int AbsDiskRead(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr)
{
return absdisk(0x25, drive, num_of_sectors, (unsigned)sector, ptr);
}

int AbsDiskWrite(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr)
{
return absdisk(0x26, drive, num_of_sectors, (unsigned)sector, ptr);
}

ASCII to EBCDIC conversion functions

#include "a2e.h"

static unsigned char a2e[256] = {
0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15,
16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
64, 79,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
215,216,217,226,227,228,229,230,231,232,233, 74,224, 90, 95,109,
121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
151,152,153,162,163,164,165,166,167,168,169,192,106,208,161, 7,
32, 33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,225,
65, 66, 67, 68, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
88, 89, 98, 99,100,101,102,103,104,105,112,113,114,115,116,117,
118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
159,160,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
184,185,186,187,188,189,190,191,202,203,204,205,206,207,218,219,
220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255
};

static unsigned char e2a[256] = {
0, 1, 2, 3,156, 9,134,127,151,141,142, 11, 12, 13, 14, 15,
16, 17, 18, 19,157,133, 8,135, 24, 25,146,143, 28, 29, 30, 31,
128,129,130,131,132, 10, 23, 27,136,137,138,139,140, 5, 6, 7,
144,145, 22,147,148,149,150, 4,152,153,154,155, 20, 21,158, 26,
32,160,161,162,163,164,165,166,167,168, 91, 46, 60, 40, 43, 33,
38,169,170,171,172,173,174,175,176,177, 93, 36, 42, 41, 59, 94,
45, 47,178,179,180,181,182,183,184,185,124, 44, 37, 95, 62, 63,
186,187,188,189,190,191,192,193,194, 96, 58, 35, 64, 39, 61, 34,
195, 97, 98, 99,100,101,102,103,104,105,196,197,198,199,200,201,
202,106,107,108,109,110,111,112,113,114,203,204,205,206,207,208,
209,126,115,116,117,118,119,120,121,122,210,211,212,213,214,215,
216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,
123, 65, 66, 67, 68, 69, 70, 71, 72, 73,232,233,234,235,236,237,
125, 74, 75, 76, 77, 78, 79, 80, 81, 82,238,239,240,241,242,243,
92,159, 83, 84, 85, 86, 87, 88, 89, 90,244,245,246,247,248,249,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,250,251,252,253,254,255
};

unsigned char ASCIItoEBCDIC(const unsigned char c)
{
return a2e[c];
}

unsigned char EBCDICtoASCII(const unsigned char c)
{
return e2a[c];
}

Program to Read the File.

#include "stdio.h"

int main()
{
FILE *infile;
char c, infilename[25], inputline[100];
int line = 1;

printf("Enter input file name ----> ");
scanf("%s", infilename);
infile = fopen(infilename, "r");

printf("%5d", line);
do
{
c = fgets(inputline, 100, infile); /* read a line */
if (c != NULL)
{
printf("%5d %s", line, inputline);
line++;
}
} while (c != NULL);

fclose(infile);

return 0;
}

Structures In C

#include
#include

struct child
{
char initial;
int age;
int grade;
} *kids[12];

int main()
{
int index;

for (index = 0 ; index < 12 ; index++)
{
kids[index] = (struct child *)malloc(sizeof(struct child));
kids[index]->initial = 'A' + index;
kids[index]->age = 16;
kids[index]->grade = 84;
}

kids[3]->age = kids[5]->age = 17;
kids[2]->grade = kids[6]->grade = 92;
kids[4]->grade = 57;

*kids[10] = *kids[4]; /* Structure assignment */

for (index = 0 ; index < 12 ; index++)
printf("%c is %d years old and got a grade of %d\n",
kids[index]->initial, kids[index]->age,
kids[index]->grade);

return 0;
}



/* Result of execution

A is 16 years old and got a grade of 84
B is 16 years old and got a grade of 84
C is 16 years old and got a grade of 92
D is 17 years old and got a grade of 84
E is 16 years old and got a grade of 57
F is 17 years old and got a grade of 84
G is 16 years old and got a grade of 92
H is 16 years old and got a grade of 84
I is 16 years old and got a grade of 84
J is 16 years old and got a grade of 84
E is 16 years old and got a grade of 57
L is 16 years old and got a grade of 84

*/

atan Function in C.

atan Function in C.


#include "libc.h"
#include "math.h"
#include "errno.h"

static int nopper() {;}

#define PI 3.14159265358979323846
#define PIov2 1.57079632679489661923

double atan2(v,u)
double u,v;
{
double f;
int (*save)();
extern int flterr;
extern int errno;

if (u == 0.0) {
if (v == 0.0) {
errno = EDOM;
return 0.0;
}
return PIov2;
}

save = Sysvec[FLT_FAULT];
Sysvec[FLT_FAULT] = nopper;
flterr = 0;
f = v/u;
Sysvec[FLT_FAULT] = save;
if (flterr == 2) /* overflow */
f = PIov2;
else {
if (flterr == 1) /* underflow */
f = 0.0;
else
f = atan(fabs(f));
if (u <>
f = PI - f;
}
if (v <>
f = -f;
return f;
}

#define P0 -0.13688768894191926929e+2
#define P1 -0.20505855195861651981e+2
#define P2 -0.84946240351320683534e+1
#define P3 -0.83758299368150059274e+0
#define Q0 +0.41066306682575781263e+2
#define Q1 +0.86157349597130242515e+2
#define Q2 +0.59578436142597344465e+2
#define Q3 +0.15024001160028576121e+2

#define P(g) (((P3*g P2)*g P1)*g P0)
#define Q(g) ((((g Q3)*g Q2)*g Q1)*g Q0)

double atan(x)
double x;
{
double f, r, g;
int n;
static double Avals[4] = {
0.0,
0.52359877559829887308,
1.57079632679489661923,
1.04719755119659774615
};

n = 0;
f = fabs(x);
if (f > 1.0) {
f = 1.0/f;
n = 2;
}
if (f > 0.26794919243112270647) {
f = (((0.73205080756887729353*f - 0.5) - 0.5) + f) /
(1.73205080756887729353 + f);
++n;
}
if (fabs(f) <>
r = f;
else {
g = f*f;
r = f + f *
((P(g)*g)/Q(g));
}
if (n > 1)
r = -r;
r += Avals[n];
if (x <>
r = -r;
return r;
}


String Functions In C

char *strcpy(dest, src)
register char *dest, *src;
{
char *sav;

sav = dest;
while (*dest++ = *src++)
;
return sav;
}

char *strncpy(dest, src, len)
register char *dest, *src;
{
char *sav;

sav = dest;
while (len--) {
if ((*dest++ = *src++) == 0) {
while (len--)
*dest++ = 0;
break;
}
}
return sav;
}

char *strcat(dest, src)
register char *dest, *src;
{
char *sav;

sav = dest;
while (*dest)
++dest;
while (*dest++ = *src++)
;
return sav;
}

char *strncat(dest, src, len)
register char *dest, *src;
{
char *sav;

sav = dest;
while (*dest)
++dest;
do {
if (len-- == 0) {
*dest = 0;
break;
}
} while (*dest++ = *src++);
return sav;
}

strcmp(a, b)
register char *a, *b;
{
int i;

while ((i = *a - *b++) == 0 && *a++)
;
return i;
}

strncmp(a, b, len)
register char *a, *b;
{
int i = 0;

while (len-- && (i = *a - *b++) == 0 && *a++)
;
return i;
}

strlen(str)
register char *str;
{
register int len = 0;

while (*str++)
++len;
return len;
}

char *index(str, c)
register char *str; register int c;
{
while (*str) {
if (*str == c)
return str;
++str;
}
return (char *)0;
}

char *rindex(str, c)
register char *str; int c;
{
register char *cp;

for (cp = str ; *cp++ ; )
;
while (cp > str)
if (*--cp == c)
return cp;
return (char *)0;
}

arcsine function

#include "math.h"
#include "errno.h"

double arcsine();

double asin(x)
double x;
{
return arcsine(x,0);
}

double acos(x)
double x;
{
return arcsine(x,1);
}

#define P1 -0.27368494524164255994e+2
#define P2 +0.57208227877891731407e+2
#define P3 -0.39688862997504877339e+2
#define P4 +0.10152522233806463645e+2
#define P5 -0.69674573447350646411
#define Q0 -0.16421096714498560795e+3
#define Q1 +0.41714430248260412556e+3
#define Q2 -0.38186303361750149284e+3
#define Q3 +0.15095270841030604719e+3
#define Q4 -0.23823859153670238830e+2

#define P(g) ((((P5*g P4)*g P3)*g P2)*g P1)
#define Q(g) (((((g Q4)*g Q3)*g Q2)*g Q1)*g Q0)

double arcsine(x,flg)
double x;
{
double y, g, r;
register int i;
extern int errno;
static double a[2] = { 0.0, 0.78539816339744830962 };
static double b[2] = { 1.57079632679489661923, 0.78539816339744830962 };

y = fabs(x);
i = flg;
if (y < 2.3e-10)
r = y;
else {
if (y > 0.5) {
i = 1-i;
if (y > 1.0) {
errno = EDOM;
return 0.0;
}
g = (0.5-y)+0.5;
g = ldexp(g,-1);
y = sqrt(g);
y = -(y+y);
} else
g = y*y;
r = y + y*
((P(g)*g)
/Q(g));
}
if (flg) {
if (x < 0.0)
r = (b[i] + r) + b[i];
else
r = (a[i] - r) + a[i];
} else {
r = (a[i] + r) + a[i];
if (x < 0.0)
r = -r;
}
return r;
}


Fuzzy string searching subroutines

#define DEBUG 1

#include
#include
#include

/* local, static data */

static char *Text, *Pattern; /* pointers to search strings */
static int Textloc; /* current search position in Text */
static int Plen; /* length of Pattern */
static int Degree; /* max degree of allowed mismatch */
static int *Ldiff, *Rdiff; /* dynamic difference arrays */
static int *Loff, *Roff; /* used to calculate start of match */

void App_init(char *pattern, char *text, int degree)
{
int i;

/* save parameters */

Text = text;
Pattern = pattern;
Degree = degree;

/* initialize */

Plen = strlen(pattern);
Ldiff = (int *) malloc(sizeof(int) * (Plen + 1) * 4);
Rdiff = Ldiff + Plen + 1;
Loff = Rdiff + Plen + 1;
Roff = Loff + Plen + 1;
for (i = 0; i <= Plen; i++)
{
Rdiff[i] = i; /* initial values for right-hand column */
Roff[i] = 1;
}

Textloc = -1; /* current offset into Text */
}

void App_next(char **start, char **end, int *howclose)
{
int *temp, a, b, c, i;

*start = NULL;
while (*start == NULL) /* start computing columns */
{
if (Text[++Textloc] == '\0') /* out of text to search! */
break;

temp = Rdiff; /* move right-hand column to left ... */
Rdiff = Ldiff; /* ... so that we can compute new ... */
Ldiff = temp; /* ... right-hand column */
Rdiff[0] = 0; /* top (boundary) row */

temp = Roff; /* and swap offset arrays, too */
Roff = Loff;
Loff = temp;
Roff[1] = 0;

for (i = 0; i < Plen; i++) /* run through pattern */
{
/* compute a, b, & c as the three adjacent cells ... */

if (Pattern[i] == Text[Textloc])
a = Ldiff[i];
else a = Ldiff[i] + 1;
b = Ldiff[i+1] + 1;
c = Rdiff[i] + 1;

/* ... now pick minimum ... */

if (b < a)
a = b;
if (c < a)
a = c;

/* ... and store */

Rdiff[i+1] = a;
}

/* now update offset array */
/* the values in the offset arrays are added to the
current location to determine the beginning of the
mismatched substring. (see text for details) */

if (Plen > 1) for (i=2; i<=Plen; i++)
{
if (Ldiff[i-1] < Rdiff[i])
Roff[i] = Loff[i-1] - 1;
else if (Rdiff[i-1] < Rdiff[i])
Roff[i] = Roff[i-1];
else if (Ldiff[i] < Rdiff[i])
Roff[i] = Loff[i] - 1;
else /* Ldiff[i-1] == Rdiff[i] */
Roff[i] = Loff[i-1] - 1;
}

/* now, do we have an approximate match? */

if (Rdiff[Plen] <= Degree) /* indeed so! */
{
*end = Text + Textloc;
*start = *end + Roff[Plen];
*howclose = Rdiff[Plen];
}
}

if (start == NULL) /* all done - free dynamic arrays */
free(Ldiff);
}

#ifdef DEBUG

void main(int argc, char **argv)
{
char *begin, *end;
int howclose;

if (argc != 4)
{
puts("Usage is: approx pattern text degree\n");
exit(0);
}

App_init(argv[1], argv[2], atoi(argv[3]));
App_next(&begin, &end, &howclose);
while (begin != NULL)
{
printf("Degree %d: %.*s\n", howclose, end-begin+1, begin);
App_next(&begin, &end, &howclose);
}
}

#endif

Program to find the size of a file

#include "stdio.h"

long flength(char *fname)
{
FILE *fptr;
long length = -1L;

fptr = fopen(fname, "rb");
if(fptr != NULL)
{
fseek(fptr, 0L, SEEK_END);
length = ftell(fptr);
fclose(fptr);
}

return length;
}

#ifdef TEST

main(int argc, char *argv[])
{
printf("Length of %s = %ld\n", argv[0], flength(argv[0]));
return 0;
}

#endif /

Program to read a file and count blank space, characters,numbers & speical char

/* to read a file and count blank space, characters,numbers & speical char */
#include
main()
{ char filename[20],ch;
FILE *fp;
int x=0,y=0,z=0,p=0,q=0;
clrscr();
printf("\nEnter the name of the file");
scanf("%s",filename);
fp=fopen(filename,"r");
if(fp==NULL)
{ printf("\nError in reading");
getch();
return;
}
while((ch=fgetc(fp))!=EOF)
{ if(ch==' ')
x=x+1;
else if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
y=y+1;
else if(ch>=0 && ch<=9)
z=z+1;
else if(ch=='\n' || ch=='\r' || ch=='\t')
p=p+1;
else
q=q+1;
}
fclose(fp);
printf("\nBlank space=%d",x);
printf("\nAlphabets =%d",y);
printf("\nNumbers =%d",z);
printf("\nWhite spaces=%d",p);
printf("\nSpecial char=%d",q);
getch();
}

program to use # define

//program to use # define
#include
#include
#define square(x) x*x
void main()
{
int i;
clrscr();
printf("\nEnter a number");
scanf("%d",&i);
printf("\nSquare of %d is %d ",i,square(i));
getch();
}


last position for fibonacci

void main()
{
int a=0,b=1,c=0,pos,i=1;
clrscr();
printf("\n Enter last position for fibonacci");
scanf("%d",&pos);
printf("\n %d\t%d",a,b);
while(i<=pos)
{
c=a+b;
printf("\t%d",c);
a=b;
b=c;
i++;
}
getch();
}

Global Variable In C

#include"conio.h"
#include"stdio.h"
int i;
main()
{
clrscr();
call1();
call2();
printf("%d\n",i);
call1();
printf("%d\n",i);
call2();
printf("%d\n",i);
getch();
}
call1()
{
i++;
printf("%d\n",i);
}
call2()
{
printf("%d\n",i);
i++;
}

Heap Sort Program In Data structure

#include "stdio.h"
#include "conio.h"
void main()
{
int i,v,p,l[100],n,k,t,j;
clrscr();
printf("enter the number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&l[i]);
}
printf("\nentered list are as follows");
for(i=1;i<=n;i++)
{
printf("\t%d",l[i]);
}
/*creation of heap*/
for(k=2;k<=n;k++)
{
i=k;
t=l[k];
j=i/2;
while((i>1)&&(t > l[j]))
{
l[i]=l[j];
i=j;
j=i/2;
if(j<1)
j=1;
}
l[i]=t;
}



printf("\nHEAP");


for(i=1;i<=n;i++)
{
printf("\t%d",l[i]);
}
printf("\n");
//heapsort
for(k=n;k>=2;--k)
{
t=l[1];
l[1]=l[k];
l[k]=t;
i=1;
v=l[1];
j=2;
if((j+1)
if(l[j+1]>l[j])
j++;
while((j<=(k-1))&&(l[j]>v))
{
l[i]=l[j];
i=j;
j=2*i;
if((j+1)
if(l[j+1]>l[j])
j++;
else
if(j>n)
j=n;
l[i]=v;
}
for(p=1;p<=n;p++)
{
printf("\t%d",l[p]);
}
printf("\n");
}
printf("\nthe sorted list ");
for(i=1;i<=n;i++)
{
printf("\t%d",l[i]);
}
getch();

}



Program on initialization of double dimensional array

#include "stdio.h"
#include"conio.h"
void main()
{
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
getch();
}

Insertion Sort In C

/* what is insertion sort*/
#include
#include
main()
{
int i,l[100],n,p,t,k;
clrscr();
printf("enter the number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&l[i]);
}
l[0]=-0;
for(i=1;i<=n;i++)
{
p=i-1;
t=l[i];
while(t {
l[p+1]=l[p];
p--;
}
l[p+1]=t;
/* for( k=1;k<=n;k++)
{
printf("\t%d",l[k]);
}
printf("\n");*/
}
printf("Sorted list");
for(i=1;i<=n;i++)
{
printf("\t%d",l[i]);
}

getch();


}

Link List Program In C

/* link list */
# include
# include
struct node
{ int code;
char name[15];
struct node *next;
}*start,*ptr,*temp;
main()
{ int ch;
while(1)
{ clrscr();
printf("\n\t\tMAIN MENU");
printf("\n \t1. Create a link list");
printf("\n \t2. Display the list");
printf("\n \t3. Add a node in the beginning");
printf("\n \t4. Append a node");
printf("\n \t5. Insert a node");
printf("\n \t6. Delete a node");
printf("\n \t7. Sort the list");
printf("\n \t8. Exit the program");
printf("\n\tEnter your choice(1 to 8):");
scanf("%d",&ch);
switch (ch)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
addbeg();
break;
case 4:
addend();
break;
case 5:
insnode();
break;
case 6:
delnode();
break;
case 7:
sort();
break;
case 8:
return;
default:
printf("\nWrong choice

entered,Try again");
}
getch();
}
}
create()
{
char ans='y';
start=ptr=(struct node *) malloc(sizeof(struct

node));
// printf("SIZE OF NODE=%d",sizeof(struct node

)); //c
printf("\tstart=%u",start); //c
printf("\tptr=%u",ptr); //c
while (ans=='y' || ans=='Y')
{ printf("\nEnter code and name:");


scanf("%d%s",&ptr->code,ptr->name);
printf("\nWant to add another

node(Y/N):");
ans=getche();
if(ans=='y' || ans=='Y')
{
ptr->next=(struct node *)

malloc(sizeof(struct node));
printf("\tptr

next=%d",ptr->next); //c
ptr=ptr->next;
printf("\tptr =%u",ptr); //c
}
else
ptr->next=NULL;
}
return;
} //*******END****



display()
{
ptr=start;
printf("start in display=%u",ptr); //c
while(ptr!=NULL)
{
printf("\nCode=%d \t

Name=%s",ptr->code,ptr->name);
ptr=ptr->next;
printf("ptr =%u",ptr); //c
}
return;
}

addbeg()
{
char ans='y';
while(ans=='y' || ans=='Y')
{
ptr=(struct node *)

malloc(sizeof(struct node));
printf("\tptr=%u",ptr); //c
printf("\nEnter code and name:");


scanf("%d%s",&ptr->code,ptr->name);
ptr->next=start;
printf("ptr next=%d",ptr->next); //c
start=ptr;
printf("Start=%u",start); //c
printf("\nWant to add another(y/n)");
ans=getche();
}
return;
}

addend()
{ char ans='y';
ptr=start;
printf("ptr=%u",ptr); //c
while(ptr->next!=NULL)
ptr=ptr->next;
while(ans=='y'|| ans=='Y')
{
temp=(struct node *)

malloc(sizeof(struct node));
printf("temp=%u",temp); //c
printf("\nEnter code & name");


scanf("%d%s",&temp->code,temp->name);
temp->next=NULL;
ptr->next=temp;
printf("ptr next=%d",ptr->next);
ptr=ptr->next;
printf("ptr=%u",ptr);
printf("\nwant to add (y/n)");
ans=getche();
}
return;
}

insnode()
{ int cd;
ptr=start;
if(ptr!=NULL)
{
printf("\nEnter code before which to insert:");
scanf("%d",&cd);
}

if(ptr==NULL)
{
start=(struct node *)

malloc(sizeof(struct node));
printf("\nEnter code & name");


scanf("%d%s",&start->code,start->name);
start->next=NULL;
}
else if(ptr->code==cd)
{ ptr=(struct node *)

malloc(sizeof(struct node));
printf("ptr=%u",ptr); //c
printf("in 1"); //c
printf("\nEnter code & name:");


scanf("%d%s",&ptr->code,ptr->name);
ptr->next=start;
printf("ptr next=%d",ptr->next); //c
start=ptr;
printf("start =%u",ptr); //c
}
else
{
while(ptr->code!=cd && ptr!=NULL)
{
printf(" in 2 ");
temp=ptr;
printf("temp=%u",ptr);

//c
ptr=ptr->next;
printf("ptr

next=%d",ptr->next); //c
}
temp->next=(struct node *)

malloc(sizeof(struct node));
printf("temp next=%d",temp->next);

//c
printf("\nEnter code & name");


scanf("%d%s",&temp->next->code,temp->next->name);
temp->next->next=ptr;
printf("\ntemp next

next=%d",ptr);//c
}
return;
}

delnode()
{
int cd;
ptr=start;
if(ptr==NULL)
printf("\nLink list is empty");
else
{ printf("\nEnter code to be deleted");
scanf("%d",&cd);
if(start->code==cd)
{ ptr=start;
start=start->next;
free(ptr);
printf("\nNode successfully deleted");
}
else
{
while(ptr->code!=cd && ptr!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
if(ptr==NULL)
printf("\nNode not found");
else
{
temp->next=ptr->next;
free(ptr);
printf("\nNode deleted

successfully");
}
}
}
return;
}
sort()
{
struct node *ptr1;
char *temp1[15];
ptr=start;
ptr1=start;
if(ptr==NULL)
printf("\n Link list is empty");
else
while(ptr->next!=NULL)
{
ptr1=ptr->next;
while(ptr1!=NULL)
{
if(ptr->code > ptr1->code)
{
temp=ptr->code;
ptr->code=ptr1->code;
ptr1->code=temp;

strcpy(temp1,ptr->name);
strcpy(ptr->name,ptr1->name);
strcpy(ptr1->name,temp1);
}
ptr1=ptr1->next;
}
ptr=ptr->next;
}
return;
}

Program to create Marklist In C

#include "stdio.h"
#include "conio.h"
void main()
{
char n[5];
int m1[5],m2[5],m3[5],tot[5],i,g=0,pos;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter name and marks of 3 subjects");
scanf("%c %d %d %d",&n[i],&m1[i],&m2[i],&m3[i]);
fflush(stdin);
tot[i]=m1[i]+m2[i]+m3[i];
if(tot[i]>g)
{
g=tot[i];
pos=i+1;
}
}
clrscr();
printf("\n \t\t\tMarksheet");
printf("\n \t\t\t---------");
printf("\n\n----------------------------------------------");
printf("\nName\tMaths\tPhy\tChem\tTotal");
printf("\n------------------------------------------------");
for(i=0;i<5;i++)
{
printf("\n%c\t %d\t %d\t %d\t %d\n",n[i],m1[i],m2[i],m3[i],tot[i]);
}
printf("\n------------------------------------------------");
printf("\nThe Highest Total=%d",g);
printf("\nThe Number of the Candidate=%d",pos);
getch();
}


program for multiplication of 2 matrix of 3 x 3

//program for multiplication of 2 matrix of 3 x 3
#include "stdio.h"
#include "conio.h"
void main()
{
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int b[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int c[3][3];
int i,j,k;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}


Program on Nested Structure

//Program on Nested Structure
#include "stdio.h"
#include "conio.h"
struct adress {
char ct[10];
char dist[10],state[5];
long int pin;
};
struct emp {
char name[10];
int age,sal;
struct adress a;
};
void main()
{
struct emp e[2];
int i;
clrscr();
for(i=0;i<=1;i++)
{
printf("Enter [%d]st Employee's Name,Age,salary :: ", i);
scanf("%s%d%d",e[i].name,&e[i].age,&e[i].sal);
printf("\nEnter city, district,state & pincode ::");
scanf("%s%s%s%ld",e[i].a.ct,e[i].a.dist,e[i].a.state,&e[i].a.pin);
}

for(i=0;i<=1;i++)
{
printf("\n[%d]st Employee's Name :: %s",i,e[i].name);
printf("\n[%d]st Employee's Age :: %d ",i,e[i].age);
printf("\n[%d]st Employee's Salary :: %d",i,e[i].sal);
printf("\n[%d]st Employee's City :: %s ",i,e[i].a.ct);
printf("\n[%d]st Employee's District :: %s",i,e[i].a.dist);
printf("\n[%d]st Employee's State :: %s",i,e[i].a.state);
printf("\n[%d]st Employee's Pin :: %ld",i,e[i].a.pin);
}
getch();
}

Queue In C

/* queues */
# include "conio.h"
# include "stdio.h"
struct node
{ struct node *prev;
int num;
struct node *next;
}*top=NULL,*ptr,*bottom=NULL;
main()
{
int ch;
while(1)
{
clrscr();
printf("\n\t\tMain Menu");
printf("\n\t\t1. Insert into queue");
printf("\n\t\t2. Delete from queue");
printf("\n\t\t3. Display queue details");
printf("\n\t\t4. Exit program");
printf("\n\t\Enter your choice(1-4)");
scanf("%d",&ch);

switch(ch)
{ case 1:
ins();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
return;
default:
printf("\nWrong choice");
}
getch();
}
}
ins()
{ if(top==NULL)
{
printf("in 1");
ptr=(struct node *) malloc(sizeof(struct node));
printf("ptr=%u",ptr);
top=ptr;
printf("top=%u",top);
bottom=ptr;
printf("bottom=%u",bottom);
printf("\nEnter a Number");
scanf("%d",&ptr->num);
ptr->prev=ptr->next=NULL;
printf("ptr prev=%d, ptr next=%d",ptr->prev,ptr->prev);
}
else
{
printf("in 2");
ptr=(struct node *) malloc(sizeof(struct node));
printf("ptr=%u",ptr);
printf("\nEnter a Number");
scanf("%d",&ptr->num);
ptr->next=top;
printf("ptr next=%d",ptr->next);
top=ptr;
printf("top =%u",top);
ptr->next->prev=top;
printf("ptr-next-prev=%d",ptr->next->prev);
ptr->prev=NULL;
printf("ptr->prev=%d",ptr->prev);
}
}
del()
{
if(bottom==NULL)
printf("\nQueue is empty");
else
{
printf("bottom=%u",bottom);
ptr=bottom;
printf("ptr=%u",ptr);
printf("ptr prev=%d",ptr->prev);
bottom=ptr->prev;
printf("ptr bottom=%u",bottom);
printf("\nDeleting node:%d",ptr->num);
free(ptr);
printf("bottom next=%d",bottom->next);
bottom->next=NULL;
}
}

disp()
{
ptr=bottom;
if(ptr==NULL)
printf("\nQueue is empty");
else
while(ptr!=NULL)
{
printf("\n%d",ptr->num);
ptr=ptr->prev;
}
}


Stack Programs

/* stack */
# include
# include
struct node
{
int code;
char name[20];
struct node *next;
}*start=NULL,*ptr;
main()
{
int ch;
while(1)
{
clrscr();
printf("\n\t\tMain Menu");
printf("\n\t\t1. Push");
printf("\n\t\t2. Pop");
printf("\n\t\t3. Display Stack");
printf("\n\t\t4. Exit program");
printf("\n\t\Enter your choice(1-4)");
scanf("%d",&ch);
switch(ch)
{ case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
default:
printf("\nWrong choice");
}
getch();
}
}
push()
{ ptr=(struct node *) malloc(sizeof(struct node));
printf("ptr=%u",ptr);
printf("\nEnter code & name");
scanf("%d%s",&ptr->code,ptr->name);
printf("start=%u",start);
ptr->next=start;
printf("ptr next=%d",ptr->next);
start=ptr;
printf("start=%u",start);
}
pop()
{
ptr=start;
printf("ptr =%u",ptr);
if(ptr==NULL)
printf("\nStack Empty");
else
{
printf("\n%d popped",ptr->code);
printf("ptr next=%d",ptr->next);
start=ptr->next;
printf("start=%u",start);
free(ptr);
}
}
display()
{ ptr=start;
if(ptr==NULL)
printf("\nThe stack is empty");
else
while(ptr!=NULL)
{ printf("\nCode=%d \tName=%s",ptr->code,ptr->name);
ptr=ptr->next;
}
}

Static Variable In C

#include
#include
main()
{
void callme();
clrscr();
callme();
callme();
callme();
getch();
}
void callme()
{
static int i=0;
i++;
printf("\n%d",i);
}

Tree testing

/* trees */
# include
# include
struct node
{ struct node *left;
int num;
struct node *right;
};
main()
{
int ch,num;
struct node *start=NULL;
while(1)
{
clrscr();
printf("\n\t\tMain Menu");
printf("\n\t\t1. Insert into tree");
printf("\n\t\t2. Display tree in preorder fashion");
printf("\n\t\t3. Display tree in inorder fashion");
printf("\n\t\t4. Display tree in postorder fashion");
printf("\n\t\t5. Exit program");
printf("\n\t\Enter your choice(1-5)");
scanf("%d",&ch);

switch(ch)
{ case 1:
printf("\nEnter number to be inserted");
scanf("%d",&num);
insnode(&start,num);
break;
case 2:
preorder(start);
break;
case 3:
inorder(start);
break;
case 4:
postorder(start);
break;
case 5:
return;
default:
printf("\nWrong choice");
}
getch();
}
}
insnode(struct node *p, int n)
{
if(p==NULL)
{
p=(struct node *) malloc(sizeof(struct node));
p->num=n;
p->left=NULL;
p->right=NULL;
}
else
{
if(n>(p->num))
insnode(p->right,n);
else
insnode(p->left,n);
}
}

preorder(struct node *p)
{
if(p==NULL)
return;
else
{
printf("\nNode:%d",p->num);
preorder(p->left);
preorder(p->right);
}
}

inorder(struct node *p)
{
if(p==NULL)
return;
else
{

inorder(p->left);
printf("\nNode:%d",p->num);
inorder(p->right);
}
}

postorder(struct node *p)
{
if(p==NULL)
return;
else
{
postorder(p->left);
postorder(p->right);
printf("\nNode:%d",p->num);
}
}