Computer Assignment 4: Homework Title / No.: 4
Computer Assignment 4: Homework Title / No.: 4
Computer Assignment 4: Homework Title / No.: 4
: 4
Declaration:
I declare that this assignment is my individual work. I have not
copied from any other student’s work or from any other source
except where due acknowledgment is made explicitly in the text,
nor has any part been written for me by another person.
Student’s Signature :
Evaluator’s comments:
__________________________________________________________________
___
COMPUTER ASSIGNMENT 4
Q1. How can you relate the function with the structure? Explain with an
appropriate example.
Solution: Structure is a derived data type like array. In function we pass values to
variable from main() to user defined function to alter the values. Like other
variables we can also pass structure to a function.
For eg.
#include<stdio.h>
#include<conio.h>
struct clas
char section[10];
void f(clas c)
c.nos++;
strcpy(c.section,”abc”);
printf(“values in function”);
printf(“Section is %s”,c.section);
void main()
clas c1;
printf(“Enter no of students”);
scanf(“%d”,&c1.nos);
scanf(“%s”,c1.section);
printf(“Section is %s”,c1.section);
f(c1);
printf(“Section is %s”,c1.section);
In the program clas is a structure having its data members nos (no of students) and
section;
In the main we declare the variable of type clas and scan the value of its variable
using scanf ()and printf(). Then we pass the variable c1 of type structure to the
f().In function f values of c1 copied to the variable c . In the f() by using “.”
Operator we can acces the values of c of type clas.In this the changes doesn’t
reflect back to the main().
Output:
Enter no of students 25
No of students are 25
Section is G1001
Values in function
No of students are 26
Section is abc
No of students are 25
Section is G1001
#include<stdio.h>
#include<conio.h>
Struct clas
Void main()
{{
char section[10];
c.nos++;
strcpy(c.section,”abc”);
printf(“values in function”);
printf(“Section is %s”,c.section);
}
void main()
clas c1;
printf(“Enter no of students”);
scanf(“%d”,&c1.nos);
scanf(“%s”,c1.section);
printf(“Section is %s”,c1.section);
f(c1);
printf(“Section is %s”,c1.section);
It is same as previous but the difference is this that the changed values reflected
back to the main function.
Output:
Enter no of students 25
No of students are 25
Section is G1001
Values in function
No of students are 26
Section is abc
No of students are 26
Section is abc
Solution:
#include<stdio.h>
#include<conio.h>
struct emp
char n[21];
int eno;
long basic;
int exp.
};
void fun(emp e)
void main()
emp e1;
scanf(“%s%d”,e1.n, e1.eno);
scanf(“%ld%d”,e1.basic,e1.exp);
fun(e1);
Output:
Ram
15000
Employee no—1
experience –2
Solution: A structure in which its one data member is a pointer of its type and
point the structure itself is self referential structures.
Foe eg;
Struct s
Int a;
Char b;
S *c;
};
It is a self referential structure because it contain a *c of its type. With the help of c
we can point the another variable of same structure. It stores the address of other
structure. Similary it forms the chain we can use the information part of the
structure without declaring any array of structure.
Application:
Now consider a problem in which se have to store the data of some employees.
here we can declare the structure of array but as we know that in doing that we
have to specify the memory which can’t be change during the execution of
program. So we use self referential structures in linked list,stacks and queues .
In this the *c stores the address of proceeding variable and this formed the chain
and we can use the referential structures using”→”( arrow) operator
Q4. Is it necessary that a file created in text mode must always be opened
in text mode for subsequent operations? What is the appropriate answer?
Explain with example.
Solution:
Yes, it is necessary that a file created in text mode must always be opened in text
mode for subsequent operations.
When we work with text files then conversions takes place from binary to text and
vice versa. While in binary mode these conversions doesn’t takes place. In text
mode a character whose ASCII value is 26 is inserted by the end of last character
which is treated as a end of file. So if a file is stored in binary mode and then we
open it in text mode and let us consider that file contain number 26 and this no is
detected while reading in a text mode then our program would terminate at that
point.
Thus two modes are not compatible. The file written in text mode is read back only
in text mode and file written in binary mode must be read back only in binary mode.
Solution:
Structure
Array
index no.
3.Eg. 3. Eg.
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h>
{ {
};
printf(“%d”,a[0]);
void main()
printf(“%d”,a[1]);
{ s s1; getch();
s1.a=10; s1.b=’a’; }
printf(“%d”,s1.a);
printf(“%c”,s1.b);
getch();
Difference between structure and union: In structure and unions there is only
one difference that is memory allocation. In structure memory allocated separately
for each an every variable but in unions memory allocated to only that variable
whose size is max. this can be understand by the following example:
#include<stdio.h>
#include<conio.h>
union un
{
char c[21];
int a[2];
}u1;
struct s
char c[21];
int a[2];
}s1;
void main()
getch();
Output:
Size of union 21
Size of structure 25
In union size of string which is max. size in all the members is total size of union .
Q6. WAP that prompts the user to input name of a text file, read the text
file & output number of vowels and words in the text.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
FILE *f;
char c;
int now=0,nov=0;
f=fopen(“file.txt”,”r”);
while(!feof(f))
c=getc(f);
if(c==’ ‘)
now++;
if(c==’a’||c==’A’||c==’e’||c==’E’||c==’I’||c==’I’||c==’o’||c==’O’||c==’u’||c==’U’)
nov++;
fclose(f);
getch();
Q7. Write a program to copy the contents of one FILE into another FILE.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
FILE *f1,*f2;char c;
f1=fopen(“file1.txt”,”r”);
f2=fopen(“file2.txt”,”w);
while(!feof(f1))
c=getc(f1);
fputc(c,f2);
fclose(f1);
fclose(f2);
getch();