HII Friends i have put LOTs of C programmes.If need something, let me know. :)
Hello
/* hello.c -- The most famous program of them all ..
*/
#include
int main(void) {
printf("Hello World!\n");
// return 0;
}
/* power2.c -- Print out powers of 2: 1, 2, 4, 8, .. up to 2^N
*/
#include
#define N 16
int main(void) {
int n; /* The current exponent */
int val = 1; /* The current power of 2 */
printf("\t n \t 2^n\n");
printf("\t================\n");
for (n=0; n<=N; n++) {
printf("\t%3d \t %6d\n", n, val);
val = 2*val;
}
return 0;
}
/* It prints out :
n 2^n
================
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
*/
/* makebinfile.c - Reads a file containing a sequence of text records
* and writes it out to a new binary files.
* The names of the files are passed in as command
* line parameters.
*/
#include
#define SIZE 10
#define NAMESIZE 25
typedef struct {
char name[NAMESIZE];
int midterm;
int final;
int homeworks;
} student;
int writeastudent(FILE *fdout, student * who){
/* Write to an open binary file fdout the content of who.
* Return the number of bytes that were written out.
*/
char * p; /* Cursor in outputting a byte at a time */
char * limit = ((char *)who)+sizeof(student); /*Address just past who */
for (p=(char *)who;p
fputc(*p, fdout);
}
return (limit - (char *)who);
}
int main (int argc, char *argv[]){
int n = 0; /* Number of records read */
int m; /* Number of bytes in a record */
student who; /* Buffer for a record */
FILE *fdin; /* File descriptor for input file */
FILE *fdout; /* File descriptor for output file */
if(argc!=3){
printf("Usage: %s infile outfile\n", argv[0]);
exit(0);
}
if((fdin=fopen(argv[1],"r"))==NULL){
perror("fopen");
exit(1);
}
if((fdout=fopen(argv[2],"w"))==NULL){
perror("fopen");
exit(1);
}
while(fscanf(fdin,"%s %d %d %d",
who.name, &who.midterm, &who.final, &who.homeworks)!=EOF){
m = writeastudent(fdout, &who);
printf("m=%d\n", m);
n++;
}
printf("n=%d\n", n);
fclose(fdin);
fclose(fdout);
}
/* studentlist.c -- Reads a file containing a sequence of records
* representing students, places them into a linked
* list (a queue), then writes that out to a new files.
* The names of the files are passed in as command
* line parameters.
*/
#include
#define NAMESIZE 25
typedef struct {
char name[NAMESIZE];
int midterm;
int final;
int homeworks;
} student;
typedef student *studentptr;
/***********THE QUEUE ABSTRACT DATA TYPE************/
typedef struct node {
struct node *next;
student value;
}node;
typedef node *nodeptr;
typedef struct {
nodeptr head; /* Here we point to the oldest element in the queue */
nodeptr tail; /* Here we point to the most recent element in queue*/
} queue;
queue *init(void){
/* It creates, initializes, and returns a queue */
queue *q;
if((q=(queue *)malloc(sizeof(queue)))==NULL){
perror("malloc");
exit(1);
}
q->head=NULL;
q->tail=NULL;
return q;
}
void final(queue *q){
/* It frees the queue q */
student s;
while(get(q,&s));
free(q);
}
void put(queue *q, studentptr s){
/* It inserts a new value *s in the queue q */
nodeptr p;
if ((p=(nodeptr)malloc(sizeof(node)))==NULL){
perror("malloc");
exit(1);
}
p->value=*s;
p->next = NULL;
if (q->head == NULL)
q->head = p;
else
q->tail->next = p;
q->tail = p;
}
int get(queue *q, studentptr s){
/*It obtains in s a student record from queue and returns true
if such a record existed */
nodeptr p;
if(p=q->head){
q->head = q->head->next;
if((q->head)==NULL)
q->tail = NULL;
*s=p->value;
free(p);
return 1;
}else
return 0;
}
/*******END OF QUEUE ABSTRACT DATA TYPE******************/
void writeStudentList(char filename[], queue *q)
/* filename is the name of the file where we will
* write sequentially the list accessed from q.
*/
{
FILE *fd; /* File descriptor used for filename */
student s;
if((fd=fopen(filename,"w"))==NULL){
perror("fopen");
exit(1);
}
while(get(q, &s)){
fprintf(fd,"%s %d %d %d\n",
s.name,
s.midterm,
s.final,
s.homeworks);
}
fclose(fd);
}
int readStudentList(char filename[], queue *q)
/* It reads student records from file filename
* and stores them in q.
* It returns the number of records actually read.
*/
{
FILE *fd; /* File descriptor used for filename */
int i=0;
student temp;
if((fd=fopen(filename,"r"))==NULL){
perror("fopen");
exit(1);
}
while(fscanf(fd,"%s %d %d %d",
temp.name,
&temp.midterm,
&temp.final,
&temp.homeworks)!=EOF){
put(q,&temp);
i++;
}
fclose(fd);
return i;
}
int main(int argc, char *argv[]){
int n;
queue *q;
q = init();
if(argc!=3){
printf("Usage: %s infile outfile\n", argv[0]);
exit(0);
}
n = readStudentList(argv[1],q);
writeStudentList(argv[2],q);
final(q);
}
/* clean.c -- Given as command line parameter a filename,
* it removes from that file all occurrences of ^M
* If 'clean' is the executable image of this
* program, you can use it as follows:
* % clean dirtyfile > cleanfile
*/
#include
#define CONTROLM 13
int main(int argc, char *argv[]){
char c;
FILE *fd;
if(argc!=2){
printf("Usage: %s filename\n", argv[0]);
exit(0);
}
if((fd = fopen(argv[1],"r"))==NULL){
perror("fopen");
exit(1);
}
while((c=getc(fd))!=EOF)
if (c!=CONTROLM)
putchar(c);
fclose(fd);
}
/* sortmerge.c - Reads a file containing a sequence of records
* representing students and places them into an
* array table1. It does the same for a second file
* placing result in table2. It then sorts table1
* and table2 in non descending order on the basis
* of their name fields. It now stores the merge of
* the two sorted sequences in an array table3.
* Finally it writes the result to a third file.
* The names of the three files are passed as
* command line parameters
*/
#include
#define SIZE 10
#define NAMESIZE 25
typedef struct {
char name[NAMESIZE];
int midterm;
int final;
int homeworks;
} student;
void writeStudentArray(char filename[], student a[], int n)
/* n is the number of elements in the array a.
* filename is the name of the file where we will
* write.
*/
{
FILE *fd; /* File descriptor used for filename */
int i;
if(n<=0)
return;
if((fd=fopen(filename,"w"))==NULL){
perror("fopen");
exit(1);
}
for (i=0;i
fprintf(fd,"%s %d %d %d\n",
a->name, a->midterm, a->final, a->homeworks);
a++;
}
fclose(fd);
}
int readStudentArray(char filename[], student a[], int nmax)
/* It reads up to nmax student records
* from file filename and stores them in a.
* It returns the number of records actually read.
* WARNING: We assume that the name of the students contain no spaces.
*/
{
FILE *fd; /* File descriptor used for filename */
int i=0;
if((fd=fopen(filename,"r"))==NULL){
perror("fopen");
exit(1);
}
while(fscanf(fd,"%s %d %d %d",
a->name, &a->midterm, &a->final, &a->homeworks)!=EOF){
if(++i==nmax)break; /* We have filled up the table */
a++;
}
fclose(fd);
return i;
}
void sortStudentArray(student table[], int n){
/* It sorts in nondecreasing order on the basis of their
* names the first n records of table.
* WARNING: THIS IS A NULL STUB.
*/
}
int mergeStudentArray(student table3[], student table1[], int n1,
student table2[], int n2){
/* It merges into table3 the first n1 student records of table1
* and the first n2 student records of table2. The records in
* table1 and table2 are sorted in nondecreasing order of their
* name fields. It returns n1+n2.
* WARNING: THIS IS A STUB WHERE WE JUST COPY, NOT MERGE.
*/
int i,j=0;
for(i=0;i
table3[j++] = table1[i];
}
for(i=0;i
table3[j++] = table2[i];
}
return n1+n2;
}
int main(int argc, char *argv[]){
int n1, /* Number of students in first sequence */
n2, /* Number of students in second sequence */
n3; /* Number of students in resulting sequence */
student table1[SIZE], /* Students in first sequence */
table2[SIZE], /* Students in second sequence */
table3[SIZE+SIZE]; /* Students in merged sequence */
if(argc!=4){
printf("Usage: %s unsfile1 unsfile2 outfile\n", argv[0]);
exit(0);
}
n1 = readStudentArray(argv[1],table1,SIZE);
sortStudentArray(table1,n1);
n2 = readStudentArray(argv[2],table2,SIZE);
sortStudentArray(table2,n2);
n3 = mergeStudentArray(table3, table1, n1, table2, n2);
writeStudentArray(argv[3],table3,n3);
}
/* stringmerge.c -- Given two sorted files of strings, it creates
* a sorted file consisting of all their elements.
* The names of the files are passed as command
* line parameters.
*/
#include
#define MAXBUFFER 128
int getline(FILE * fd, char buff[], int nmax){
/* It reads a line from fd and stores up to nmax of
* its characters to buff.
*/
char c;
int n=0;
while ((c=getc(fd))!='\n'){
if(c==EOF)return EOF;
if(n
buff[n++]=c;
}
buff[n]='\0';
return n;
}
int stringMerge(char filename1[], char filename2[] , char filename3[]) {
/* Given two sorted files of strings, called filename1 and filename2,
* it writes their merged sequence to the file filename3.
* It returns the total number of strings written to filename3.
*/
FILE *fd1, *fd2, *fd3;
char buffer1[MAXBUFFER], buffer2[MAXBUFFER];
int ln1, ln2;
int n=0;
if ((fd1=fopen(filename1, "r"))==NULL) {
perror("fopen");
exit(1);
}
if ((fd2=fopen(filename2, "r"))==NULL) {
perror("fopen");
exit(1);
}
if ((fd3=fopen(filename3, "w"))==NULL) {
perror("fopen");
exit(1);
}
ln1 = getline(fd1,buffer1,MAXBUFFER-1);
ln2 = getline(fd2,buffer2,MAXBUFFER-1);
while ((ln1!=EOF) && (ln2!=EOF)){
if (strcmp(buffer1,buffer2)<=0){
fprintf(fd3, "%s\n", buffer1);
ln1 = getline(fd1,buffer1,MAXBUFFER-1);
}else{
fprintf(fd3, "%s\n", buffer2);
ln2 = getline(fd2,buffer2,MAXBUFFER-1);
}
n++;
}
while (ln1!=EOF){
fprintf(fd3, "%s\n", buffer1);
ln1=getline(fd1,buffer1,MAXBUFFER-1);
n++;
}
while (ln2!=EOF){
fprintf(fd3, "%s\n", buffer2);
ln2=getline(fd2,buffer2,MAXBUFFER-1);
n++;
}
fclose(fd1);
fclose(fd2);
fclose(fd3);
return n;
}
int main(int argc, char *argv[]) {
if(argc!=4){
printf("Usage: %s sortedfile1 sortedfile2 mergefile\n", argv[0]);
exit(0);
}
printf("We have %d merged records\n",
stringMerge(argv[1], argv[2], argv[3]));
}
/* studentarray.c - Reads a file containing a sequence of records
* representing students, places them into an
* array, then writes that array out to a new files.
* The names of the files are passed in as command
* line parameters.
*/
#include
#define SIZE 10
#define NAMESIZE 25
typedef struct {
char name[NAMESIZE];
int midterm;
int final;
int homeworks;
} student;
void writeStudentArray(char filename[], student a[], int n)
/* n is the number of elements in the array a.
* filename is the name of the file where we will
* write.
*/
{
FILE *fd; /* File descriptor used for filename */
int i;
if(n<=0)
return;
if((fd=fopen(filename,"w"))==NULL){
perror("fopen");
exit(1);
}
for (i=0;i
fprintf(fd,"%s %d %d %d\n",
a->name, a->midterm, a->final, a->homeworks);
a++;
}
fclose(fd);
}
int readStudentArray(char filename[], student a[], int nmax)
/* It reads up to nmax student records
* from file filename and stores them in a.
* It returns the number of records actually read.
*/
{
FILE *fd; /* File descriptor used for filename */
int i=0;
if((fd=fopen(filename,"r"))==NULL){
perror("fopen");
exit(1);
}
while(fscanf(fd,"%s %d %d %d",
a->name, &a->midterm, &a->final, &a->homeworks)!=EOF){
if(++i==nmax)break; /* We have filled up the table */
a++;
}
fclose(fd);
return i;
}
int main(int argc, char *argv[]){
int n;
student table[SIZE];
if(argc!=3){
printf("Usage: %s infile outfile\n", argv[0]);
exit(0);
}
n = readStudentArray(argv[1],table,SIZE);
writeStudentArray(argv[2],table,n);
}
// struct.c - Simple uses of structures
#include
struct student {
int mid;
int final;
int hmws;
};
void main(void){
struct student sam = {85, 90, 88};
struct student tom = {93, 88, 91};
struct student *he = &tom; // We can access a structure and its fields through a pointer
// We cannot read or write directly a studentas we can integers, and reals, etc.
// Also, we cannot compare directly two students (i.e. we cannot say (sam == tom)].
// But we can assign a student to another student. For example:
tom = sam;
// Then we can output the value of tom by printing its various fields:
printf("tom = {%d, %d, %d}\n", tom.mid, tom.final, tom.hmws);
// Notice the dot notation for accessing the fields, of tom.
// The output will be tom = {85, 90, 88}
printf("*he = {%d, %d, %d}\n", he->mid, he->final, he->hmws);
// Notice the arrow notation for accessing the fields from he.
// The output will be *he = {85, 90, 88}
// We would get the same output if we access the fields in a different way:
printf("*he = {%d, %d, %d}\n", (*he).mid, (*he).final, (*he).hmws);
}
/* binary.c - Binary search using two methods. The first is more intuitive, but it is
slower.
*/
#include
#include
/* Given a sorted array with n elements, we search for who using binary search.
We return a position where found, or -1 if not there
*/
int binary1(int n, int a[n], int who) {
int left = 0;
int right = n-1;
while (left <= right) {
int mid = left + (right-left)/2;
if (who < a[mid])
right = mid - 1;
else if (who > a[mid])
left = mid + 1;
else
return mid;
}
return -1;
}
/* Given a sorted array with n elements, we search for who using binary search.
We return a position where found, or -1 if not there
*/
int binary2(int n, int a[n], int who) {
int p = n/2;
while (n > 0) {
n = n/2;
if (who < a[p]) {
p -= n;
} else if (who > a[p]) {
p += n;
} else
return p;
}
return -1;
}
/* Returns the difference in microseconds between before and after */
long timediff(struct timeval before, struct timeval after) {
long sec = after.tv_sec - before.tv_sec;
long microsec = after.tv_usec - before.tv_usec;
return 1000000*sec + microsec;
}
int main() {
int a[] = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33};
int n = sizeof(a)/sizeof(int);
int where;
struct timeval before;
struct timeval after;
int k;
int j;
gettimeofday(&before, NULL);
for (j = 0; j < 1000000; j++)
for (k = 0; k < 2*n+1; k++) {
where = binary1(n, a, k);
// printf("who = %d, \tvalue = %d\n", k, where);
}
gettimeofday(&after, NULL);
printf("before=[%ld,%ld], after=[%ld,%ld]\n", before.tv_sec, before.tv_usec,
after.tv_sec, after.tv_usec);
printf("The difference is %ld\n", timediff(before, after));
printf("---------------------------------------------\n");
gettimeofday(&before, NULL);
for (j = 0; j < 1000000; j++)
for (k = 0; k < 2*n+1; k++) {
where = binary2(n, a, k);
// printf("who = %d, \tvalue = %d\n", k, where);
}
gettimeofday(&after, NULL);
printf("before=[%ld,%ld], after=[%ld,%ld]\n", before.tv_sec, before.tv_usec,
after.tv_sec, after.tv_usec);
printf("The difference is %ld\n", timediff(before, after));
return 0;
}