Vous êtes sur la page 1sur 14

Homework Title / No.

: 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:
__________________________________________________________________
___

Marks obtained : ___________ out of ______________________

Content of Homework should start from this page only:

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.

Structure passed to the function by value:

#include<stdio.h>

#include<conio.h>

struct clas

int nos; //no of students

char section[10];

void f(clas c)

c.nos++;

strcpy(c.section,”abc”);

printf(“values in function”);

printf(“No of students are %d”,c.nos);

printf(“Section is %s”,c.section);

void main()

clas c1;
printf(“Enter no of students”);

scanf(“%d”,&c1.nos);

printf(“enter section of class”);

scanf(“%s”,c1.section);

printf(“ Values before the function call”);

printf(“No of students are %d”,c1.nos);

printf(“Section is %s”,c1.section);

f(c1);

printf(“ Values after the function call”);

printf(“No of students are %d”,c1.nos);

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

Enter section of class G1001

Values before the function call

No of students are 25

Section is G1001

Values in function

No of students are 26
Section is abc

Values after the function call

No of students are 25

Section is G1001

Structure passed to the function by reference

#include<stdio.h>

#include<conio.h>

Struct clas

Void main()

{{

int nos; //no of students

char section[10];

void f(clas &c)

c.nos++;

strcpy(c.section,”abc”);

printf(“values in function”);

printf(“No of students are %d”,c.nos);

printf(“Section is %s”,c.section);

}
void main()

clas c1;

printf(“Enter no of students”);

scanf(“%d”,&c1.nos);

printf(“enter section of class”);

scanf(“%s”,c1.section);

printf(“ Values before the function call”);

printf(“No of students are %d”,c1.nos);

printf(“Section is %s”,c1.section);

f(c1);

printf(“ Values after the function call”);

printf(“No of students are %d”,c1.nos);

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

Enter section of class G1001

Values before the function call

No of students are 25

Section is G1001
Values in function

No of students are 26

Section is abc

Values after the function call

No of students are 26

Section is abc

Q2. Explain the method of passing a structure by value as an argument to


a function with an example of prototype function declaration, function
call.

Solution:

#include<stdio.h>

#include<conio.h>

struct emp

char n[21];

int eno;

long basic;

int exp.

};
void fun(emp e)

printf(“Details of employee are :“)

printf(“\t name-- %s”,e.n);

printf(“\tEmployee no-- %d”,e.eno);

printf(“ \tBasic pay -- %ld” e.basic);

printf(“\texperience -- %d” e.exp);

void main()

emp e1;

printf(“Enter Details of an employee(name, employee no,basic pay,experience”);

scanf(“%s%d”,e1.n, e1.eno);

scanf(“%ld%d”,e1.basic,e1.exp);

fun(e1);

Output:

Enter Details of an employee(name, employee no,basic pay,experience

Ram

15000

Details of emplyoyee are :


name-- Ram

Employee no—1

Basic pay –15000

experience –2

Q3. What are self-referential structures find their applications? Explain.

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.

Q5. How a structure is different from an array & union?

Solution:

Difference between structure and array:

Structure
Array

1. It is used to store different types of variables in a same . 1. Array contain


same type of elements.
2.In this variables are accessed by using ”.” operator. 2.In this variables are
accessed by using

index no.

3.Eg. 3. Eg.

#include<stdio.h>
#include<stdio.h>

#include<conio.h>
#include<conio.h>

struct s void main()

{ {

int a; char b; int a[2]={


1,2};

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

printf(“ Size of union %d”,sizeof(u1));

printf(“Size of structure %d”,sizeof(s1));

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 .

In structure total size is sum of size of all the data types.

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

printf(“No of vowels %d”,nov);

printf(“No of words %d”,now+1);

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

Vous aimerez peut-être aussi