Anda di halaman 1dari 7

LOVELY PROFESSIONAL

UNIVERSITY
ASSIGNMENT-04
CSE(101)

Submitted to:- Submitted by:-


Ms. Amandeep Kaur Name-Jashanjeet Singh
Section-G4005
Roll no.-RG4005A19
Reg. no.-11009091
Q1. How can you relate the function with the structure? Explain with an
appropriate example.

Ans 1-Relation between function and structure is that both are used to pass values
to do some work from the main function. Both can also be easily related with the
function as both are declared and defined almost in the same manner. Both are
declared and defined outside the main function.
For example:-

• In functions-

#include<stdio.h>

#include<conio.h>

Void add()

Int a,b,c;

Scanf(“%d”,a);

Scanf(“%d”,b);

C=a+b;

Void main()

{
add;

getch();

• In structures

#include<stdio.h>

#include<conio.h>

Struct employee

Int a;

Char b;

};

Void main()

Struct employee e;

Scanf(“%d”,e.a);

Scanf(“%d”,e.b);

Getch();

As in both the case declaration and definition is almost same.


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.

Ans 2
void student1();
struct Student
{
char name[20];
int roll;
}s[10];
void main()
{
for(i-0;i<10;i++)
{
gets(s[i],name);
scanf(“%d”,&s[i].roll);
}
stud(struct Student s[10]);
getch();
}
void student1()
{
for(i=0;i<10;i++)
{
puts(s[i].name);
printf(“%d”.s[i].roll);
}
}
Q3. Where self-referential structures find their applications? Explain.

Ans 3-A self -referential structure provides definition to structure variables.


struct Stud{
int a;
struct Stud *ptr;
};

Self-referential find their application in linked lists. When we have to make the
linked lists self- referential structures find their application

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.
Ans 4-Yes a file opened in text mode has to opened in text mode foe further
operations because of several reasons. The first reason is in text modes a new line
character is converted into a return-linefeed combination before written to the disk
and similarly converted back to new line when file is read by C program so if
opened in binary mode these conversions will not take place. The second reason is
in text files end of a file is marked by a character of ASCII value 26. But in binary
files the track of the end of the file is kept by the number of characters present in
the array.
Thus this two modes are incompatible and a particular type of file should be
always opened in that particular mode.
Q5. How a structure is different from an array & union?
Ans 5-A structure is different from array and union by the property of memory
allocation as in structure all the variables allocate different memory locations to all
the variables. A structure is a collection of dissimilar data types whereas array is a
collection of similar type of data and in array contigious memory locations are
allocated to all the variables but in case of union variable which has maximum size
the memory of that variable is allocated to all the variable of different 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.

Ans 6-#include<stdio.h>
#include<conio.h>
void main()
{
FILE *a,*b;
char c,b[20];
a=fopen(“A.c”,”r”);
if(a==’\0’)
{
puts(“No file”);
exit(1);
}
b=fopen(“B.c”,”w”);
if(b==’\0’)
{
puts(“No file”);
fclose(b);
exit(1);
}
while(1)
{
ch=fgetc(a);
if(c==EOF)
break;
else
{
if(ch==’a’|| ch==’e’|| ch==’i’|| ch==’o’|| ch==’u’||ch==’A’||ch==’E’||
ch==’I’ch==’O’||ch==’U’)
v++;
if(ch==’ ‘)
sp++;
}
printf(“No. of variables in the file are :%d”,v);
printf(“No. of words in the file are %d”,(sp+1));
fclose(a);
fclose(b);
}