POP Set - 1 Solution
POP Set - 1 Solution
1. a)Define computer. Describe the various types of computers based on speed, memory and cost.
Ans:
Computer is an electronic device which is capable of receiving information or data and perform a series of
operations in accordance with a set of instructions and produces results in the form of data or information.
Computers can be categorized based on size, performance and application as follows:
i) Super Computer:
Supercomputers are fastest computers and are very expensive.
These are employed for specialized applications that require immense amounts of mathematical calculations.
For example, weather forecasting requires a supercomputer.
Other uses of supercomputers include animated graphics, fluid dynamic calculations, nuclear energy research,
and petroleum exploration.
Start
b)Draw a flowchart and C program which takes as input p,t,r. Compute the
simple interest and display the result.
Ans: Read p,t,r
#include<stdio.h>
void main()
{ compute si=(p*t*r)/100
float p,t,r,si;
printf(“Enter the P,T,R values\n”);
scanf(“%f %f %f”,&p,&t,&r); Display SI = si
si=(p*t*r)/100;
printf(“Simple interest = %f”,si);
} Stop
c)Write a note on following operators. i)Relational ii)Logical iii)Conditional
Ans:
i) 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
ii) Logical Operator:
Logical operators are usually used with conditional statements. Operators used with one or more operand and
return either value zero (for false) or one (for true). The operand may be constant, variables or expressions. And
the expression that combines two or more expressions is termed as logical expression.
The three basic logical operators are: -
AND ( && ) : - Takes two or more inputs and produces high/true output if all inputs are high.
OR ( || ) : - Takes two or more inputs and produces high output if any one of input is high/true.
NOT ( ! ) : - Takes one input and inverts input as output.
ii) 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:
#include <stdio.h>
void main( )
{
int num1, num2,product;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
product = num1 * num2;
printf(“sum of %d & %d is %d", num1, num2, product);
}
c) Explain with syntax, if and if-else statements in C program.
Ans:
i) if: It executes the statement or set of commands when the condition is true.
Syntax:
if(condition)
{
statement x;
}
Example: #include<stdio.h>
void main( )
{
int age;
printf (“ enter the age:”);
scanf(“%d”,&age);
if (age>=18)
printf(“ eligible for voting”);
}
ii) if-else: An if statement can be followed by an optional else statement. If the expression evaluates to true,
then the if block of code will be executed, otherwise else block of code will be executed.
Syntax:
if(condition)
{
statement 1;
}
else
{
statement 2;
}
Example: #include<stdio.h>
void main( )
{
int age;
printf (“ enter the age:”);
scanf(“%d”,&age);
if (age>=18)
printf(“ eligible for voting”);
else
printf(“Not eligible for voting”);
}
5. a) Write a C program to swapping of 2 numbers using call by reference and call by value.
Ans:
Call by value:
#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);
}
Call by reference:
#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);
}
b) –no question…
Ans: - - - - -
c) Discuss the implementation of user defined function with suitable example.
Ans:
User defined functions are the functions written by the user to perform some specific task.
Example:
#include <stdio.h>
int add (int x, int y); //Function prototype
void main()
{
int a,b,c;
printf (“enter any two numbers/n);
scanf(“%d%d”, &a, &b);
c=add (a,b); //Calling the function
printf(“sum =%d”, c);
} Passing arguments to function
int add (int x, int y)
{
int sum;
sum =x+y; Function Definition
return sum;
}
6. a) Write a C program to find the product of two given matrix.
Ans:
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
printf("\n enter the order of the matrix A\n");
scanf("%d%d",&m,&n);
printf("\n enter the order of the matrix B\n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("\n Invalid inputs, Multiplication is not Possible \n");
else
{
printf("\n enter the elements of matrix A\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n enter the elements of matrix B\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<p;k++)
c[i][j] = c[i][j]+a[i][k]*b[k][j];
}
printf(" \n The Resultant Matrix is: \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
}
b) Explain the working of recursion with suitable example.
Ans:
A recursive function is defined as a function that calls itself to solve a smaller version of its task until a final call is
made which does not require a call to itself.
Example:
#include<stdio.h>
int fact(int n);
void main()
{
int num;
printf(“Enter a number\n”);
scanf(“%d”,&num);
printf(“Factorial of %d = %d”,num,fact(num));
}
int fact(int n)
{
if(n= = 1)
return 1;
return (n*fact(n-1));
}
c) Explain the declaration and initialization of one dimensional and two dimensional arrays with an
example.
Ans:
1-D array:
Declaration Syntax datatype array_name[size];
Initialization Syntax datatype array_name[size]={v1,v2,v3,………,vn};
Example: #include<stdio.h>
void main()
{
int a[10]={1,2,3,4,5},i,n;
printf(“enter the size of array:”);
scanf(“%d”,&n);
printf(“enter 5 integer values for array:”);
for(i=5;i<10;i++)
scanf(“%d”, &a[i]);
printf(“the array elements are: \n”);
for (i=0;i<10;i++)
printf(“%d\t”, a[i]);
}
2-D array:
Declaration Syntax datatype array_name[row_size][column_size];
Initialization 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]);
}
7. a) Develop a C program to concatenate 2 strings without using built-in function.
Ans:
#include <stdio.h>
#include<string.h>
void main()
{
char s1[100] = "Welcome to ", s2[ ] = "C programming";
int length, j;
length = 0;
while (s1[length] != '\0')
length++;
for (j = 0; s2[j] != '\0'; j++)
{
s1[length] = s2[j];
length++;
}
s1[length] = '\0';
printf("After concatenation: ");
puts(s1);
}
b) Define String. Explain any 4 string manipulation function with suitable example.
Ans:
A string is a sequence of characters enclosed between double quotes.
i) 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) 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));
}
iii) strlwr: This string function is used to convert the string to lowercase.
Syntax: strlwr(str);
Example:
#include<stdio.h>
void main()
{
char str[10]= “COMPUTER”;
printf(“The lowercase of string is = %s”,strlwr(str));
}
iv) strcat: This string function is used to concatenate two or more strings.
Syntax: strcat(str1,str2);
Example:
#include<stdio.h>
void main()
{
char str1[10]= “COMPUTER”;
char str2[10]= “SCIENCE”;
char str3[20];
str3=strcat(str1,str2);
printf(“The concatenated string is = %s”,str3);
}
b) Define Pointer. Explain pointer variable declaration and initialization with suitable example.
Ans:
A pointer is a special variable which stores the address of another variable.
Declaration Syntax datatype *pointer_name;
Initialization Syntax pointer_name = &variable_name;
Example:
#include< stdio.h>
void main()
{
int a=22,*p ;
float b=2.25,*q ;
p=&a ;
q=&b ;
printf( “\n value of a = %d”,*p) ;
printf( “\n value of b = %d”,*q) ;
}
c) Explain the difference between a null pointer and a void pointer.
Ans:
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);
} }
9. a) Discuss the general syntax of structure variable declaration of structure to store book information.
Ans:
After defining a structure format we must declare variables of that type. A Structure variable
declaration is similar to the declaration of variables of any other data types.
It includes the following elements.
1) The keyword struct.
2) The structure tag name.
3) List of variable names separated by commas.
4) A terminating semicolon.
For example: syntax
struct book_bank book1, book2, book3;
It declares book1, book2 and book3 as variables of type struct book_bank.
Example:
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
}book1, book2, book3;
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 program to write employees details in a file called employee.txt. Then read the record of the
nth employee and calculate his salary.
Ans:
Answer - - - - -will - - - - be - - - done - - - later
10. a) Discuss the different modes of operation on files with suitable example.
Ans:
Mode Description
r Opens a file in read mode
w Opens or creates a text file in write mode
a Opens a file in append mode
r+ Opens a file in both read and write mode
w+ Opens a file in both read and write mode
a+ Opens a file in both read and write mode
Example:
Program to read and write data to a file.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
fp = fopen("hello.txt", "w");
printf("Enter data");
while( (ch = getchar()) != EOF)
putc(ch,fp);
fclose(fp);
fp = fopen("hello.txt", "r");
while( (ch = getc(fp)! = EOF)
printf("%c",ch);
fclose(fp);
}