0% found this document useful (0 votes)
16 views

POP Set - 3 Solution

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

POP Set - 3 Solution

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

POP Model paper – 3

1. a) Explain the components of a computer with a neat diagram.


Ans:

There are three main components of computer. They are:


i) Input Unit: The input unit consists of one or more input devices. The keyboard of the computer is one of the
most commonly used and standardized input devices. Regardless of the type of input device used in a
computer system, all input devices perform the following functions:
 Accept data and instruction from the outside world.
 Convert it to a form that the computer can understand.
 Supply the converted data to the computer system for further processing.
ii) Central Processing Unit: It is known as the heart or brain of the computer system. All operations are carried
out in this unit only. The CPU is sub-divided into 3 parts:
a) Control Unit: The control unit instructs the computer how to carry out program instructions. It
directs the flow of data between memory and ALU.
b) Arithmetic and Logical Unit (ALU): Arithmetic and logical unit performs all the arithmetic
operations (such as +, -, *, /) and logical operations (such as AND, OR, NOT).
c) Memory Unit: Memory unit is the part of computer which holds data for processing and other
information. It stores temporarily in primary memory and permanently in secondary memory.
iii) Output Unit: These devices used to get the response or result from the processor. Output unit is the
communication between the user and the computer. The output unit of a computer provides the information
and results of a computation to the outside world.

b) Describe any three input devices.


Ans:
Keyboard:
 Most common and very popular input device is keyboard.
 The keyboard helps in inputting the data to the computer.
 The layout of the keyboard is like that of traditional typewriter, although there are some additional keys
provided for performing some additional functions.
 Keyboards are of two sizes 84 keys or 101/102 keys, but now 104 keys or 108 keys keyboard is also available
for Windows and Internet.(Alpha numeric keys(A-Z, 0-9), Functional keys(F1-F12),Control keys, Special
purpose keys).
Mouse:
 Mouse is most popular Pointing device. It is a very famous cursor-control device. It is a small palm size box
with a rounded ball at its base which senses the movement of mouse and sends corresponding signals to
CPU on pressing the buttons. Generally, it has two buttons called left and right button and scroll bar is
present at the mid. Mouse can be used to control the position of cursor on screen, but it cannot be used to
enter text into the computer.
Scanner:
Scanner is an input device which works more like a photocopy machine. It is used when some information is
available on a paper and it is to be transferred to the hard disc of the computer for further manipulation. Scanner
captures images from the source which are then converted into the digital form that can be stored on the disc.
These images can be edited before they are printed.

c) Design an algorithm, flowchart and program to compute area of a circle.


Ans:
Algorithm: Start
Step 1: start
Step 2: read r
Step 3: compute area=pi*r*r Read r
Step 4: display area
Step 5: stop

Program: compute area=pi*r*r


#include<stdio.h>
#define pi 3.142
void main() Display area
{
float r,area;
printf(“Enter radius value\n”); Stop
scanf(“%f”,&r);
area = pi*r*r;
printf(“Area of circle = %f”,area);
}

2. a) Define Identifiers and explain its rules. State whether the following identifiers are valid or invalid with
justification. i)$roll no ii)_name123 iii)If iv)Name__123
Ans:
Variable is a named location in a memory where a program can manipulate the data.
The rules to construct variables are:
 They can contain letters, digits and underscores.
 They should not begin with any special characters except underscore (_).
 They are case sensitive.
 They cannot contain whitespaces or special characters like !, #, %, etc.
 Reserved words cannot be used as variables(such as int, float, char etc.).

Valid: _name123, If, Name__123.


Invalid: $roll no.

b) Summarize the formatted input and output statements with suitable syntax and example.
Ans:
iii) scanf(): -
The scanf() function is an input function. It used to read the mixed type of data from keyboard. You
can read integer, float and character data by using its control codes or format codes.
Syntax: scanf(“control strings”,arg1, arg2, ….., argn);
Example: scanf(“%d %f”,&a,&b);

iv) printf(): - This is an output function. It is used to display a text message and to display the mixed type (int,
float, char) of data on screen.
Syntax: printf(“control strings”,arg1, arg2,…..,arg3);
Example: printf(“%d %f”,a,b);
c) Explain the SDLC life cycle for the efficient design of a program with a neat diagram.
Ans:

Stage 1: Planning requirement Analysis


 Requirement analysis is the most important and fundamental stage in SDLC. It is performed by the senior
members of the team with inputs from the customer, the sales department, market surveys and domain experts
in the industry. This information is then used to plan the basic project approach and to conduct product
feasibility study in the economical, operational and technical areas.
 Planning for the quality assurance requirements and identification of the risks associated with the project is also
done in the planning stage. The outcome of the technical feasibility study is to define the various technical
approaches that can be followed to implement the project successfully with minimum risks.

Stage 2: Defining requirements


 Once the requirement analysis is done the next step is to clearly define and document the product requirements and
get them approved from the customer or the market analysts. This is done through an SRS (Software
Requirement Specification) document which consists of all the product requirements to be designed and
developed during the project life cycle.

Stage 3: Designing the product architecture


 SRS is the reference for product architects to come out with the best architecture for the product to be developed.
Based on the requirements specified in SRS, usually more than one design approach for the product architecture
is proposed and documented in a DDS - Design Document Specification.
 This DDS is reviewed by all the important stakeholders and based on various parameters as risk assessment,
product robustness, design modularity, budget and time constraints, the best design approach is selected for the
product.
 A design approach clearly defines all the architectural modules of the product along with its communication and
data flow representation with the external and third party modules (if any). The internal design of all the
modules of the proposed architecture should be clearly defined with the minutest of the details in DDS.

Stage 4: Building or Developing the product


 In this stage of SDLC the actual development starts and the product is built. The programming code is generated
as per DDS during this stage. If the design is performed in a detailed and organized manner, code generation
can be accomplished without much hassle.
 Developers must follow the coding guidelines defined by their organization and programming tools like compilers,
interpreters, debuggers, etc. are used to generate the code. Different high level programming languages such as
C, C++, Pascal, Java and PHP are used for coding. The programming language is chosen with respect to the
type of software being developed.
Stage 5: Testing the product
 This stage is usually a subset of all the stages as in the modern SDLC models, the testing activities are mostly
involved in all the stages of SDLC. However, this stage refers to the testing only stage of the product where
product defects are reported, tracked, fixed and retested, until the product reaches the quality standards defined
in the SRS.
Stage 6: Deployment in the market and maintenance
 Once the product is tested and ready to be deployed it is released formally in the appropriate market. Sometimes
product deployment happens in stages as per the business strategy of that organization. The product may first be
released in a limited segment and tested in the real business environment (UAT- User acceptance testing).
 Then based on the feedback, the product may be released as it is or with suggested enhancements in the targeting
market segment. After the product is released in the market, its maintenance is done for the existing customer
base.

3. a) Describe any 4 types of operators in C with example.


Ans:
i) Arithmetic operators: Arithmetic operators are used to perform mathematical calculations like addition,
subtraction, multiplication, and division in C programs.
Operator Operation Example
+ Addition A+B
- Subtrction A-B
* Multiplication A*B
/ Division A/B
% Modulo division A%B
ii) Assignment operator: Assignment operator is used for assigning a value of an expression to another
variable.
Operator Description Example
= Simple assignment operator C = A+B assigns sum value of A & B to C
+= Add and assignment operator C += A is equivalent to C = C+A
-= Subtract and assignment operator C - = A is equivalent to C = C-A
*= Multiply and assignment operator C *= A is equivalent to C = C*A
/= Divide and assignment operator C /= A is equivalent to C = C/A
%= Modulus and assignment operator C %= A is equivalent to C = C%A

iii) Logical operator: Operators used with one or more operand and return either value zero(false) or one(true)
are called logical operators. There are 3 basic logical operators:
Operator Operation Example
AND(&&) Takes two or more inputs and produces high/true/1 if all 12&&24 = 1
inputs are high else low/false/0 12&&0 = 0
0&&0 = 0
OR(||) Takes two or more inputs and produces high/true/1 if any one 12||24 = 1
input is high else low/false/0 0||0 = 0
12||0 = 1
NOT(!) Takes one input and inverts output. low to high, high to low. !(12&&0) = !(0) = 1
!(0 || 0) = !(1) = 0

iv) Relational operator: Relational operators are used to determine the relationship of one quantity to another.
It is used to compare values of two expressions depending on their relation. They always return 1 or 0
depending upon the outcome of the test.
Operator Description Example
== Equal to A==B
!= Not equal to A!=b
< Less than A<B
> Greater than A>B
<= Less than or equal to A<=B
>= Greater than or equal to A>=B
b) Differentiate between type conversion and type casting in C.
Ans:
Type conversion(implicit conversion) Type casting(explicit conversion)
The automatic conversion of one datatype to another The forced conversion of one datatype to another
datatype implicitly is called type conversion datatype explicitly is called type casting
Implemented on two compatible datatypes Implemented on two incompatible datatypes
No operator is required Casting operator is required
It is implicitly done during compiling It is done during the program execution
It is a widening conversion It is a narrow conversion
Example: Example:
#include<stdio.h> #include<stdio.h>
main( )
void main()
{
{ float f_num;
float x; int i_num;
int y=3; printf(“ enter any integer:\n”);
scanf(“%d”, &i_num);
x=y;
f_num =(float)i_num;
printf(“X= %f”,x); printf(“ the floating-point number of %d is= %f ”, i_num, f_num);
} }

c) Write a program to compute the roots of quadratic equation by accepting coefficients.


Ans: #include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,d,real,img,root1,root2;
printf(“\nEnter the values of a,b,c\n”);
if(a = = 0)
printf(“\n Invalid coefficients, roots cannot be computed\n”);
else
{
d=b*b-4*a*c;
printf(“\n The value of discriminant(d) is = %f”,d);
if(d = = 0)
{
root1=root2= -b/(2*a);
printf(“\n The real and equal roots are: root1 = root2 = %.2f”,root1);
}
else if(d > 0)
{
root1 = (-b+sqrt(d))/(2*a);
root2 = (-b-sqrt(d))/(2*a);
printf(“\n The real and distinct roots are: root1=%.2f & root2=%.2f”,root1,root2);
}
else
{
real = -b/(2*a);
img = sqrt(fabs(d))/(2*a);
printf(“\nThe Complex roots are: root1=%.2f+%.2fi & root2=%.2f-%.2fi”,real,img,real,img);
}
}
}
4. a) Define looping. Explain while and do-while with suitable example.
Ans:
Looping is a process of executing some block of statement until some condition is satisfied.
While Loop: It is a loop in which if the condition is true, the body of the loop is executed else the program
control comes out of the loop. It is also called as entry control loop.
Example:
#include<stdio.h>
void main()
{
int i=1;
printf(“ The numbers from 1 – 10 are:”);
while(i<=10)
{
printf(“ %d \n”, i);
i++;
}
}

Do-while Loop: It is a loop in which if the condition is true, the body of the loop is executed else the program
control comes out of the loop. But here it checks the condition at the end of block. It is also called as exit
control loop.
Example:
#include<stdio.h>
void main()
{
int i=1;
printf(“ The numbers from 1 – 10 are:”);
do
{
printf(“ %d \n”, i);
i++;
} while(i<=10) ;
}
b) Implement a C program to simulate a simple calculator that performs arithmetical operations using
switch statement.
Ans: #include<stdio.h>
void main()
{
char op;
float n1,n2,result=0;
printf(“\nEnter an operator(+,-,*,/): ”);
scanf(“%c”,op);
printf(“\nEnter the value of two operands: n1 and n2: ”);
scanf(“%f %f”,&n1,&n2);
switch(op)
{
case ‘+’:result = n1+n2;
break;
case ‘-’:result = n1-n2;
break;
case ‘*’:result = n1*n2;
break;
case ‘/’:result = n1/n2;
break;
default:printf(“\nYou have entered an invalid operator”);
}
printf(“%f %c %f = %f”,n1,op,n2,result);
}

c) Explain unconditional statements with example.


Ans:
 These are the control constructs which allow the execution to move from one part of the program to another
part of the program without any condition. These are also called as jump statements.
 There are 3 types:
i) break: The break is a keyword in C which is used to bring the program control out of the loop. It is used
inside loops or switch statement.
Example:
#include<stdio.h>
void main()
{
int i;
for(i=1;i<10;i++)
{
printf(“%d\t”,i);
if(i==5)
break;
}
printf(“Came outside of loop at i = %d”,i);
}
ii) continue: The continue statement in C language is used to bring the program control to the beginning
of the loop. The continue statement skips some lines of code inside the loop and continues with
the next iteration.
Example:
#include<stdio.h>
void main()
{
int i=0;
while(i!=10)
{
i++;
if(i==5)
continue;
printf(“%d\t”,i);
}
}

iii) goto: The goto statement is used to transfer the program control to a predefined label. It can be used to
repeat some part of the code for a particular condition. It can also be used to break the multiple
loops which can't be done by using a single break statement.
Example:
#include <stdio.h>
void main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
}
5. a) Explain the syntax of Function Declaration and Function Definition with example.
Ans:
Declaration  Syntax  return_type function_name(arguments);
Definition  Syntax  return_type function_name(arguments)
{
Local declaration statements;
Executable statements;
return statement;
}
Example:
#include <stdio.h>
int add (int x, int y); //function declaration
void main ( )
{
int a,b,c;
printf (“enter any two numbers/n”);
scanf(“%d%d”, &a, &b);
c=add (a,b);
printf(“sum =%d”, c);
}
int add (int x, int y)
{
int sum;
sum =x+y; Function Definition
return sum;
}

b) Write a C program to swap two integers using call by value method of passing arguments to a
function.
Ans: #include<stdio.h>
void swapping(int a, int b);
void main()
{
int x,y;
printf(“Enter x, y values\n”);
scanf(“%d %d”,&x,&y);
printf(“Before swapping\n”);
printf(“%d %d”,x,y);
swapping(x,y);
}
void swapping(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf(“After swapping\n”);
printf(“%d %d”,a,b);
}
c) Describe different types of storage classes with example.
Ans:
 A storage class defines the scope (visibility) and life-time of variables and/or functions
within a C Program. These specifiers precede the type that they modify.
 There are the following storage classes, which can be used in a C Program:
 auto
 register
 static
 extern
Storage Storage Default value Scope Lifetime Example
classes place
auto RAM Garbage Value Local Within the function auto a=14;
register Register Garbage Value Local Within the function register b=10;
static RAM Zero Local Till the end of the main program, Retains value static c=27
between multiple functions call
extern RAM Zero Global Till the end of the main program, Maybe declared extern d=85
anywhere in the program

6. a) What is an array? Explain how two dimensional arrays are declared and initialized.
Ans:
Array is an ordered collection of homogeneous elements stored in contagious memory location.
Declaration of 2D array  Syntax  datatype array_name[row_size][column_size];
Initialization of 2D array  Syntax  datatype array_name[row_size][column_size]={v1,v2,v3,………,vn};
Example: #include<stdio.h>
void main()
{
int a[2][3]={1,2,3,4,5,6},i,;
printf(“The array elements are: \n”);
for (i=0;i<2;i++)
for(j=0;j<3;j++)
printf(“%d\t”, a[i][j]);
}

b) Write a C program to find transpose of a 3x3 matrix.


Ans: #include<stdio.h>
void main()
{
int M[3][3], T[3][3],i,j;
printf(“Enter elements of matrix M:”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”,&M[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
T[i][j]=M[j][i];
printf(“The elements of transpose matrix T are: \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf(“%d\t”,T[i][j]);
printf(“\n”);
}
}

c) Discuss any three operations that can be performed on arrays with example.
Ans:
i) Insertion: The process of adding a new element into the array is called insertion. We can insert an element at
any position in the array.
Example:
#include<stdio.h>
void main()
{
int a[100],i,n,ele,pos;
printf(“Enter n value\n”);
scanf(“%d”,&n);
printf(“Enter %d elements into array\n”,n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter the position of inserting element\n”);
scanf(“%d”,&pos);
printf(“Enter the insering element\n”);
scanf(“%d”,&ele);
for(i=n;i>=pos;i--)
a[i+1]=a[i];
a[pos]=ele;
printf(“Array elements after insertion\n”);
for(i=0;i<=n;i++)
printf(“%d\t”,a[i]);
}

ii) Deletion: The process of removing an existing element from the array is called deletion. We can remove the
element from any position in the array.
Example:
#include<stdio.h>
void main()
{
int a[100],i,n,ele,pos;
printf(“Enter n value\n”);
scanf(“%d”,&n);
printf(“Enter %d elements into array\n”,n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter the position of deleting element\n”);
scanf(“%d”,&pos);
ele=a[pos];
for(i=pos;i<n;i++)
a[i]=a[i+1];
printf(“The deleted element is = %d”,ele);
printf(“Array elements after deletion\n”);
for(i=0;i<n-1;i++)
printf(“%d\t”,a[i]);
}

iii) Sorting: The process of arranging the array elements in either ascending or descending order is called
sorting.
Example:
#include<stdio.h>
void main( )
{
int a[10],n,i,j,temp;
printf(“enter the size of array:\n”);
scanf(“%d”,&n);
printf(“enter the array elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf(“ the sorted array is:\n”);
for(i=0;i<n;i++)
printf(“%d”,a[i]);
}

7. a) What are strings? Mention different operations that can be performed on strings? Explain any two
with an example?
Ans:
A string is a sequence of characters enclosed between double quotes.
The operations that can be performed on strings are:
i) strlen( )
ii) strupr( )
iii) strlwr( )
iv) strcmp( )
v) strcat( )
vi) strcpy( )
vii) strrev( )
viii) strstr( )
ix) strchr( )
x) strrchr( )
1)strlen: This string function is basically used for the purpose of computing the length of string.
Syntax: strlen(str);
Example:
#include<stdio.h>
void main()
{
char str[10]= “COMPUTER”;
int len = strlen(str);
printf(“The length of string is = %d”,len);
}

2)strupr: This string function is used to convert the string to uppercase.


Syntax: strupr(str);
Example:
#include<stdio.h>
void main()
{
char str[10]= “computer”;
printf(“The uppercase of string is = %s”,strupr(str));
}

b) Explain array of strings with an example?


Ans:
In C programming String is a 1-D array of characters and is defined as an array of characters. But an array of
strings in C is a two-dimensional array of character types. Each String is terminated with a null character (\0). It
is an application of a 2d array.
Syntax: char variable_name[r][c] = {list of string};
Here,
 var_name is the name of the variable in C.
 r is the maximum number of string values that can be stored in a string array.
 c is a maximum number of character values that can be stored in each string array.
Example:
#include <stdio.h>
void main()
{
char arr[4][10] = {“Harish”, “Karan”, “Naveen”, “Sumit”};
printf("String array Elements are:\n");
for (int i=0; i<4;i++)
printf("%s\n",arr[i]);
}
c) Discuss the working of the following string manipulation functions with suitable code:
i)strchr ii)strspn iii)strcmp iv)strcpy
Ans:
i) strchr: This function searches for the first occurrence of an unsigned character in the string pointed to by the
argument str. The function returns a pointer pointing to the first matching character or null if no match
was found.
Example:
#include<stdio.h>
#include<string.h>
void main()
{
char str[50]=“PROGRAMMING IN C”;
char *pos;
pos=strchr(str,‘n’);
if(pos)
printf(“n is found in str at position %d”,pos);
else
printf(“n is not present in the string”);
}

ii) strspn: This function returns the index of the first character in str1 that doesn’t match any character in str2.
Example:
#include<stdio.h>
#include<string.h>
void main()
{
char str1[]= “HAPPY BIRTHDAY TO YOU”;
char str2[]= “HAPPY BIRTHDAY JOE”;
printf(“The position of first character in str2 that does not match with that in str1 is %d”,strspn(str1,str2));
}

iii) strcmp: This string function is basically used for the purpose of comparing two strings. It compares two
strings character by character. Thus it gives result in three cases:
case-1: if str1 > str2, result is true.
case-2: if str1 < str2, result is false.
case-3: if str1= = str2, result is zero.
Example:
#include<stdio.h>
#include<string.h>
void main()
{
char str1[10]= “computer”;
char str2[10]= “science”;
if(strcmp(str1,str2)==0)
printf(“The strings are identical\n”);
else
printf(“The strings are not identical”);
}
iv) strcpy: This string function is basically used for the purpose of copying one string into another string.
Example:
#include<stdio.h>
#include<string.h>
void main()
{
char str1[10]= “computer”;
char str2[20];
str2 = strcpy(str2,str1);
printf(“%s”,str2);
}

8. a) Define pointers and explain how to declare a pointer variable? Differentiate null pointer and void
pointer with suitable example?
Ans:
Pointer is a special variable which stores the address of another variable.
Declaration  Syntax  datatype *pointer_name;
Null pointer Void pointer
It does not point to any address of any type. It can point to any address of any type.
Null pointer are created using the NULL keyword Void pointers are created using the void keyword in
with assignment operator. the place where the datatype is placed.
It does not hold any address It can hold address of any datatype
It is faster compared to void pointer It is slower compared to null pointer
It is a bad programming approach It increases the flexibility of program
It has nothing to do with type casting It excludes programmers from type casting a pointer
repeatedly.
Syntax: datatype *pointer_name = NULL; Syntax: void *pointer_name;
Example: Example:
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
printf(“C Programming”); int a = 10;
int *p=NULL; void *ptr = &a;
printf(“The value of pointer is: %d”,p); printf("%d", *(int *)ptr);
} }

b) Write a program to add two integers by passing pointer variable as parameters to functions?
Ans:
#include<stdio.h>
void add(int *a, int *b);
void main()
{
int x,y;
printf(“Enter two integers\n”);
scanf(“%d %d”,&x,&y);
add(&x,&y);
}
void add(int *a, int *b)
{
int s;
s=*a+*b;
printf(“Sum = %d”,s);
}
c) Write a program to print all even numbers from m to n using pointers?
Ans:
#include<stdio.h>
void main()
{
int i,m,n;
printf(“Enter m & n values\n”);
scanf(“%d %d”,&m,&n);
for(i=m;i<=n;i++)
if(i%2==0)
printf(“%d\t”,i);
}

9. a) What is structure? Explain the C syntax of structure declaration with example.


Ans:
Structure is a user-defined data type available in C that allows to combining of data items of different kinds.
Declaration  Syntax:
struct structure_name
{
member definition;
member definition;
...
member definition;
};
Example:
struct employee
{
char ename[10];
int e_id;
int age;
float salary;
char address[10];
};
b) Differentiate between Structure and Union.
Ans:
Si. No. Sturcture Union
1 Structures is a user-defined data type Union is a special data type available
available in C that allows to combining in C that allows storing different data
of data items of different kinds. types in the same memory location.
2 It allocates memory equal to sum of It allocates piece of memory that is
memory allocated to its each individual Large enough to hold the Largest
member. variable of type in union.
3 Each member have their own memory One block is used by all the members
space of union
4 Structure can not be implemented in Union is the Best environment where
shared memory memory is shared.
5 It has less Ambiguity As memory is shared, Ambiguity is
more in union.
6 Self referential structure can be Self referential union can not be
implemented in data structure. implemented.
7 All members of structure can be Only one member is accessed at a time.
accessed at a time
8 Size of structure is equal to the sum of Size of union is equal to the size of the
the size of member variables. large sized member variable.

9 Syntax: Syntax:
struct [structure name] union [union name]
{ {
member definition; member definition;
member definition; member definition;
... ...
member definition; member definition;
}; };
10 Example structure declaration: Example union declaration:
struct sVALUE union uVALUE
{ {
int ival; int ival;
float fval; float fval;
char *ptr; char *ptr;
}s; }u;
Here size of struct is 8 Bytes. Here size of union is 4 Bytes.
c) Write a C program to maintain a record of “n‟ students details using an array of structures with four
fields (roll no, name, marks and grade).Assume appropriate data type for each field. Print the names of
the student with marks>=70.
Ans: #include<stdio.h>
struct student
{
int rollno;
char name[20];
int marks;
char grade;
};
void main()
{
int n,i;
printf(“enter the number of students:”);
scanf(“%d”, &n);
struct student s[n];
for(i=0;i<n;i++)
printf(“ enter details of students %d:\n”,i+1);
printf(“Roll no:”);
scanf(“%d”,&s[i].rollno);
printf(Name”);
scanf(“%s”,s[i].name);
printf(“Marks:”);
scanf(“%d”,&s[i].marks);
if(s[i].marks>=90)
s[i].grade=’A’;
else if(s[i].marks>=80)
s[i].grade =’B’;
else if(s[i].marks>=70)
s[i].grade =’C’;
else if(s[i].marks>=60)
s[i].grade =’D’;
else
s[i].grade=’F’;
printf(“\nNames of students with marks >= 70:\n”):
for(i=0;i<n;i++){
if(s[i].marks>=70)
printf(“Name:%s\n”,s[i].name);
}
10. a) Explain I/O stream and Data I/P stream used in files.
Ans:
 The Standard I/O streams are stdin and stdout: Two streams exist to allow you to communicate with your
"console" - the screen and keyboard setup that makes up the traditional computer user interface. These are
the input stream stdin (console input), which is connected to the keyboard, and the output stream stdout
(console output), which is connected to your display. These two streams are created when the system starts
our program.
 To use stdin and stdout and the input and output functions for them, you have to tell the compiler about them
by including the relevant declarations in the library header file: #include when the program is linked, the
relevant library object modules must be included - usually this is automatic or a normal part of your
compilation/linking process.
 The streams stdin and stdout are actually global variables that are declared in and initialized during program
startup. This document uses stdin and stdout for examples, but everything herein works for any other text
input or output stream, such as one your program might create itself for disk file input/output. The C
Standard Library includes a variety of functions for doing input/output.

b) Define Enumerated data type. Explain the declaration and access of enumerated data types with a
code in C.
Ans:
 An enumeration provides the data type with a set of values. An enumeration constant is a type of an integer. A
variable type takes and stores the values of the enumeration set defined by that type. In C programming, an
enumeration type (also called enum) is a data type that is used to assign integral constants to set of names.
 They are declared by using keyword enum in place of datatye.
 Declaration: Syntax enum enum_variable_name{ele1,ele2,ele3,…….,elen};
 Example:
#include <stdio.h>
enum week {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
void main()
{
enum week today;
today = Wednesday;
printf("Day %d",today+1);
}
c) Write a function to perform the following:
i)Read data from a file ii)Write data to a file.
Ans:
i)Read data from file:
#include<stdio.h>
main()
{
FILE *fp;
char buff[255];
fp = fopen("file.txt", "r");
while(fscanf(fp, "%s", buff)!=EOF)
printf("%s ", buff );
fclose(fp);
}

ii)Write data to file:


#include<stdio.h>
main()
{
FILE *fp;
fp = fopen("file.txt", "w");
fprintf(fp, "Hello file by fprintf...\n");
fclose(fp);
}

You might also like