Function Address Founder and Tester

#include
void my();
void (*my_address)();
void my()
{
printf("WWW");
}
void main()
{
char far *p;
unsigned seg,off;
my_address=my;
clrscr();
/*****Calling function with the address of my function***/
(*my_address)();
printf("Address of my=%u",my_address);
printf("Segment Address=%x",FP_SEG(my_address));
printf("Offset Address=%x",FP_OFF(my_address));
printf("Getting address by using segment and offset address");
printf("Address of
my=%u",MK_FP(FP_SEG(my_address),FP_OFF(my_address)));
getch();
}

Program for reading the disk's data onto the buffer

#include"stdio.h"
#include"conio.h"
#include"dos.h"

void main(void)
{
clrscr();
union REGS regs;
struct SREGS sregs;
char buff[1000];
int i;
regs.h.ah = 2;
regs.h.al = 1;
regs.h.ch = 1;
regs.h.dh = 0;
regs.h.cl = 1;
regs.h.dl = 0x80;
regs.x.bx = FP_OFF(buff);
sregs.es = FP_SEG(buff);
int86x(0x13,®s,®s,&sregs);
printf("
regs.x.cflag - %d",regs.x.cflag);
printf("
regs.h.ah - %d",regs.h.ah);
printf("
regs.h.al - %d",regs.h.al);
printf("
Buff - %c",buff);
getch();
}

Program for verifying the disk sector

#include "stdio.h"
#include"conio.h"
#include"dos.h"

void main(void)
{
clrscr();
union REGS regs;
struct SREGS sregs;
char buff[1000];
int i;
regs.h.ah = 4;
regs.h.al = 1;
regs.h.ch = 1;
regs.h.dh = 0;
regs.h.cl = 1;
regs.h.dl = 0x80;
regs.x.bx = FP_OFF(buff);
sregs.es = FP_SEG(buff);
int86x(0x13,®s,®s,&sregs);
printf("regs.x.cflag - %d",regs.x.cflag);
printf("regs.h.ah - %d",regs.h.ah);
printf("regs.h.al - %d",regs.h.al);
printf("Buffer - %d",buff);
getch();
}

Program for Scrolling the Window Down



void main(void)
{
clrscr();
union REGS regs;
regs.h.ah = 07;
regs.h.al = 5;
regs.h.bh = 8;
regs.h.ch = 0;
regs.h.cl = 0;
regs.h.dh = 50;
regs.h.dl = 50;
int86(0x10,®s,®s);
getch();
}

Program to write data in DMA Mode

void main()
{
clrscr();
union REGS regs;
int ans;
char arr[1000];

outp(0x3f2,0x1c); //Motor On
delay(200);
outp(0x3f5,0x0f); //Command Code
delay(200);
outp(0x3f5,0x00); //Command Code
delay(200);
outp(0x3f5,0); //Cylinder no.
delay(200);
outp(0x3f5,0x08); //Sense Interrupt Command
delay(200);

ans=inp(0x3f5); //Reading ST0 in data register
delay(100);
ans=inp(0x3f5); //pcn

cout<
delay(200);
outportb(0x12,0); /*initialization of DMA Mode*/
delay(200);
outportb(0x11,10); /*supplying Mode Byte*/

int ar=FP_OFF(arr);
int ar1=FP_SEG(arr);
outportb(0x10,2); /*supplying channel no. on port 10*/

regs.h.ch = (ar1)&(0x0f00);
regs.x.ax = regs.h.ah+ar;
(regs.h.ch)++;

outportb(0x04,regs.h.al);
delay(200);

outportb(0x04,regs.h.ah);
delay(200);
outportb(0x81,regs.h.ch);
delay(200);
outportb(0x05,1);
// DMA End

//READ command
delay(200);
outportb(0x3f5,46); //Command Code
delay(200);
outportb(0x3f5,0); //Command Code
delay(200);

outportb(0x3f5,0); //Cylinder no.
delay(200);

outportb(0x3f5,0); //Head Addr
delay(200);
outportb(0x3f5,3); //Record
delay(200);
outportb(0x3f5,2); //Sector size
delay(200);
outportb(0x3f5,5); //EOT
delay(200);
outportb(0x3f5,21); //GPL
delay(200);
outportb(0x3f5,10); //DTL

//result
delay(200);


// st0

int st0 = inportb(0x3f5);
printf("
st0 = %x",st0);
if(inportb(0x3f5) & (192)!=0)
{
printf("
st0= %d",st0);
cout<<"
Abnormal termination st0";
delay(200);
outp(0x3f2,0x0c); //Motor off
exit(0);
}

delay(200);

int st1 = inportb(0x3f5);
printf("
st1 = %x",st1);
if((st1=inportb(0x3f5)) !=0)
{
printf("
st1=%d ",st1);
cout<<"
Abnormal termination st1";
delay(200);
outp(0x3f2,0x0c); //Motor off
exit(0);
}

delay(200);

int st2 = inportb(0x3f5);
printf("
st2 = %x",st2);
if((st2=inportb(0x3f5)) != 0)
{
cout<<"st2= "<
cout<<"
Abnormal termination st2";
delay(200);
outp(0x3f2,0x0c); //Motor off
exit(0);
}

cout<<"
Successful Termination";
delay(200);

cout<<"
c= "<
delay(200);

cout<<"
h= "<
delay(200);

cout<<"
r= "<
delay(200);

cout<<"
n= "<
delay(200);

outp(0x3f2,0x0c); //Motor off

getch();

}

To Implement Recalibrate Command

#include "conio.h"
#include "stdio.h"
#include "dos.h"

void main()
{
int show;
clrscr();
//Put on the motor
outp(0x3f2,28);

// Check whether the FDC is ready
show=inp(0x3f4); //Read the status of MAIN STATUS REGISTER
show=(show&128);

if(show==128)//Check whether FDC is ready
{

//Input the command parameters
outp(0x3f5,7);//Enter command parameters
delay(200);
outp(0x3f5,0);//Enter Drive No.
delay(300);
}
// Check the status of data register
show=inp(0x3f5);
if(show==0)
printf("Succesfully executed Recalibrate command");
getch();
//Put off the motor
outp(0x3f2,0);

}

Reversal of a singly linklist by recursion

Reversal of a singly linklist by recursion

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
struct node
{
int data;
struct node*next;
};
void insert(struct node**p,int num)
/*Function for inserting an

element into a list */
{
if(*p==NULL)
{
(*p)=(struct node*)malloc(sizeof(struct node));
(*p)->next=NULL;
(*p)->data=num;
}
else
{
insert(&((*p)->next),num);
}
}
void display(struct node*p)
/*Function for displaying the list*/

{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void reverse(struct node**p)
/*Function for reversing the list by

recursion */
{
struct node*q,*r,*x;
int d;
q=(*p); /*stores the address of the first element */
x=q; /*also stores the element of the first element for
counter pourpose */
d=q->data; /*stores the data of the first element*/
r=q->next; /*stores the address of the second element in the list
*/
free(q); /*deletes the first element of the list*/
if(x==NULL)
return ;
else
{
reverse(&(r));/*Recursive call*/
insert(p,d); /*This function is put in the stack so the first
will be taken as last element for the new list */
}
}
void main()
{
clrscr();
struct node*p=NULL;
int n,d,i=0;
printf("How many...? ");
scanf("%d",&n);
while(i++!=n)
{
scanf("%d",&d);
insert(&p,d);
}
display(p);
reverse(&p);
printf("
The reversed list is...");
display(p);
getch();
}

Program to show Basic interactive linked list

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

struct NODE
{

int number;
struct NODE *next;
};

int search_value(struct NODE *llist, int num);
void append_node(struct NODE *llist, int num);
void display_list(struct NODE *llist);
void delete_node(struct NODE *llist, int num);

int main(void) {
int num = 0;
int input = 1;
int retval = 0;
struct NODE *llist;

llist = (struct NODE *)malloc(sizeof(struct NODE));
llist->number = 0;
llist->next = NULL;

while(input != 0) {
printf("\n-- Menu Selection --\n");
printf("0) Quit\n");
printf("1) Insert\n");
printf("2) Delete\n");
printf("3) Search\n");
printf("4) Display\n");
scanf("%d", &input);

switch(input)
{

case 0:
default:
printf("Goodbye ...\n");
input = 0;
break;
case 1:
printf("Your choice: `Insertion'\n");
printf("Enter the value which should be inserted: ");
scanf("%d", &num);
append_node(llist, num);
break;
case 2:
printf("Your choice: `Deletion'\n");
printf("Enter the value which should be deleted: ");
scanf("%d", &num);
delete_node(llist, num);
break;
case 3:
printf("Your choice: `Search'\n");
printf("Enter the value you want to find: ");
scanf("%d", &num);
if((retval = search_value(llist, num)) == -1)
printf("Value `%d' not found\n", num);
else
printf("Value `%d' located at position `%d'\n", num, retval);
break;
case 4:
printf("You choice: `Display'\n");
display_list(llist);
break;
} /* switch */
} /* while */

free(llist);
return(0);
}

void display_list(struct NODE *llist) {
while(llist->next != NULL) {
printf("%d ", llist->number);
llist = llist->next;
}

printf("%d", llist->number);
}

void append_node(struct NODE *llist, int num) {
while(llist->next != NULL)
llist = llist->next;

llist->next = (struct NODE *)
malloc(sizeof(struct NODE));

llist->next->number = num;
llist->next->next = NULL;
}

void delete_node(struct NODE *llist, int num) {
struct NODE *temp;
temp = (struct NODE *)malloc(sizeof(struct NODE));

if(llist->number == num) {
/* remove the node */
temp = llist->next;
free(llist);
llist = temp;
} else {
while(llist->next->number != num)
llist = llist->next;

temp = llist->next->next;
free(llist->next);
llist->next = temp;
}
}

int search_value(struct NODE *llist, int num) {
int retval = -1;
int i = 1;

while(llist->next != NULL)
{

if(llist->next->number == num)
return i;
else
i++;

llist = llist->next;
}

return retval;
}

Program to Add the two long positive intergers

#include"stdio.h"
#include"alloc.h"
#include"conio.h"
#include"ctype.h"
struct node
{
int data;
struct node*next;
};
void insert(struct node**p,int num)
{
struct node*temp;
if(*p==NULL)
{
(*p)=(struct node*)malloc(sizeof(struct node));
(*p)->next=NULL;
(*p)->data=num;
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
temp->next=(*p);
(*p)=temp;
(*p)->data=num;
}
}


void add_in(struct node*a,struct node*b,struct node**c)
{
int d,carry;
carry=0;
struct node*t;
while(a!=NULL&&b!=NULL)
{
d=(a->data+b->data+carry)%10;
insert(c,d);
if( (a->data+b->data+carry) >= 10)
{
carry=1;
}
else carry=0;
a=a->next;
b=b->next;
}
if(a==NULL&&b==NULL)
{
return;
}
else
{
if(a!=NULL&&b==NULL)
{
t=a;
}
else
{
t=b;
}
while(t!=NULL)
{
d=(carry+t->data)%10;
if((carry+t->data)>=10)
carry=1;
else
carry=0;
insert(c,d);
t=t->next;
}
if(carry==1)
insert(c,carry);
}
}
void numin(struct node**p)
{
*p=NULL;char c='c';
while(c!='n')
{
c=getch();
if(!isdigit(c))
return;
else
{
putch(c);
insert(p,c-'0');
}
}
}
void disp(struct node*p)
{
if(p==NULL)
return;
else
{
printf("%d",p->data);
disp(p->next);
}

}
void main()
{
struct node *a,*b,*c;
clrscr();
a=b=c=NULL;
printf("Enter the first number....");
numin(&a);
printf("Enter the second number....");
numin(&b);
printf("The added result is...");
add_in(a,b,&c);
disp(c);
getch();
}

Program to write even and odd integers into different files


#include "stdio.h"

#include "math.h"
void main()
{
FILE *all,*even,*odd;
int number,i,records;
printf("INPUT THE TOTAL NUMBER OF RECORDS THAT U WANT TO ENTER");
scanf("%d",& records);
all=fopen("ANYNUMBER","w");
for(i=1;i<=records;i++)
{
scanf("%d",&number);
if(number==-1)break;
putw(number,all);
}
fclose(all);
all=fopen("ANYNUMBER","r");
even=fopen("EVENNUMBER","w");
odd=fopen("ODDNUMBER","w");
while((number=getw(all))!=EOF)
{
if(number%2==0)
putw(number,even);
else
putw(number,odd);
}
fclose(all);
fclose(even);
fclose(odd);


even=fopen("EVENNUMBER","r");
odd=fopen("ODDNUMBER","r");
printf(" THE EVEN NUMBERS ARE");
while((number=getw(even))!=EOF)
printf(" %4d",number);
printf(" THE ODD NUMBERS ARE");
while((number=getw(odd))!=EOF)
printf(" %4d",number);

fclose(even);
fclose(odd);
}

Create distinct wordlist sorted on frequency

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

#define MAXLINE 1024
#define MAXTOKENS 256
#define MINLEN 3

struct tnode
{
char *word;
int count;
struct tnode *left, *right;
};

char **split(char *, char *);
struct tnode *addtree(struct tnode *, char *);
struct tnode *talloc(void);
void treeprint(struct tnode *);
void treefree(struct tnode *);
void arrayprint(struct tnode **, int);
int compare(const void *, const void *);
int tnodecount(struct tnode *);
int treetoarray(struct tnode *, struct tnode **);

int main(void)
{
char *delim = ".,:;`'\"+-_(){}[]<>*&^%$#@!?~/|\\= \t\r\n1234567890";
char **tokens = NULL;
char line[MAXLINE];
struct tnode *root = {0};
struct tnode **tarray = {0};
int i, len, lcount, ncount;

i = len = lcount = ncount = 0;

while(fgets(line, MAXLINE, stdin) != NULL)
{
len = strlen(line);
if(len < MINLEN) continue; else lcount++;
if(line[len - 1] == '\n')
line[--len] = '\0';
tokens = split(line, delim);
for(i = 0; tokens[i] != NULL; i++)
root = addtree(root, tokens[i]);
for(i = 0; tokens[i] != NULL; i++)
free(tokens[i]); free(tokens);
}
ncount = tnodecount(root);
if(ncount == 0)
return 1;
/* treeprint(root); */
tarray = malloc(ncount * sizeof(struct tnode *));
if(tarray == NULL) return 1;
if(treetoarray(root, tarray) == -1)
return 1;
qsort(tarray, ncount, sizeof(struct tnode *), compare); arrayprint(tarray, ncount); treefree(root);
return 0;
}
struct tnode *addtree(struct tnode *p, char *w)
{
int cond; if(p == NULL)
{ p = talloc();
p->word = strdup(w);

p->count = 1;
p->left = p->right = NULL;
} else if((cond = strcmp(w, p->word)) == 0)
p->count++;
else
if(cond <>left = addtree(p->left, w);

else
p->right = addtree(p->right, w);

return p;
}

struct tnode *talloc(void)
{
return(struct tnode *)malloc(sizeof(struct tnode));
}

void treeprint(struct tnode *p)
{
if(p != NULL)
{
treeprint(p->left);
printf("%4d %s\n", p->count, p->word);
treeprint(p->right);
}
}

void treefree(struct tnode *p)
{
if(p != NULL)
{
treefree(p->left);
treefree(p->right);

free(p->word);
free(p);
}

return;
}

void arrayprint(struct tnode **p, int x)
{
int i = 0;

for(i = 0; i < x; i++)
printf("%4d %s\n", p[i]->count, p[i]->word);


return;
}

char **split(char *string, char *delim)
{
char **tokens = NULL;
char *working = NULL;
char *token = NULL;
int idx = 0;

tokens = malloc(sizeof(char *) * MAXTOKENS);
if(tokens == NULL)
return NULL;
working = malloc(sizeof(char) * strlen(string) + 1);
if(working == NULL)
return NULL;

strcpy(working, string);
for(idx = 0; idx < MAXTOKENS; idx++)
tokens[idx] = NULL;
token = strtok(working, delim);
idx = 0; while((idx < (MAXTOKENS - 1)) && (token != NULL))
{
tokens[idx] = malloc(sizeof(char) * strlen(token) + 1); if(tokens[idx] != NULL)
{
strcpy(tokens[idx], token);
idx++; token = strtok(NULL, delim);
}
}
free(working); return tokens;
}
int treetoarray(struct tnode *tree, struct tnode **array)
{
static struct tnode **write = NULL;
if(tree == NULL) return -1;
if(array != NULL) write = array;
if(tree != NULL)
{
*write = tree, write++;
if(tree->left != NULL)

treetoarray(tree->left, NULL);
if(tree->right != NULL)
treetoarray(tree->right, NULL);
}

return 0;
}

int compare(const void *x, const void *y)
{
struct tnode * const *a = x;
struct tnode * const *b = y;

if(a == NULL || b == NULL)
return -2;
else {
if((*a)->count < (*b)->count)
return 1;
else if((*a)->count > (*b)->count)
return -1;
else
return 0;
}
}

int tnodecount(struct tnode *p)
{
if(p == NULL)
return 0;
else {
if(p->left == NULL && p->right == NULL)
return 1;
else
return(1 + (tnodecount(p->left) + tnodecount(p->right)));
}
}

Program to Extract character ngrams from textdata

#include
#include
#include

#define MAXLINE 1024
#define MINLEN 3

/* print all ngrams for `str' */
void printgrams(char *, int);
/* padd token to, one prefix, and (N - 1) affix */
char *mkpadgr(char *, char *, int);

int main(int argc, char *argv[])
{

char *delim = ".,:;`'\"+-_(){}[]<>*&^%$#@!?~/|\\= \t\r\n1234567890";
char *token = NULL;
char line[MAXLINE];
int nglen, i;

i = nglen = 0;

if(argc != 2)
{

fprintf(stderr, "Usage: chargram INT\n");
return 1;
}
else

nglen = atoi(argv[1]);

while(fgets(line, MAXLINE, stdin) != NULL)
{

if(strlen(line) <>
continue;

token = strtok(line, delim);
while(token != NULL)
{

printgrams(token, nglen);
token = strtok(NULL, delim);
}
}

return 0;
}

/* print all ngrams for `str' */
void printgrams(char *str, int N) {
char *padded = NULL;
char *gram = NULL;
int i = 0, j = 0;

padded = mkpadgr(str, "_", N);

for(i = 0; i <>
gram = &padded[i];
for(j = 0; j <>
printf("%c", gram[j]);
}
printf("\n");
}

free(padded);
return;
}

/* padd word according to one prefix, and (N - 1) affix */
char *mkpadgr(char *str, char *padd, int N) {
char *buff = NULL;
int i = 0;

buff = calloc(strlen(str) + 2 + (N - 1), sizeof(char));
strcat(buff, padd), strcat(buff, str);
for(i = 0; i < (N - 1); i++)
strcat(buff, padd);

return buff;
}

Filter text with a stop word list

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

#define MAXTOKENS 256
#define MAXLINE 1024
#define MINLEN 3
#define STMINLEN 2

struct tnode
{
char *word;
int count;
struct tnode *left, *right;
};

struct tnode *buildstoptree(char *, struct tnode *);
struct tnode *addtree(struct tnode *, char *);
struct tnode *findstopword(struct tnode *, char *);
struct tnode *talloc(void);
void freetree(struct tnode *);
char **split(char *, char *);

int main(int argc, char *argv[])
{
/* delim does not include \' [\047] quote */
char *delim = ".,:;`\"+-_(){}[]<>*&^%$#@!?~/|\\= \t\r\n1234567890";
char **tokens = NULL;
struct tnode *root = {0};
struct tnode *querry = {0};
char line[MAXLINE];
int i = 0;

if(argc != 2) {
fprintf(stderr, "Usage: tokstop STOPLIST.txt\n");
return 1;
}

root = buildstoptree(argv[1], root);
if(root == NULL)
return 1;

while(fgets(line, MAXLINE, stdin) != NULL) {
if(strlen(line) < MINLEN)
continue;

tokens = split(line, delim);
for(i = 0; tokens[i] != NULL; i++) {
querry = findstopword(root, tokens[i]);
if(querry == NULL)
printf("%s ", tokens[i]);
}

for(i = 0; tokens[i] != NULL; i++)
free(tokens[i]);
free(tokens[i]);
printf("\n");
}

freetree(root);
return 0;
}

/* read stoplist into binary tree, expects one entry per line */
struct tnode *buildstoptree(char *fname, struct tnode *p) {
FILE *fp = {0};
char line[MAXLINE];
int len = 0, lcount = 0;

fp = fopen(fname, "r");
if(fp == NULL) {
fprintf(stderr, "Error - fopen(%s)\n", fname);
return NULL;
}

while(fgets(line, MAXLINE, fp) != NULL) {
len = strlen(line);
if(len < STMINLEN)
continue;
else
lcount++;

if(line[len - 1] == '\n')
line[--len] = '\0';

p = addtree(p, line);
}

if(lcount == 0) {
fprintf(stderr, "Error - Zero stopwords..\n");
return NULL;
}

fclose(fp);
return p;
}

/* split string into tokens, return token array */
char **split(char *string, char *delim) {
char **tokens = NULL;
char *working = NULL;
char *token = NULL;
int idx = 0;

tokens = malloc(sizeof(char *) * MAXTOKENS);
if(tokens == NULL)
return NULL;
working = malloc(sizeof(char) * strlen(string) + 1);
if(working == NULL)
return NULL;

/* to make sure, copy string to a safe place */
strcpy(working, string);
for(idx = 0; idx < MAXTOKENS; idx++)
tokens[idx] = NULL;

token = strtok(working, delim);
idx = 0;

/* always keep the last entry NULL terminated */
while((idx < (MAXTOKENS - 1)) && (token != NULL)) {
tokens[idx] = malloc(sizeof(char) * strlen(token) + 1);
if(tokens[idx] != NULL) {
strcpy(tokens[idx], token);
idx++;
token = strtok(NULL, delim);
}
}

free(working);
return tokens;
}

/* install word in binary tree */
struct tnode *addtree(struct tnode *p, char *w) {
int cond;

if(p == NULL) {
p = talloc();
p->word = strdup(w);
p->count = 1;
p->left = p->right = NULL;
} else if((cond = strcmp(w, p->word)) == 0)
p->count++;
else if(cond < 0)
p->left = addtree(p->left, w);
else
p->right = addtree(p->right, w);

return p;
}

/* make new tnode */
struct tnode *talloc(void) {
return(struct tnode *)malloc(sizeof(struct tnode));
}

/* find value w in binary tree */
struct tnode *findstopword(struct tnode *p, char *w) {
struct tnode *temp;
int cond = 0;

temp = p;

while(temp != NULL) {
if((cond = strcmp(temp->word, w)) == 0)
return temp;
else if(cond > 0)
temp = temp->left;
else
temp = temp->right;
}

return NULL;
}

/* free binary tree */
void freetree(struct tnode *p) {
if(p != NULL) {
free(p->left);
free(p->right);

free(p->word);
free(p);
}
}

Folder Protection Software

#include "stdio.h"
#include "string.h"
#include "process.h"
void main()
{
FILE *p;
char ch,s[100];
char r[100]="REN ";

char u[]=".{21EC2020-3AEA-1069-A2DD-08002B30309D} ";
char v[50];
int choice,i;

clrscr();
p=fopen("C:\a.bat","w+");
if(p==NULL)
{
printf("Error in opening the file a.c");
exit(0);
}


printf("This software can convert your File/Folder to Control
Panel and can Restore again.");
printf("Enter the path of the file");
fflush(stdin);
gets(s);

for(i=0;i<25;i++)
fputs("echo This software is not responsible for any loss in
data",p);

printf("Enter choice :");
printf("1.Protect Folder/File");
printf("2.Unprotect folder/File");
printf("3.Exit");
scanf("%d",&choice);


switch(choice)
{
case 1:
strcat(r,s);
printf("Enter new name of your folder/file");
fflush(stdin);
gets(v);
strcat(r," ");
strcat(r,v);
strcat(r,u);
break;
case 2:
strcat(r,s);
strcat(r,u);
printf("Enter new name of your folder/file");
fflush(stdin);
gets(v);
strcat(r,v);
break;
default:
fclose(p);
remove("C:\a.bat");

exit(0);
}



{
fputs(r,p);
for(i=0;i<25;i++)
fputs("
echo This software is not responsible for any loss in data
",p);
fputs("
exit",p);

}


fclose(p);

system("C:\a.bat");


remove("C:\a.bat");

}

Program to Generate a color palette

#include "stdio.h"
#include "error.h"
#include "math.h"
#include "gd.h"

#define RGB2INT(r,g,b) (b + 256 * g + 256 * 256 * r)

int main(int argc, char *argv[])
{
FILE *fp = {0};
gdImagePtr img = {0};
char *fname = NULL;
int x, y, w, h, color;
int red, green, blue;
int palette[256] = {0};

if(argc != 2)
error(1, 0, "Usage: gdpalette image.png");
else
fname = argv[1];

w = 256, h = 100;
img = gdImageCreateTrueColor(w, h);

for(x = 0; x < 256; x++)
{
red = (int)(128.0 + 128 * sin(3.1415 * x / 16.0));
green = (int)(128.0 + 128 * sin(3.1415 * x / 32.0));
blue = (int)(128.0 + 128 * sin(3.1415 * x / 64.0));
palette[x] = RGB2INT(red, green, blue);
}

for(x = 0; x < w; x++)
for(y = 0; y < h; y++)
{
red = gdImageRed(img, palette[x]);
green = gdImageGreen(img, palette[x]);
blue = gdImageBlue(img, palette[x]);
color = gdImageColorAllocate(img, red, green, blue);
gdImageSetPixel(img, x, y, color);
}

if((fp = fopen(fname, "w")) == NULL)
error(1, 0, "Error - fopen(): %s", fname);
else
{
gdImagePng(img, fp);
fclose(fp);
}

gdImageDestroy(img);
return 0;
}

Program to Pick random values

#include "time.h"
#include "stdio.h"
#include "fcntl.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
#include "getopt.h"
#include "limits.h"
#include "sys/stat.h"
#include "sys/types.h"

#define PACKAGE "raval"
#define VERSION "0.0.1"

/* status epilepticus, print help and exit with `exval' */
void print_help(int exval);
/* picks a radom value withing range low-high*/
int get_rand_val(int low, int high);
/* ! definitely not cryptographic secure ! */
/* value returned seeds the rand() function */
unsigned time_seed(void);

int main(int argc, char *argv[]) {
int opt = 0; /* holds option */
int max = 10; /* upper limit */
int min = 0; /* minimum limit */
int many = 1; /* the number of numbers to pick ... */
char *ptr = NULL; /* separator for number range */

while((opt = getopt(argc, argv, "hvn:r:")) != -1) {
switch(opt) {
case 'h':
print_help(0);
break;
case 'v':
exit(0);
case 'n':
many = atoi(optarg);
break;
case 'r':
if((ptr = strchr(optarg, ':')) == NULL) {
fprintf(stderr, "%s: Error - range `LOW:HIGH'\n\n", PACKAGE);
print_help(1);
} else {
ptr++, max = atoi(ptr);
ptr--, ptr = '\0';
min = atoi(optarg);
if(min >= max || min < 0 || max < 0) {
fprintf(stderr, "%s: Error - range `LOW:HIGH'\n\n", PACKAGE);
print_help(1);
}
}
break;
case '?':
fprintf(stderr, "%s: Error - No such option: `%c'\n\n", PACKAGE, optopt);
print_help(1);
case ':':
fprintf(stderr, "%s: Error - option `%c' needs an argument\n\n", PACKAGE, optopt);
print_help(1);
}
}

/* first seed the random function */
srand((time_seed()));

/* print the random values */
for(; many > 0; many--)
printf("%4d\n", get_rand_val(min, max));

return 0;
}

/* picks a radom value withing range low-high*/
int get_rand_val(int low, int high) {
int k = 0;
double d = 0;

d = (double)rand() / ((double)RAND_MAX + 1);
k = (int)(d * (high - low + 1));

return(low + k);
}

/* ! definitely not cryptographic secure ! */
/* value returned seeds the rand() function */
unsigned time_seed(void) {
int retval = 0;
int fd;

/* just in case open() fails.. */
if(open("/dev/urandom", O_RDONLY) == -1) {
retval = (((int)time(NULL)) & ((1 << 30) - 1)) + getpid();
} else {
read(fd, &retval, 4);
/* positive values only */
retval = abs(retval) + getpid();
close(fd);
}

return retval;
}

void print_help(int exval) {
printf("%s,%s print a random number\n", PACKAGE, VERSION);
printf("Usage: %s OPTION...\n\n", PACKAGE);

printf(" -h print this help and exit\n");
printf(" -v print version and exit\n\n");

printf(" -n INT return `INT' numbers\n");
printf(" -r INT:INT keep the number within range `LOW:HIGH',\n");
printf(" default=(1:10)\n\n");


exit(exval);
}

Positional notation of numbers

#include "stdio.h"
#include "math.h"

#define PACKAGE "digits"

int number_of_digits(int x);
void print_help(void);

int main(int argc, char *argv[]) {
int n; /* input; the number to convert */
short base; /* input; base to which we will convert n */
short rhdigit; /* right-hand digit of n-prime */
int power; /* loop */

if(argc != 3) {
print_help();
return 1;
} else {
sscanf(argv[1], "%d", &n);
if(n <> 10) {
fprintf(stderr, "%s: Error: Base is not in required range.", PACKAGE);
return 1 ;
}
} /* else */

printf("The number %d, is %d wide, ", n, number_of_digits(n));
printf("and contains the numbers: ");

/*
// Generate digits of converted number, right to left.
*/
for(power = 0; n != 0; power++) {
rhdigit = n % base; /* Isolate right-hand digit of n. */
n /= base; /* then eliminate right-hand digit. */

printf("%hi ", rhdigit);
}

printf("\n");
return 0;
}

int number_of_digits(int x) {
return x ? (int)(log10((double)(abs(x))))+1 : 1;
}

void print_help(void) {
fprintf(stdout, "Usage : %s [NUMBER] [BASE]\n", PACKAGE);
fprintf(stdout, "Example: %s 4591 10\n", PACKAGE);
}

Program to Print several types of alphabets

#include "stdio.h"
#include "ctype.h"
#include "getopt.h"
#define PACKAGE "alphabet"
#define VERSION "0.0.5"
/*
// `0' westeren, `1' phonetic, `2' greek `3' hebrew
// `4' phoenician `5' arab
*/
static int type = 0;
/* all output upper case */
static int upper = 0;
/* recursive, output until killed */
static int recur = 0;

/* phonetic alphabet */
const char *palpha[]= { "alpha", "bravo", "charlie", "delta",
"echo", "foxtrot", "golf", "hotel",
"india", "juliet", "kilo", "lima",
"mike", "november", "oscar", "papa",
"quebec", "romeo", "sierra", "tango",
"uniform", "victor", "whisky", "x-ray",
"yankee", "zulu" };

/* lower case greek alphabet */
const char *galpha[]= { "alpha", "beta", "gamma", "delta",
"epsilon", "zeta", "eta", "theta",
"iota", "kappa", "lambda", "mu",
"nu", "xi", "omicron", "pi",
"rho", "sigma", "tau", "upsilon",
"phi", "chi", "psi", "omega" };

/* lower case hebrew alphabet */
const char *halpha[]= { "tet", "chet", "zayin", "vav",
"he", "dalet", "gimel", "bet",
"alef", "samech", "nun", "nun",
"mem", "mem", "lamed", "khaf",
"kaf", "yod", "tav", "shin",
"resh", "qof", "tzade", "tzade",
"fe", "pe", "ayin" };

/* lower case phoenician alphabet */
const char *nalpha[]= { "aleph", "beth", "gimel", "daleth",
"he", "waw", "heth", "yodh",
"kaph", "lamedh", "mem", "nun",
"ayin", "pe", "qoph", "resh",
"sin", "taw", "waw", "samekh",
"zayin" };

/* lower case arab alphabet */
const char *aalpha[]= { "xaa", "haa", "jiim", "th!aa",
"taa", "baa", "alif", "saad",
"shiin", "siin", "zaay", "raa",
"thaal", "daal", "qaaf", "faa",
"ghayn", "ayn", "th:aa", "taa",
"daad", "yaa", "waaw", "haa",
"nuun", "miim", "laam", "kaaf" };

/* status epilepticus, and die with `exval' */
void print_help(int exval);
/* print the specified alphabet */
void print_alphabet(void);

int main(int argc, char *argv[]) {
int opt = 0;

/* option parser */
while((opt = getopt(argc, argv, "hvpgwiaru")) != -1) {
switch(opt) {
case 'h': /* print help and exit */
print_help(0);
case 'v': /* print version and exit */
exit(0);
case 'p': /* print the phonetic alphabet */
type = 1;
break;
case 'g': /* print the lower case greek alphabet */
type = 2;
break;
case 'w': /* print the lower case hebrew alphabet */
type = 3;
break;
case 'i': /* print the lower case phoenician alphabet */
type = 4;
break;
case 'a': /* print the lower case arab alphabet */
type = 5;
break;
case 'r': /* print the alphabet repeatedly until killed */
recur = 1;
break;
case 'u': /* print all output upper case */
upper = 1;
break;
case '?': /* unkown option */
fprintf(stderr, "%s: Error - no such option `%c'\n\n", PACKAGE, optopt);
print_help(1);
break;
} /* switch */
} /* while */

/* prints the selected alphabet */
print_alphabet();

return 0;
}

/* prints the selected alphabet */
void print_alphabet(void) {
const char *ptr = NULL;
int i = 0;

/* western alphabet */
if(type == 0) {
if(upper == 0)
for(i = 97; i <= 122; i++)
printf("%c\n", i);
else
for(i = 65; i <= 90; i++)
printf("%c\n", i);
/* phonetic alphabet */
} else if(type == 1) {
if(upper == 0) {
for(i = 0; i <>
printf("%s\n", palpha[i]);
} else {
for(i = 0; i <>
ptr = palpha[i];
while(*ptr)
printf("%c", toupper(*ptr++));
printf("\n");
} /* for */
} /* else */
/* greek alphabet */
} else if(type == 2) {
if(upper == 0) {
for(i = 0; i <>
printf("%s\n", galpha[i]);
} else {
for(i = 0; i <>
ptr = galpha[i];
while(*ptr)
printf("%c", toupper(*ptr++));
printf("\n");
} /* for */
} /* else */
} else if(type == 3) {
if(upper == 0) {
for(i = 0; i <>
printf("%s\n", halpha[i]);
} else {
for(i = 0; i <>
ptr = halpha[i];
while(*ptr)
printf("%c", toupper(*ptr++));
printf("\n");
} /* for */
} /* else */
} else if(type == 4) {
if(upper == 0) {
for(i = 0; i <>
printf("%s\n", nalpha[i]);
} else {
for(i = 0; i <>
ptr = nalpha[i];
while(*ptr)
printf("%c", toupper(*ptr++));
printf("\n");
} /* for */
} /* else */
} else if(type == 5) {
if(upper == 0) {
for(i = 0; i <>
printf("%s\n", aalpha[i]);
} else {
for(i = 0; i <>
ptr = aalpha[i];
while(*ptr)
printf("%c", toupper(*ptr++));
printf("\n");
} /* for */
} /* else */
} /* else if */

if(recur == 1)
print_alphabet();
else
return;
}

/* status epilepticus, and die with `exval' */
void print_help(int exval) {
printf("%s,%s prints different alphabets\n", PACKAGE, VERSION);
printf("Usage: %s [-h] [-v] [-p] [-g] [-w] [-i] [-a] [-r] [-u]\n\n", PACKAGE);

printf(" Startup:\n");
printf(" -h print this help and exit\n");
printf(" -v print version and exit\n\n");

printf(" Alphabet:\n");
printf(" -p print the phonetic alphabet\n");
printf(" -g print the greek alphabet\n");
printf(" -w print the hebrew alphabet\n");
printf(" -i print the phoenician alphabet\n");
printf(" -a print the arab alphabet\n\n");

printf(" Output:\n");
printf(" -r print the alphabet repeatedly until killed\n");
printf(" -u print all output in upper case\n\n");

printf(" Per default the Westeren alphabet is printed\n");
exit(exval);
}