270E150
LAB - I
ANNAMALAI UNIVERSITY
DIRECTORATE OF DISTANCE EDUCATION
Diploma in Commuter Applications (DCA)
PROGRAMMING LAB - I
C - PROGRAMMING
Copyright Reserved
(For Private Circulation Only)
1. Write a Program to print “Hello World”.
#include <stdio.h>
int main (void)
{
printf ("Hello World! \n");
}
OUT PUT:
Hello World!
2. Write a Program to Print powers of 2: 1, 2, 4, 8, up to 2^N
#include <stdio.h>
#define N 16
int main(void) {
int n;
int val = 1;
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;
}
OUT PUT:
n 2^n
================
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
2
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
3. Write a program Add two numbers and print them out with their sum.
#include <stdio.h>
int main(void) {
int first, second;
printf("Enter two integers > ");
scanf("%d %d", &first, &second);
printf("The two numbers are: %d %d\n", first,
second);
printf("Their sum is %d\n", first+second);
}
4. Write a program to Read a sequence of positive integers and print them out
together with their sum. Use a Sentinel value (say 0) to determine when the sequence
has terminated.
#include <stdio.h>
#define SENTINEL 0
int main(void) {
int sum = 0; /* The sum of numbers already read */
int current; /* The number just read */
do {
printf("\nEnter an integer > ");
scanf("%d", ¤t);
if (current > SENTINEL)
sum = sum + current;
} while (current > SENTINEL);
printf("\nThe sum is %d\n", sum);
}
3
5. Write a program to find the factorial of an integer.
#include <stdio.h>
int fact(int n);
int main(void) {
int current;
printf("Enter a positive integer [to terminate enter
non-positive] > ");
scanf("%d", ¤t);
while (current > 0) {
printf("The factorial of %d is %d\n", current,
fact(current));
printf("Enter a positive integer [to terminate
enter non-positive] > ");
scanf("%d", ¤t);
}
}
/* n is a positive integer. The function returns its
factorial */
int fact(int n) {
int lcv; /* loop control variable */
int p; /* set to the product of the first lcv
positive integers */
for(p=1, lcv=2; lcv <= n; p=p*lcv, lcv++);
return p;
}
4
6. Write a program to find the given number is Prime Number or not.
#include <stdio.h>
int main(void) {
int n;
int i;
int flag;
printf("Enter value of N > ");
scanf("%d", &n);
for (i=2, flag=1; (i<(n/2)) && flag; ) {
if ((n % i) == 0)
flag = 0;
else
i++;
}
if (flag)
printf("%d is prime\n", n);
else
printf("%d has %d as a factor\n", n, i);
}
5
7. Write a program to find first N Fibonacci Numbers.
#include <stdio.h>
int main(void) {
int n;
int i;
int current;
int next;
int twoaway;
printf("How many Fibonacci numbers do you want to
compute? ");
scanf("%d", &n);
if (n<=0)
printf("The number should be positive.\n");
else {
printf("\n\n\tI \t Fibonacci(I)
\n\t=====================\n");
next = current = 1;
for (i=1; i<=n; i++) {
printf("\t%d \t %d\n", i, current);
twoaway = current+next;
current = next;
next = twoaway;
}
}
}
OUTPUT:
How many Fibonacci numbers do you want to compute? 9
I Fibonacci(I)
=====================
1 1
2 1
3 2
4 3
6
5 5
6 8
7 13
8 21
9 34
8. Write a program using function declarations, definitions.
#include <stdio.h>
void square1(void);
void square2(int i);
int square3(void);
int square4(int i);
int area(int b, int h);
/* Main program: Using the various functions */
int main (void) {
square1(); /* Calling the square1 function */
square2(7); /* Calling the square2 function
using
7 as actual parameter corresponding
to
the formal parameter i */
printf("The value of square3() is %d\n",
square3());
printf("The value of square4(5) is %d\n",
square4(5));
printf("The value of area(3,7) is %d\n",
area(3,7));
}
/* Definitions of the functions */
/* Function that reads from standard input an
integer and prints it out together with its sum
*/
void square1(void){
int x;
7
printf("Please enter an integer > ");
scanf("%d", &x);
printf("The square of %d is %d\n", x, x*x);
}
/* Function that prints i together with its sum */
void square2(int i){
printf("The square of %d is %d\n", i, i*i);
}
/* Function that reads from standard input an
integer and returns its square */
int square3(void){
int x;
printf("Please enter an integer > ");
scanf("%d", &x);
return (x*x);
}
/* Function that returns the square of i */
int square4(int i){
return (i*i);
}
/* Function that returns the area of the rectangle
with base b and hight h */
int area(int b, int h){
return (b*h);
}
OUTPUT:
Please enter an integer > 3
The square of 3 is 9
The square of 7 is 49
Please enter an integer > 4
The value of square3() is 16
The value of square4(5) is 25
The value of area(3,7) is 21
8
9. Write a program to display the reverse number.
#include <stdio.h>
main()
{
int n,reverse;
clrscr();
printf("Enter any positive number");
scanf("%d",&n);
while (n!=0)
{
reverse=n%10;
printf("%d",reverse);
n=n/10;
}
getche();
}
10. Write a program on Operations on arrays to accessing array elements in
different ways.
#include <stdio.h>
#include <conio.h>
main()
{
int num[] = {24,34,12,44,56,17};
int i=0;
clrscr();
while (i < 6)
{
printf("\nAddress = %u %u", &num[i],num+i);
printf("\nElement = %d ", num[i]);
printf(" %d ", *(num+i));
printf(" %d ", *(i+ num));
printf(" %d ", i[num]);
printf(" %d\n", *(num + (i + 1)));
i++;
}
getch();
}
9
11. Write a program using Simple string operations:
String literals:
printf, scanf, %s, %c
strlen
strcpy
strcmp
#include <stdio.h>
#define MAXBUFF 128
int main(void) {
char c[] = "012345";
char line[MAXBUFF];
int lcv;
int cmp;
printf("sizeof(c)= %d\n", sizeof(c));
printf("sizeof(line)= %d\n", sizeof(line));
for (lcv=0; lcv<=strlen(c); lcv++)
printf("c[lcv]= %d = %c\n",c[lcv],c[lcv]);
printf("Please enter a string : ");
scanf("%s",line);
printf("strlen(line) = %d\n", strlen(line));
printf("line = [%s]\n",line);
cmp = strcmp(c,line);
if(cmp<0)
printf("%s is less than %s\n", c, line);
else if (c==0)
printf("%s is equal to %s\n", c, line);
else
printf("%s is greater than %s\n", c, line);
strcpy(line,c); /*copy the string c into line */
cmp = strcmp(c,line);
if(cmp<0)
printf("%s is less than %s\n", c, line);
else if (cmp==0)
printf("%s is equal to %s\n", c, line);
else
printf("%s is greater than %s\n", c, line);
}
10
OUTPUT:
sizeof(c)= 7
sizeof(line)= 128
c[lcv]= 48 = 0
c[lcv]= 49 = 1
c[lcv]= 50 = 2
c[lcv]= 51 = 3
c[lcv]= 52 = 4
c[lcv]= 53 = 5
c[lcv]= 0 =
Please enter a string: roses are red
strlen(line) = 5
line = [roses]
012345 is less than roses
012345 is equal to 012345
12. Write a program to find weather the given string is PALINDROM or Not.
#include<stdio.h>
#include<string.h>
#include"youdei.h"
main()
{
char string[100];
int i,len,check;
frontend("PROGRAM TO FIND WHETHER A STRING IS
PALINDROME");
printf("Enter a string =>");
scanf("%[^\n]",string);
len=strlen(string)-1;
for(i=0;i<=len/2+1;i++)
if (string[i]==string[(len-i)]) check=1;
else
{
check=0;
i=len;
}
if (check==1)
printf("\n%s is a palindrome",string);
else
printf("%s is not a palindrome",string);
getche();
}
11
13. Write a program to Compacting sequences of spaces in a string, using two
different methods.
#include <stdio.h>
#define MAXBUFF 128
int getline(char line[], int nmax);
int compact1(char line[]);
int compact2(char line[]);
int main(void) {
char buffer1[MAXBUFF];
char buffer2[MAXBUFF];
int len;
len = getline(buffer1, MAXBUFF);
printf("You entered : %s\n", buffer1);
strcpy(buffer2,buffer1);
printf("Which is : %s\n", buffer2);
len=compact1(buffer1);
printf("compact1: len=%d, %s\n",len, buffer1);
len=compact2(buffer2);
printf("compact2: len=%d, %s\n",len, buffer2);
}
int getline(char line[], int nmax)
/* It prompts user and reads up to nmax
* characters into line. It returns number
of characters read. ['\n' terminates the
line] */
{
int len;
char c;
len = 0;
printf("Enter a string [CR to exit]: ");
while(((c=getchar())!='\n') && len<nmax-1)
line[len++]=c;
line[len]='\0';
return len;
}
int compact1(char line[])
/* It replaces streaks of spaces in line by a
* single space. It returns lenght of
resulting string.
12
*/
{
int cursor=0; /* Cursor on the line */
int prevspace = 0; /* True iff preceding position
was with a space */
int lcv=0; /* Other cursor */
if(line[cursor]=='\0')
return 0;
do{
if((line[cursor]== ' ')&&prevspace){
/*If we have a space preceded by a space,
move rest of string left one position */
for(lcv=cursor;line[lcv];lcv++)
line[lcv]=line[lcv+1];
}else
prevspace=(line[cursor++]==' ');
}while(line[cursor]);
return cursor;
}
int compact2(char line[])
/* It replaces streaks of spaces in line by a
* single space. It returns length of
resulting string.
*/
{
int cursor=0; /* Cursor on the line */
int prevspace = 0; /* True iff preceding position
was with a space */
int lcv = 0; /* Where we copy characters to
*/
do{
if(!((line[cursor]==' ')&&prevspace)){
line[lcv++]=line[cursor];
prevspace=(line[cursor]==' ');
}
}while(line[cursor++]);
return(lcv-1); /* We need the -1 since it counts
also
the '\0' */
}
13
14. Write a program to illustration of pointer to pointer to pointer.
#include <stdio.h>
main()
{
int i;
int *j;
int **k;
int ***l;
int ****m;
clrscr();
j = &i;k=&j;l=&k;m=&l;
printf("Address of i = %u %u %u %u %u \n",
&i,j,*k,**l,***m);
printf("Address of j = %u %u %u %u \n", &j,k,*l,**m);
printf("Address of k = %u %u %u \n", &k,l,*m);
printf("Address of l = %u %u \n", &l,m);
printf("Address of m = %u \n", &m);
i = 100;
printf("Contents of i = %d %d %d %d %d %d
\n",i,*(&i),*j,**k,***l,****m);
printf("Contents of j = %u %u %u %u %u %u
\n",&i,j,*(&j),*k,**l,***m);
printf("Contents of k = %u %u %u %u %u
\n",&j,k,*(&k),*l,**m);
printf("Contents of l = %u %u %u %u
\n",&k,l,*(&l),*m);
printf("Contents of m = %u %u %u \n",&l,m,*(&m));
*j = 200;
printf("Contents of i = %d %d %d %d %d %d
\n",i,*(&i),*j,**k,***l,****m);
**k = 300;
printf("Contents of i = %d %d %d %d %d %d
\n",i,*(&i),*j,**k,***l,****m);
***l = 400;
printf("Contents of i = %d %d %d %d %d %d
\n",i,*(&i),*j,**k,***l,****m);
****m = 500;
printf("Contents of i = %d %d %d %d %d %d
\n",i,*(&i),*j,**k,***l,****m);
getch();
}
14
15. Write a program Illustration of function with pointer arguments
#include <stdio.h>
#include <conio.h>
main()
{
int i1 = -5,i2 = 66,*p1 = &i1,*p2=&i2;
clrscr();
printf("i1 and i2 before call to function test :\n
i1 = %d i2 = %d \n\n",i1,i2);
exchange(p1,p2);
printf("i1 and i2 after call to 1st exchange :\n
i1
= %d i2 = %d \n",
i1,i2);
exchange(*p1,*p2);
printf("i and j after call to 2nd exchange :\n i1
=
%d i2 = %d \n",
i1,i2);
getch();
}
exchange(ptr1,ptr2)
int *ptr1,*ptr2;
{
int tmp;
tmp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = tmp;
}
15
16. Write a program using of linked list to input nos in the beginning AND to insert
a number in any position AND to delete a number from the list.
#include <stdio.h>
struct node
{
int data;
struct node *link;
};
struct node *p;
main()
{
int i,value,where,what;
p = NULL;
for(i=1;i<4;i++)
{
clrscr();
printf("\n");
printf("Enter Values (3 Numbers) ");
scanf("%d",&value);
append(value,p);
}
display(p);
for(i=1;i<4;i++)
{
printf("Enter Data Value to add at beginning of
the
list ");
scanf("%d",&value);
addatbeg(value,p);
}
display(p);
printf("Enter where to Insert the value ");
scanf("%d",&where);
printf(" Enter the value ");
scanf("%d",&value);
addafter(value,where,p);
display(p);
printf("\n ");
printf("Enter data element to delete");
scanf("%d",&value);
delete(value,p);
display(p);
getch();
}
16
append(int num,struct node *q)
{
struct node *temp;
if (q==NULL)
{
p = malloc(sizeof(struct node));
p->data = num;
p->link = NULL;
}
else
{
while(q->link != NULL)
q=q->link;
temp = malloc(sizeof(struct node));
temp->data = num;
temp->link = NULL;
q->link = temp;
}
}
display(struct node *q)
{
printf("\n");
while (q != NULL)
{
printf(" %d ",q->data);
q = q->link;
}
printf("\n");
}
addatbeg(int num, struct node *q)
{
p = malloc(sizeof(struct node));
p->data = num;
p->link = q;
}
addafter(int num,int post,struct node *q)
{
struct node *temp;
int i;
for(i=1;i<post;i++)
{
q=q->link;
if (q == NULL)
17
{
printf("\nThere are less than %d elements in the
list",post);
return;
}
}
temp = malloc(sizeof(struct node));
temp->data = num;
temp->link = q->link;
q->link = temp;
}
delete(int num,struct node *q)
{
if (q->data == num)
{
p = q->link;
free(q);
}
while(q->link->link != NULL)
{
if(q->link->data == num)
{
q->link = q->link->link;
printf("The value is Deleted !!!");
return;
}
q = q->link;
}
if (q->link->data == num)
{
free(q->link);
q->link=NULL;
return;
}
printf("Element %d not found ",num);
}
18
17. It contains a program that is given as command parameter the name of a text
file, (i.e [Link].) It will read each line of [Link] and print out in
correspondence the number of characters and words on that line. At the end it
will print out the number of lines that were read.
#include <stdio.h>
int main (int argc, char *argv[]){
FILE *fp;
int nchars, nwords, nlines;
int lastnblank; /* 0 if f the last character was
a space */
char c;
if(argc!=2){
printf("Usage: %s filename\n", argv[0]);
exit(0);
}
if((fp=fopen(argv[1],"r"))==NULL){
perror("fopen");
exit(0);
}
nchars=nwords=nlines=lastnblank=0;
while((c=getc(fp))!=EOF){
nchars++;
if (c=='\n'){
if (lastnblank)
nwords++;
printf("words=%d, characters=%d\n", nwords,
nchars);
nchars=nwords=lastnblank=0;
nlines++;
}else{
if (((c==' ')||(c=='\t'))&(lastnblank))
nwords++;
lastnblank=((c!=' ')&&(c!='\t'));
}
}
printf("lines=%d\n", nlines);
fclose(fp);
19
18. Write a program for stack operations.
#include <stdio.h>
#define MAXCOLS 80
struct stack
{
int top;
float items[MAXCOLS];
};
push(ps,x)
struct stack *ps;
float x;
{
ps->items[++(ps->top)] = x;
return;
}
float pop(ps)
struct stack *ps;
{
return(ps->items[ps->top--]);
}
float eval(expr)
char expr[];
{
int c,position;
float opnd1,opnd2,value;
float oper(),pop();
struct stack opndstk;
[Link] = -1;
for(position =0;(c=expr[position]) != '\0';
position++)
if (isdigit(c))
push(&opndstk,(float)(c-'0'));
else
{
opnd2 = pop(&opndstk);
opnd1 = pop(&opndstk);
value = oper(c,opnd1,opnd2);
push(&opndstk,value);
}
return (pop(&opndstk));
}
20
isdigit(symb)
char symb;
{
return(symb >= '0' && symb <= '9');
}
float oper(symb,op1,op2)
int symb;
float op1,op2;
{
switch(symb)
{
case '+' : return (op1 + op2);
case '-' : return (op1 - op2);
case '*' : return (op1 * op2);
case '/' : return (op1 / op2);
default : printf("%s","Illegal Operation");
exit(1);
}
}
main()
{
char expr[MAXCOLS];
int position = 0;
float eval();
clrscr();
while((expr[position++] = getchar()) != '\n');
expr[--position] = '\0';
printf("%s%s", "The Original Postfix Expression is
:"
, expr);
printf(" &its value is %f\n", eval(expr));
getch();
}
21
19. Write a program for deque operations.
#include<stdio.h>
#include<conio.h>
struct deque
{
int data;
struct deque *front;
struct deque *rear;
} ;
void addfnt(struct deque *);
void addrear(struct deque *);
void delfnt(struct deque *);
void delrear(struct deque *);
void disp(void);
struct deque *l,*f,*f1,*f2,*fn,*re,*r1,*r2;
main()
{
struct deque *d1;
int ch;
char tt='Y';
while (tt=='Y')
{
printf("1. Addition at front\n");
printf("2. Addition at rear\n");
printf("3. Deletion at front\n");
printf("4. Deletion at rear \n");
printf("5. Display queue\n");
printf("6. Exit\n");
printf("Enter Your choice\n");
scanf("%d",&ch);
switch (ch)
{
case 1: addfnt(d1);
case 2: addrear(d1);
case 3: delfnt(d1);
case 4 : delrear(d1);
case 5: disp();
case 6 :
{
tt='N';
break;
}
22
}
}
}
void addfnt(struct deque *fd1)
{
char ch='y';
if (fd1==NULL)
{ fd1->front=NULL;
fd1->rear=NULL;}
fd1= (struct deque *) malloc( sizeof(struct
deque));
printf("Enter data\n");
scanf("%d",fd1->data);
re=fd1;
f1=fd1;
}
do
{
fd1=(struct deque*)malloc(sizeof(struct
deque));
printf("enter data\n");
scanf("%d",fd1->data);
fd1->rear=f1;
fd1->front=NULL;
f1=fd1;
printf("do you want to continue\n");
scanf("%c",&ch);
}
while (ch=='Y');
fn=fd1;
}
void addrear(struct deque *fd1)
{
char ch='y';
if (fd1==NULL)
{ fd1->front=NULL;
fd1->rear=NULL;}
fd1= (struct deque *) malloc( sizeof(struct
deque));
printf("Enter data\n");
scanf("%d",fd1->data);
fn=fd1;
r1=fd1;
}
while (ch=='Y'||ch=='y')
{
23
fd1=(struct deque*)malloc(sizeof(struct
deque));
printf("enter data\n");
scanf("%d",fd1->data);
fd1->front=r1;
fd1->rear=NULL;
r1=fd1;
printf("do yo7u want to continue\n");
scanf("%c",&ch);
}
re=fd1;
}
void delfnt(struct deque*fd1)
{
if (fd1==NULL)
{
printf("queue is empty");
}
else
{
f1=fn->rear;
f1->front=NULL;
}
}
void delrear(struct deque*fd1)
{
if (fd1==NULL)
{
printf("queue is empty");
}
else
{
f1=re->front;
f1->rear=NULL;
}
}
void disp(void)
{
f=fn;
l=fn;
while(f->rear!=NULL)
{
printf("%d",f->data);
l=f->rear;
f=l;
}
}
24
20. Write a program Read an integer array, print it, then sort it and print it. Use
the selection sort method.
#include <stdio.h>
#define NMAX 10
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void selectionSort(int a[], int n);
int main(void) {
int x[NMAX];
int hmny;
int who;
int where;
hmny = getIntArray(x, NMAX, 0);
if (hmny==0)
printf("This is the empty array!\n");
else{
printf("The array was: \n");
printIntArray(x,hmny);
selectionSort(x,hmny);
printf("The sorted array is: \n");
printIntArray(x,hmny);
}
}
void printIntArray(int a[], int n)
/* n is the number of elements in the array a.
* These values are printed out, five per
line.*/
{
int i;
for (i=0; i<n; ){
printf("\t%d ", a[i++]);
if (i%5==0)
printf("\n");
}
printf("\n");
}
int getIntArray(int a[], int nmax, int sentinel)
25
/* It reads up to nmax integers and stores
then in a; sentinel terminates input. */
{
int n = 0;
int temp;
do {
printf("Enter integer [%d to terminate] : ",
sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
void selectionSort(int a[], int n)
/* It sorts in non-decreasing order the first N
positions of A. It uses the selection sort method.
*/
{
int lcv;
int rh; /*Elements in interval rh..n-1 are
in
their final position*/
int where; /*Position where we have current
maximum*/
int temp; /*Used for swapping*/
for(rh=n-1;rh>0;rh--){
/*Find position of largest element in range
0..rh*/
where = 0;
for (lcv=1;lcv<=rh;lcv++)
if (a[lcv]>a[where])
where = lcv;
temp = a[where];
a[where] = a[rh];
a[rh] = temp;
}
}
26
21. Write a program to Read an integer array, print it, then sort it and print it.
Use the bubble sort method.
#include <stdio.h>
#define NMAX 10
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void bubbleSort(int a[], int n);
int main(void) {
int x[NMAX];
int hmny;
int who;
int where;
hmny = getIntArray(x, NMAX, 0);
if (hmny==0)
printf("This is the empty array!\n");
else{
printf("The array was: \n");
printIntArray(x,hmny);
bubbleSort(x,hmny);
printf("The sorted array is: \n");
printIntArray(x,hmny);
}
}
void printIntArray(int a[], int n)
/* n is the number of elements in the array a.
These values are printed out, five per
line.*/
{
int i;
for (i=0; i<n; ){
printf("\t%d ", a[i++]);
if (i%5==0)
printf("\n");
}
printf("\n");
}
int getIntArray(int a[], int nmax, int sentinel)
/* It reads up to nmax integers and stores
then in a; sentinel terminates input. */
27
{
int n = 0;
int temp;
do {
printf("Enter integer [%d to terminate] : ",
sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
void bubbleSort(int a[], int n)
/* It sorts in non-decreasing order the first N
positions of A. It uses * the bubble sort method.
*/
{
int lcv;
int limit = n-1;
int temp;
int lastChange;
while (limit) {
lastChange = 0;
for (lcv=0;lcv<limit;lcv++)
/* Notice that the values in positions
LIMIT+1 .. N are in* their final position, i.e.
they are sorted right */
if (a[lcv]>a[lcv+1]) {
temp = a[lcv];
a[lcv] = a[lcv+1];
a[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange; } }
28
22. Write a program to Read a file containing a sequence of records representing
students, places them into an array, then writes that array out to new files. The
names of the files are passed in as command line parameters.
#include <stdio.h>
#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<n;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.*/
29
{
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);
}
270E150
ANNAMALAI UNIVERSITY PRESS 2019– 2020