Program to sort the marks in asending order


#include
void sort(int m, int x[]);
main()
{

int i;
int marks[5]={40, 90, 73, 81, 35};
clrscr();
printf("Marks before sorting\n");
for(i=0; i<5; i="0;" i="1;" j="1;j<="m-1;j++)">=x[j]
{
temp=x[j-1];
x[j-1]=x[j];
x[j]=temp;
}
}
}
}




Related Links :

Program to find the difference between given two numbers




#include
float ratio(int x, int y, int z);
int difference(int x, int y);
main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%f \n", ratio(a,b,c));
getch();
}

float ratio(int x, int y, int z)
{
if(difference(y,z))
return(x/(y-z));
else
return(0.0);
}

int difference(int p, int q)
{
if(p!=q)
return(1);
else
return(0);
}


Related Links :

Program to calculate salseman salary in c



#include
#define MAXSALESMAN 4
#define MAXITEMS 3
main()
{
int value[MAXSALESMAN][MAXITEMS];
int salesman_total[MAXSALESMAN], item_total[MAXITEMS];
int i, j, grand_total;
clrscr();
printf("Input data\n");
printf("Enter values, one at a time, row-wise\n\n");
for(i=0; i<=MAXSALESMAN; i++)
{
salesman_total[i]=0
for(j=0; j<=MAXITEMS; j++)
{
scanf("%d", &value[i][j]);
salesman_total[i]=salesman_total[i]+value[i][j];
}
}
for(j=0; j<=MAXITEMS; j++)
{
item_total[j]=0;
for(i=0; i<=MAXSALESMAN; i++)
item_total[j]+=value[i][j];
}
grand_total=0;
for(i=0; i<=MAXSALESMAN; i++)
grand_total+=salesman_total[i];
printf("\n Salesmans Total\n\n");
for(i=0; i<=MAXSALESMAN; i++)
printf("Salesman[%d]=%d\n",i+1,salesman_total[i]);
printf("Items Totals\n\n");
for(j=0; j<=MAXITEMS; j++)
printf("Item[%]=%\n", j+1, item_total[j]);
printf("\nGrand Total=%d\n", grand_total);
getch();
}


Related Links :

Program to create a group of given number by range & frequency


#include
#define MAXVAL 50
#define COUNTER 11
main()
{
float value[MAXVAL]
int i, low, high;
int group[COUNTER]={0,0,0,0,0,0,0,0,0,0,0};
clrscr();
for(i=0; i scanf("%f", &value[i]);
printf("\n");
printf("Group\tRange\tFrequency\n\n");
for(i=0; i{
low=i*10;
if(i==10)
high=100;
else
high=low+9;
printf("%2d\t%3d to %3d\t%d\n", i+1, low, high, group[i]);
}
getch();
}

Related Links :

Program to create a file on disk & save a data by using fopen function in c


#include
Main()
{
FILE *f1;
char c;
printf("Data Input\n");

f1=fopen("INPUT","w");
while((c=getchar())!=EOF)
{
putc(c,f1);
}
fclose(f1);

f1=fopen("INPUT","r");
while((c=getc(f1))!=EOF)
{
printf("%c", c);
}
fclose(f1);



Related Links :

Program to print Current time & day in C


#include
#include
int current_day(void)
{ struct tm *local;
time_t t;

t = time(NULL);
local = localtime(&t);
return local->tm_wday;
}
int main(int argc, char *argv[])
{
printf("Today: %d\n", current_day());
exit(0);
}



Related Links :

Program to show working of Pointers in C & explain Concept of Pointers


main()
{
int x, *p, y;
clrscr();
x=10;
p=&x;
y=*p;
printf("Value of X: %d\nValue of P: %u\nValue of Y: %d",x,p,y);
printf("\nAddress of X: %u\nAddress of P: %u\nAddress of Y: %u",&x,&p,&y);
*p=20;
printf("\nNow Value of X: %d and Y: %d",x,y);
getch();
}

Related Links :

Program to show Nested User Defined Function in C


#include
int func1(void);
int func2(void);
int func3(void);
int x;
main()
{
x=10;
printf("Starting Value of x= %d\n", x);
prinf("x= %d\n", func1());
prinf("x= %d\n", func2());

prinf("x= %d\n", func3());
getch();
}
func1(void)
{
x+=10;
}
int func2(void)
{
int x;
x=1;
return(x);
}
func3(void)
{
x=x+10;
}



Related Links :

Program to show an example of Passing Arguments to Function.



#include

/* functions declaration */

/* demonstrate pass by pointer */
void swap(int *x, int *y);

/* demonstrate pass by value */
void swap(int x, int y);

/* demonstrate pass an array to the function */
void bubble_sort(int a[], int size);

void print_array(int a[],int size);

void main()
{
int x = 10;
int y = 20;


printf("x,y before swapping\n");
printf("x = %d\n",x);
printf("y = %d\n",y);

// pass by value
swap(x,y);

printf("x,y after swapping using pass by value\n");
printf("x = %d\n",x);
printf("y = %d\n",y);

// pass by pointer
swap(&x,&y);

printf("x,y after swapping using pass by pointer\n");
printf("x = %d\n",x);
printf("y = %d\n",y);

// declare an array
const int size = 5;
int a[size] = {1,3,2,5,4};

printf("array before sorting\n");
print_array(a,size);

bubble_sort(a,size);

printf("array after sorting\n");
print_array(a,size);


}


/* functions implementation */

void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}

void swap(int x, int y){
int temp = x;
x = y;
y = temp;
}

void bubble_sort(int a[], int size)
{
int i,j;
for(i=0;i<(size-1);i++)
for(j=0;j<(size-(i+1));j++)
if(a[j] > a[j+1])
swap(&a[j],&a[j+1]);
}



void print_array(int a[],int size)
{

for(int i = 0;i < size; i++)
{
printf("%d\t",a[i]);
printf("\n");
}
}

Related Links :

Huffman Coding in C



#include

typedef struct qnode qnode;
typedef tree qtype;
typedef struct
{
qnode *front;
qnode *rear;
} queue;

struct qnode
{
qtype op;
qnode *next;
};

bool qEmpty( queue *q )
{
return (q->front == NULL);
}

void qPushBack( queue *q, qtype op )
{
/*
* pushes op at q.rear.
*/
qnode *ptr = (qnode *)malloc( sizeof(qnode) );
ptr->op = op;
ptr->next = NULL;
if( qEmpty(q) ) // first element in q.
q->front = ptr;
else
q->rear->next = ptr;
q->rear = ptr;
}

void qInsertSorted(queue *q, qtype op)
{
/*
* inserts val in sorted q and keeps new q sorted.
*/
qnode *ptr = (qnode *)malloc(sizeof(qnode));
qnode *curr, *prev;
ptr->op = op;

for(prev=NULL, curr=q->front; curr && curr->op->w < op->w; prev=curr, curr=curr->next)
;
if(!curr && !prev) // q empty.
ptr->next = NULL, q->rear = q->front = ptr;
else if(!curr) // op is the max value.
ptr->next = NULL, prev->next = q->rear = ptr;
else if(!prev) // op is the min value.
ptr->next = curr, q->front = ptr;
else { // if prev and ptr both exist.
ptr->next = curr;
prev->next = ptr;
}
}

qtype qPopFront( queue *q ) {
/*
* pops op from q->front.
*/
qnode *ptr = q->front;
qtype op;

if( qEmpty(q) )
return (qtype)NULL;
q->front = q->front->next;
if( qEmpty(q) ) q->rear = NULL;

op = ptr->op;
free(ptr);
return op;
}

/********************* huffman.c *************************/
#include

#define MAXLEN 80

typedef struct node node;
typedef char type;
typedef node *tree;
typedef enum {FALSE, TRUE} bool;

struct node {
int w;
type val;
node *left, *right;
};

#include "huffman.q.h"

int compare(const void *e1, const void *e2) {
/*
* compare the two elements in e1 and e2.
* each element is a vector of two elements.
*/
return ((int *)e1)[1] > ((int *)e2)[1];
}

void printTree(tree t, char *outputstr) {
/*
* print the huffman codes for each element of t.
* outputstr contains huffman code for t (NOT parent of t).
* assumes t!=NULL.
*/
char str[2] = "1";

if(t->right) {
strcat(outputstr, str);
printTree(t->right, outputstr);
outputstr[strlen(outputstr)-1] = 0; // restore.
}
if(t->left) {
str[0] = '0';
strcat(outputstr, str);
printTree(t->left, outputstr);
outputstr[strlen(outputstr)-1] = 0; // restore.
}
else if(!t->right)
printf("%c=%d=%s.\n", t->val, t->w, outputstr);
}

tree buildTree(int a[][2], int n) {
/*
* build a huffman tree using frequency in a[i][1] where a[0][j] indicates
* the character
* for that sort a on frequency.
* n is the size of a.
*/
int i;
tree t = NULL;
queue sortedq = {NULL};

// sort a on frequency.
qsort(a, n, sizeof(a[0]), compare);

// insert each element in tree.
for(i=0; iw = a[i][1];
temp->val = (type)a[i][0];
qPushBack(&sortedq, temp);
}
// assume n>0.
while(TRUE) {
tree t2 = NULL,
t1 = qPopFront(&sortedq);
if(!qEmpty(&sortedq))
t2 = qPopFront(&sortedq);
else {
t = t1;
break;
}

t = (tree)malloc(sizeof(node));
t->w = t1->w + t2->w;
t->left = t1;
t->right = t2;
{
qnode *ptr;
for(ptr=sortedq.front; ptr; ptr=ptr->next)
printf("%d ", ptr->op->w);
printf("\n");
}
printf("insertsorted=%d.\n", t->w);
qInsertSorted(&sortedq, t);
}
return t;
}

int main() {
int a[][2] = { {'a', 20},
{'b', 23},
{'c', 14},
{'d', 56},
{'e', 35},
{'f', 29},
{'g', 5 }
};
char outputstr[MAXLEN] = "";
tree t = buildTree(a, sizeof(a)/2/sizeof(int));

if(t)
printTree(t, outputstr);

return 0;
}

Related Links :

Program Calculate Edit Distance between Two Strings




#include

#define MAXLEN 80

int findMin(int d1, int d2, int d3)
{
/*
* return min of d1, d2 and d3.
*/
if(d1 < d2 && d1 < d3)
return d1;
else if(d1 < d3)
return d2;
else if(d2 < d3)
return d2;
else
return d3;
}

int findEditDistance(char *s1, char *s2)
{
/*
* returns edit distance between s1 and s2.
*/
int d1, d2, d3;

if(*s1 == 0)
return strlen(s2);
if(*s2 == 0)
return strlen(s1);
if(*s1 == *s2)
d1 = findEditDistance(s1+1, s2+1);
else
d1 = 1 + findEditDistance(s1+1, s2+1); // update.
d2 = 1+findEditDistance(s1, s2+1); // insert.
d3 = 1+findEditDistance(s1+1, s2); // delete.

return findMin(d1, d2, d3);
}

int main() {
char s1[MAXLEN], s2[MAXLEN];

printf("Enter string 1: ");
gets(s1);

while(*s1)
{
printf("Enter string 2: ");
gets(s2);
printf("Edit distance(%s, %s) = %d.\n", s1, s2, findEditDistance(s1, s2));
printf("Enter string 1(enter to end): ");
gets(s1);
}

return 0;
}

Related Links :

Introduce to Queue and Array Implementation of Queue


#include
#include
#include "queue.h"
#define size 3
void main(){
int head,tail,element;
int queue[size];
// initialize queue
init(&head,&tail);
// enqueue elements
while(full(tail,size) != 1){
element = rand();
printf("enqueue element
%d\n",element);
enqueue(queue,&tail,element);
printf("head=%d,tail=%d\n",head,tail);
//press enter to enqueue more
getchar();
}
printf("queue is full\n");
// dequeue elements from
while(!empty(head,tail)){
element = dequeue(queue,&head);
printf("dequeue element %d \n",element)
//press enter to pop more
getchar();
}
printf("queue is empty\n");
getchar();
}


Related Links :

Implement Singly-linked List in C


#include
#include

struct node{
int data;
struct node *next;
};

struct node* add(struct node *head, int data){
struct node *tmp;

if(head == NULL){
head=(struct node *)malloc(sizeof(struct node));
if(head == NULL){
printf("Error! memory is not available\n");
exit(0);
}
head-> data = data;
head-> next = head;
}else{
tmp = head;

while (tmp-> next != head)
tmp = tmp-> next;
tmp-> next = (struct node *)malloc(sizeof(struct node));
if(tmp -> next == NULL)
{
printf("Error! memory is not available\n");
exit(0);
}
tmp = tmp-> next;
tmp-> data = data;
tmp-> next = head;
}
return head;
}

void printlist(struct node *head)
{
struct node *current;
current = head;
if(current!= NULL)
{
do
{
printf("%d\t",current->data);
current = current->next;
} while (current!= head);
printf("\n");
}
else
printf("The list is empty\n");

}

void destroy(struct node *head)
{
struct node *current, *tmp;

current = head->next;
head->next = NULL;
while(current != NULL) {
tmp = current->next;
free(current);
current = tmp;
}
}
void main()
{
struct node *head = NULL;
head = add(head,1); /* 1 */
printlist(head);

head = add(head,20);/* 20 */
printlist(head);

head = add(head,10);/* 1 20 10 */
printlist(head);

head = add(head,5); /* 1 20 10 5*/
printlist(head);

destroy(head);
getchar();
}


Related Links :

Memory Operations in header file memory.h

Memory.h is the great source of memory fuction in C, well i have try to explain some of the important functions of memory.h file which are useful for memory managment & string handling, hope it will be helpful for you...

  • void *memchr (void *s, int c, size_t n) -- This memory funtion Search for a character in a buffer .
  • int memcmp (void *s1, void *s2, size_t n) -- this function Compare two buffers.
  • void *memcpy (void *dest, void *src, size_t n) -- this function Copy one buffer into another .
  • void *memmove (void *dest, void *src, size_t n) -- Move a number of bytes from one buffer lo another.
  • void *memset (void *s, int c, size_t n) -- Set all bytes of a buffer to a given character.
    Their use is fairly straightforward and not dissimilar to comparable string operations (except the exact length (n) of the operations must be specified as there is no natural termination here).
    Note that in all case to bytes of memory are copied.
  • The sizeof() function comes in handy again here, for example:
    char src[SIZE],dest[SIZE];int isrc[SIZE],idest[SIZE];
    memcpy(dest,src, SIZE); /* Copy chars (bytes) ok */
    memcpy(idest,isrc, SIZE*sizeof(int)); /* Copy arrays of ints */
    memmove() behaves in exactly the same way as memcpy() except that the source and destination locations may overlap.
  • memcmp() is similar to strcmp() except here unsigned bytes are compared and returns less than zero if s1 is less than s2 etc.

Related Links :

What are the Advantages of using UNIX with C ?

Well Friends as we know that C can do Much More Different with diferent OS but as best UNIX is cool whys its best partner of C ???? Lets Know....

Portability -- UNIX, or a variety of UNIX, is available on many machines. Programs written in standard UNIX and C should run on any of them with little difficulty.
Multiuser / Multitasking -- many programs can share a machines processing power.
File handling -- hierarchical file system with many file handling routines.
Shell Programming -- UNIX provides a powerful command interpreter that
understands over 200 commands and can also run UNIX and user-defined programs.

Pipe -- where the output of one program can be made the input of another. This can done from command line or within a C program.
UNIX utilities -- there over 200 utilities that let you accomplish many routines without writing new programs. e.g. make, grep, diff, awk, more ....
System calls -- UNIX has about 60 system calls that are at the heart of the operating system or the kernel of UNIX. The calls are actually written in C. All of them can be accessed from C programs. Basic I/0, system clock access are examples. The function open() is an example of a system call.
Library functions -- additions to the operating system.

Related Links :

Program to Connect Internet Web Site Through C , DOS


Connect to a site example
#include
#include
#include
int main()
{

HINTERNET Initialize,Connection,File;
DWORD dwBytes;
char ch;
Connection = InternetConnect(Initialize,"www.xxx.com",INTERNET_DEFAULT_HTTP_PORT,
NULL,NULL,INTERNET_SERVICE_HTTP,0,0);

File = HttpOpenRequest(Connection,NULL,"/index.html",NULL,NULL,NULL,0,0);

if(HttpSendRequest(File,NULL,0,NULL,0))
{
while(InternetReadFile(File,&ch,1,&dwBytes))
{
if(dwBytes != 1)break;
putchar(ch);
}
}
InternetCloseHandle(File);
InternetCloseHandle(Connection);
InternetCloseHandle(Initialize);
return 0;
}

Related Links :

Program to Display todays date in c



Program to Display todays date in c




#include
#include
#include
#include
int main(void)
{
struct date DATE;

clrscr();
getdate(&DATE);
printf("The year is : %d\n",DATE.da_year);
printf("The month is : %d\n",DATE.da_month);
printf("The day is : %d\n",DATE.da_day);

return 0;

}

Related Links :

Program to Delete Duplicates from given Array





#include
#include
#include
#include
void main()
{
int i,j,l,t=1;
char y,s[15];
clrscr();
s[0]='E';
s[1]='n';
s[2]='t';
s[3]='e';
s[4]='r';
s[5]=' ';
s[6]='a';
s[7]=' ';
s[8]='s';
s[9]='t';
s[10]='r';
s[11]='i';
s[12]='n';
s[13]='g';
s[14]=NULL;
printf("Enter a string
");
gotoxy(1,2);
while((y=getchar())!='
')
{
for(i=3,l=200;i<=24;i++,l=l+50)
{
gotoxy(t,i);
if(y!=' ')
{
printf("%c",y);
gotoxy(t,i-1);
printf(" ");
sound(l);
delay(80);
}
}
t++;
}
gotoxy(1,1);
t=1;
for(j=0,l=100;j<14;j++,l=l+50)
{
if(l>200)
l=100;
for(i=2;i<24;i++,l++)
{
gotoxy(t,i);
if(s[j]!=' ')
{
printf("%c",s[j]);
gotoxy(t,i-1);
printf(" ");
sound(l);
delay(80);
}
}
t++;
}
nosound();
getch();
}

Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!