COMPUTER SCIENCES
ASSIGNMENT 4
Submitted To:- Miss VASUDHA RANCHAL.
Individual members of a structure can be passed to a function as argument in the function call. This method of passing
individual member is same as that of passing variable of any primitive type. A complete structure can also be passed to a
function. As structure members are stored in contiguous memory locations . While passing entire structure to a function we
do not have anything like length of the structure like in arrays.
Example:-
#include<stdio.h>
#include<conio.h>
void main()
struct address
char phone[15];
char city[25];
int pin;
};
struct emp
char name[25];
struct address a;
};
}
Q.2) Explain the method of passing a structure by value as an argument to a function with an
example of prototype function declaration , function call.
Ans:- #include<stdio.h>
#include<conio.h>
#include<string.h>
struct record
char name[20];
float balance;
};
void main()
clrscr();
getch();
b+=100;
c+=10;
return(b);
explanation:- In this program , we pass the members roll no and balance of record type variable e to the function funct1() as
arguments. This can be seen from the function call.
e.balance = func1(e.rollno,e.balance);
Q.3) What is self – referential structures find their application ? explain.
Ans:- A structure in which one member is the pointer to the parent structure is called self-referential structure. It contains
the value and the address of the memory block where the next element of the structure type is stored.
Struct list
char item[40];
};
By just altering the pointers the individual members can be easily added or deleted.
It is useful in applications involving linked lists , trees and graphs. The basic idea is that within each structure there is a
pointer which points to next element which is structure of the same type . The relative order of components can simply be
changed by altering the pointers. As a result , a linked data structure is not confined to some maximum number of
components. Rather, a data structure can expand or contract in size as required.
Eg:-
Array Structure:-
1) An array is the collection of data items of same data 1) A structure is a collection of data items of different
type and having common name. data types.
data_type member 1;
data_type member 2;
int a[30]; };
5) example:-
Structure
Struct student
char name[20];
int rollno. ;
Structure:- Union:-
1) It is composed to members where each member is 1) It is composed of members where all members share
assigned its own unique storage area. the same storage area within computer’s memory.
3) The reservation of memory is the total sum of 3) Memory assignment is that much that is required to
memories to be used by all the members of structure. access the largest member of union.
4) Example:- 4) Example:-
{ {
}; };
Q.6) Write a program that prompts the user to input name of a text file, read of the text file, and
output number of vowels and words in the text.
Ans:- #include<stdio.h>
#include<conio.h>
Void main()
file *f1;
char a , *name;
clrscr();
scanf(“%s”,name);
f1= fopen(name,”r”);
while((a=getc(f1))!=EOF)
vowel++;
if(a==’ ‘ || a==’ . ‘)
words ++;
fclose(f1);
getch();
Explaination:- The file is read character by character and it reads vowels and vowels are counted and if there is space or dot,
a word is counted.
Q.7) WAP to copy the contents of one FILE into another FILE .
Ans:- #include<stdio.h>
#include<conio.h>
void main()
{
char c;
file *fp1,*fp2;
fp1=fopen(“text.c”,”r’);
if(fp1==NULL)
printf(“read error”);
exit(1);
fp2=fopen(“text.c”,”w”);
if(fp2==NULL)
exit(1);
c=getc(fp1);
while(c!=EOF)
putc(c,fp2);
c=getc(fp1);
fclose(fp1);
fclose(fp2);
getch();
**********