0% found this document useful (0 votes)
158 views

CS ASSIGNMENT Structure and Files

The document contains 8 programming assignments involving structures and files in C language. The assignments include: 1) defining a student structure and storing details of 10 students in an array, 2) defining a complex number structure and adding two complex numbers, 3) modifying problem 2 to add complex numbers by passing pointers, 4) defining a number structure containing complex and real parts, 5) dynamically allocating space for complex numbers and adding them, 6) counting characters, words and lines in a file, 7) encrypting and decrypting a file by shifting characters, 8) copying contents of one file to another by taking file names as command line arguments.

Uploaded by

Sourin Satpathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views

CS ASSIGNMENT Structure and Files

The document contains 8 programming assignments involving structures and files in C language. The assignments include: 1) defining a student structure and storing details of 10 students in an array, 2) defining a complex number structure and adding two complex numbers, 3) modifying problem 2 to add complex numbers by passing pointers, 4) defining a number structure containing complex and real parts, 5) dynamically allocating space for complex numbers and adding them, 6) counting characters, words and lines in a file, 7) encrypting and decrypting a file by shifting characters, 8) copying contents of one file to another by taking file names as command line arguments.

Uploaded by

Sourin Satpathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SANDIP SATPATHI

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;

struct student s[10];

for(i=0; i<=9; i++)

printf("Enter name of student %d: ", i+1);

scanf("%s", s[i].name);

printf("Enter roll no of student %d: ", i+1);

scanf("%d", &s[i].roll);

printf("Enter percentage of student %d: ", i+1);

scanf("%f", &s[i].percentage);

printf("\n");

max = s[0].percentage;

for(i=1; i<=9; i++)

if (s[i].percentage > max)

max = s[i].percentage;

flag = i;

printf("Highest percentage:\nName: %s, Roll: %d",s[flag].name, s[flag].roll);

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;

};

struct complex add(struct complex a, struct complex b);

void main()

struct complex c, c1,c2;

printf("Enter first complex number (in the form x+iy): ");

scanf("%d + i%d",&c1.x, &c1.y);

printf("Enter second complex number (in the form x+iy): ");

scanf("%d + i%d",&c2.x, &c2.y);

c = add(c1, c2);

printf("The result= %d + i%d",c.x,c.y);

struct complex add(struct complex a,struct complex b)

struct complex c;

c.x = a.x + b.x;

c.y = a.y + b.y;

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 add(struct complex *a, struct complex *b);

void main()

struct complex c1,c2,*p1,*p2;

p1 = &c1;

p2 = &c2;

printf("Enter first complex number (in the form x+iy): ");

scanf("%d + i%d",&c1.x, &c1.y);


printf("Enter second complex number (in the form x+iy): ");

scanf("%d + i%d",&c2.x, &c2.y);

add(p1, p2);

printf("The result= %d + i%d",c1.x,c1.y);

void add(struct complex *a,struct complex *b)

a->x = a->x + b->x;

a->y = a->y + b->y;

4. Write a program to define a structure number as


follows:
Struct number
{
Struct complex comp;
Int real;
};
Here struct complex is same as problem 2. Populate such
a structure with suitable
values and print them.
Ans:

#include<stdio.h>

struct complex

int x,y;

};

struct number

struct complex comp;

int real;

};

void main()

struct number n;

printf("Enter a complex number(x + iy): ");

scanf("%d + i%d", &n.comp.x, &n.comp.y);

printf("Enter a real number: ");

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()

struct complex *ptr;

int n,i;

printf("Enter the numbers of complex numbers: ");

scanf("%d", &n);

ptr = (struct complex *)malloc((n+1)*sizeof(struct complex));

(ptr+n)->x = 0;

(ptr+n)->y = 0;

for(i=0; i<n; i++)

printf("Enter complex no %d(x + iy): ", i+1);

scanf("%d + i%d", &(ptr+i)->x, &(ptr+i)->y);

(ptr+n)->x += (ptr+i)->x;

(ptr+n)->y += (ptr+i)->y;

printf("The sum= %d + i%d",(ptr+n)->x, (ptr+n)->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;

int nc=0, nw=0, nl=0;

FILE *fp;

fp = fopen("text.txt","r");

if (fp == NULL)

printf("\nUnable to open file.\n");

printf("Please check if file exists and you have read privilege.\n");

exit(EXIT_FAILURE);

while((ch=fgetc(fp)) != EOF)

if((ch == ' ') || (ch == '.'))

nw++;
else if(ch == '\n')

nl++;

nc++;

nl++;

printf("%d, %d, %d", nw, nc, nl);

fclose(fp);

7. Write a program that can encrypt and decrypt a file


by changing all the characters of
the file in the following manner. Write functions for
both encryption and decryption.
Encryption: Replace each character by another that is
three places up in the stream
(cat to fdw).
Decryption: Replace each character by another that is
three places down in the stream
(fdw to cat).
Ans:

#include<stdio.h>

#include<stdlib.h>

void main()

char ch, opt, fil[50];

FILE *fp;

printf("Enter 'e' for encryption and 'd' for decryption: ");

scanf("%c", &opt);

printf("Enter full path of the file: ");

scanf("%s", fil);

fp = fopen(fil,"r");

if (fp == NULL)

printf("\nUnable to open file.\n");

printf("Please check if file exists and you have read privilege.\n");

exit(EXIT_FAILURE);

if(opt == 'e')

printf("The encrypted text: ");

while((ch=fgetc(fp)) != EOF)

if(ch == 'x')

ch = 'a';

else if(ch == 'y')

ch = 'b';

else if(ch == 'z')


ch = 'c';

else if(ch == ' ')

printf("%c", ch);

continue;

else

ch += 3;

printf("%c", ch);

else

printf("The decrypted text: ");

while((ch=fgetc(fp)) != EOF)

if(ch == 'a')

ch = 'x';

else if(ch == 'b')

ch = 'y';

else if(ch == 'c')

ch = 'z';

else if(ch == ' ')

printf("%c", ch);

continue;

else

ch -= 3;
printf("%c", ch);

fclose(fp);

8. Write a program to copy the contents of a text file into


another.
Modify program 9 to copy one existing file (source file, say
file1.txt) into another
named file (destination file, say file2.txt) such that both the
file names will be taken
as input through command line arguments.
Example: $./a.out file1.txt file2.txt
Here the contents of file1.txt have to be copied to file2.txt.
Ans:

#include<stdio.h>

#include<stdlib.h>
void main(int argc, char *argv[])

char ch;

FILE *fs, *ft;

fs = fopen(argv[1], "r");

if(fs == NULL)

printf("Error opening source file.");

exit(0);

ft = fopen(argv[2], "w");

if(ft == NULL)

printf("Error opening target file.");

exit(0);

while((ch = fgetc(fs)) != EOF)

fputc(ch, ft);

fclose(fs);

fclose(ft);

Output
Copied file.

You might also like