POP Set - 3 Solution
POP Set - 3 Solution
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.).
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:
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);
} }
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);
}
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]);
}
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);
}
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 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);
}