Anda di halaman 1dari 9

Lovely Professional University

COMPUTER SCIENCES

ASSIGNMENT 4
Submitted To:- Miss VASUDHA RANCHAL.

SUBMITTED BY:- ANKIT KUMAR PATHAK.


ROLL NO.:- RF1003A06
REG. NO:- 11005356
GROUP:- 1
SECTION:- F1003
Q.1) How can you relate the function with structure ? Explain with an appropriate example.
Ans: - A function is a self contained block of statements enclosed between braces that performs a specific task.

A structure is a collection of heterogenous elements which are related to one another.

Two ways of passing structure to a function:-

Pass by value - individual members are passed to function.

Pass by reference :- entire structure is passed by use of pointer variable.

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;

};

struct emp e={“jasmeen”,”9217706769”,”Nagaland”,13};

printf(“\n name=%s phone=%s,e.name,e.a.phone);

printf(“\n city =%s pin =%d”,e.a.city,e.a.pin);

}
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];

int roll no;

float balance;

};

float funct1(int ,float);

void main()

struct record e={“anshu”,12,10.50};

clrscr();

printf(“\n before call value=%s %d %f”,e.name,e.rollno,e.balance);

e.balance= funct1(e.rollno, e.balance);

printf(“\n after call value =%s %d %f”,e.name,e.rollno,e.balace);

getch();

float funct1(int c, float b)

b+=100;

c+=10;

printf(“\n in function, values=%d %f”,c,b);

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

struct list *next;

};

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

ram sumit neha Null


“PART – B”
Q.4) 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 an example.
Ans:- Yes,it is necessary that a file created in text mode must always be opened in text mode for subsequent operations.
Because when open a file in library mode it gives us file in graphic which is not easily understandable by user.
e.g.
#include<stdio.h>
#include<conio.h>
Void main()
{
FILE *f1
Char name[];
Int age;
Float salary;
Char designation;
f1=fopen(‘employ,text’,”w++”);
fscanf(f1”%s%d%f%s”,&name&age&salary&designation);
fprintf(f1,”%s%d%f%s”,name,age,salary,designation);
fclose(f1);
getch();
}
Q.5) How a structure is different from an array and union ?
Ans: - Basic difference between structures and arrays:-

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.

2) It has declaration only. 2) It has declaration and definition.

3) There is no keyword. 3) The keyword struct is used.

4) Syntax for 1-d array:- 4) Syntax:-

Storage_class data_type array name[expression] Struct struct_name

data_type member 1;

data_type member 2;

5) example:- -------- ---------- --

Array data_type member n;

int a[30]; };

5) example:-

Structure

Struct student

char name[20];

int rollno. ;

Basic difference between structure and union:-

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.

2) Struct is the keyword used. 2) Union is the keyword used in it.

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

Struct date Union date

{ {

int day; int day;


int month; int month;

int year; int year;

}; };

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

int vowel=0 , words=0;

file *f1;

char a , *name;

clrscr();

printf(“\n enter the name of the file :”);

scanf(“%s”,name);

f1= fopen(name,”r”);

while((a=getc(f1))!=EOF)

if (toupper(a)==’A’ || toupper(a)==’E’ || toupper(a)==’I’ || toupper(a)==’O’|| toupper(a)==’U’)

vowel++;

if(a==’ ‘ || a==’ . ‘)

words ++;

fclose(f1);

printf(“\n number of vowels are %d “, vowels);

printf(“\n number of words are %d “, words);

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)

printf(“file not opened”);

exit(1);

c=getc(fp1);

while(c!=EOF)

putc(c,fp2);

c=getc(fp1);

fclose(fp1);

fclose(fp2);

getch();

**********

Anda mungkin juga menyukai