CS ASSIGNMENT Structure and Files
CS ASSIGNMENT Structure and Files
510519056
COMPUTER ASSINMENT ON STRUCTURES
AND FILES
1.
Define a structure student above main() as follows
struct student
{
Char name[50];
int roll;
float percentage;
};
Accept data for 10 such students in an array of
structure and print them. Then print
the record of the student with highest percentage of
marks.
Program
#include<stdio.h>
struct student
char name[50];
int roll;
float percentage;
};
void main()
{
int i,flag = 0;
float max;
scanf("%s", s[i].name);
scanf("%d", &s[i].roll);
scanf("%f", &s[i].percentage);
printf("\n");
max = s[0].percentage;
max = s[i].percentage;
flag = i;
The outputs
2. Define a structure to represent complex numbers.
Write a function that will accept
two complex numbers as arguments, add these two complex
numbers and return
the sum to the calling function.
Ans:
#include<stdio.h>
struct complex
int x,y;
};
void main()
c = add(c1, c2);
struct complex c;
return c;
outputs
3. Repeat the problem 2 with the modification
that the function will add two complex
numbers as usual but not return anything. You
are to call the function from main()
and print the sum from main().
Ans:
#include<stdio.h>
struct complex
int x,y;
};
void main()
p1 = &c1;
p2 = &c2;
add(p1, p2);
#include<stdio.h>
struct complex
int x,y;
};
struct number
int real;
};
void main()
struct number n;
scanf("%d", &n.real);
printf("The complex number= %d + i%d and real number= %d", n.comp.x, n.comp.y, n.real);
}
5. Write a program to define complex number like
problem 2 using structure and
allocate space for (n+1) number of such complex numbers
using dynamic
memory allocation where n is the input during runtime.
Populate the allocated
space for first n complex numbers with suitable values
and store their sum in
the (n+1)th location. Print the sum.
Ans:
#include<stdio.h>
struct complex
int x,y;
};
void main()
int n,i;
scanf("%d", &n);
(ptr+n)->x = 0;
(ptr+n)->y = 0;
(ptr+n)->x += (ptr+i)->x;
(ptr+n)->y += (ptr+i)->y;
}
6. Write a program in C that can count the number of
characters, words and lines
present in a text file.
Ans:
#include<stdio.h>
#include<stdlib.h>
void main()
char ch;
FILE *fp;
fp = fopen("text.txt","r");
if (fp == NULL)
exit(EXIT_FAILURE);
while((ch=fgetc(fp)) != EOF)
nw++;
else if(ch == '\n')
nl++;
nc++;
nl++;
fclose(fp);
#include<stdio.h>
#include<stdlib.h>
void main()
FILE *fp;
scanf("%c", &opt);
scanf("%s", fil);
fp = fopen(fil,"r");
if (fp == NULL)
exit(EXIT_FAILURE);
if(opt == 'e')
while((ch=fgetc(fp)) != EOF)
if(ch == 'x')
ch = 'a';
ch = 'b';
printf("%c", ch);
continue;
else
ch += 3;
printf("%c", ch);
else
while((ch=fgetc(fp)) != EOF)
if(ch == 'a')
ch = 'x';
ch = 'y';
ch = 'z';
printf("%c", ch);
continue;
else
ch -= 3;
printf("%c", ch);
fclose(fp);
#include<stdio.h>
#include<stdlib.h>
void main(int argc, char *argv[])
char ch;
fs = fopen(argv[1], "r");
if(fs == NULL)
exit(0);
ft = fopen(argv[2], "w");
if(ft == NULL)
exit(0);
fputc(ch, ft);
fclose(fs);
fclose(ft);
Output
Copied file.