Questions: Pps (Unit-1) Short Answer Questions
Questions: Pps (Unit-1) Short Answer Questions
PPS (UNIT-1)
* Bubble Sort
9 Identity Matrix
10 Symmetric Matrix
(UNIT-3)
3 Explain how the members of the structure are accused 4 Program to Print
(UNIT-5)
1. Programs:
i) To access the elements of ID array
using pointer variable.
ii) Bubble Sort using pointers. iii)
Selection Sort using pointer. iv)
Addition of 2 matrices using
pointers. v) Matrix Multiplication
using pointers.
2 Call by reference – swapping example.
3 Dynamic memory allocation
4 File handling functions
5 To count no. of characters, words and files in a file.
6 Copy contents of one file into another file.
7 Compare the contents of two files.
8 Merging of 2 files into another file.
Answers:
PPS (UNIT-1)
It is system software which controls the hardware and software resources of the computer
and offers common services for computer programs.
1. Low level language is machine readable form of program. Whereas the high level
language will be in readable form.
2. Low level language are difficult to write and compile but high level languages are
easy to write as well as compile.
3. Low level language are compact and require less memory space. High level
language uses compilers and interpreters which requires large memory space.
4. In high level language debugging .I.e. Finding and correcting errors are easier
whereas debugging in the low level language is quite difficult.
5. Low level language coding and compiling is time consuming process whereas high
level language coding and compiling is much easy and takes less time to compile.
Ans:
*C-Statement:
Note: Every C statement must end with semicolon; this; acts as a statement
terminator.
*C-expression:
These expressions are evaluated left to right and the value of right most expression is the
value of the combined expression.
Ex: v=(x=10, y=5, x+y);
Here the value of the right most expression is stored in variable v i.e, 15.
Long Answer Questions:
Global Declaration
This section declares some variables that are used in more than one function. These
variables are known as global variables. This section must be declared outside of all the
functions.
Function main
Every program written in C language must contain main () function. The function
main() is a starting point of every C program. The execution of the program always begins
with the function main ().
Declaration Part
The declaration part declares the entire variables that are used in executable part.
The initializations of variables are also done in this section. Initialization means providing
initial value to the variables
Executable Part
This part contains the statements following the declaration of the variables. This part
contains a set of statements or a single statement. These statements are enclosed
between the braces.
The functions defined by the user are called user-defined functions. These functions
are generally defined after the main () function.
Step-1 Start
Step-2 Input two numbers say
I n p u t V a lu e
NUM1, NUM2 of NUM1
Step-3 IF NUM1 > NUM2 THEN
Print largest is NUM1
Input Value
of NUM2
ELSE print largest is NUM2
ENDIF
Print Ye s No Print
if
Step-4 Stop Largest is
NUM1 > NUM2
Largest is
NUM1 NUM2
Stop
(ii).Find the largest of three numbers
Algorithm
Step-1 Start
Step-2 Read three numbers say num1, num2, num3
Step-3 if num1>num2 then go to step-5
Step-4 IF num2>num3 THEN
print num2 is largest
ELSE
print num3 is largest
ENDIF
GO TO Step-6
Step-5 IF num1>num3 THEN
print num1 is largest
ELSE
print num3 is largest
ENDIF
Algorithm & Flowchart to find the largest of three numbers (an another way)
Algorithm
Step-1 Start
Step-3 BIG = A
BIG = B
ENDIF
BIG = C
ENDIF
Step-7 Stop
(iii).To check if a number is prime or not.
1. ARITHMETIC OPERATORS:
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division (gives Quetient)
% Modulo Division (gives Remainder)
2. RELATIONAL OPERATORS:
Executable C statements either performs actions (such as calculations or input or
output of data) or make decisions. We might make a decision in a program, for
example, to determine if a person is passed or failed in an exam. To check the
condition(for example in if control structure) we use Relational operators.
Operator Meaning
> x>y x is greater than y
< x<y x is less than y
>= x>=y x is greater than or equal to y
<= x<=y x is less than or equal to y
== x == y x is equal to y
!= x != y x is not equal to y
4. LOGICAL OPERATORS:
C provides logical operators that may be used to form more complex condition by
combining simple conditions. The logical operators are
Operator Meaning
&& logical AND
|| logical OR
! logical NOT
Like the simple relational expressions(x>y), a logical expression also yields a value of
zero or one.
AND operator:
(cond1&&cond2)
If both conditions are true the output of the logical AND expression is true(i.e
it returns 1) and if any of the condition is false the output is false(i.e. it returns 0)
OR Operator:
(condt1 || condt2)
If any of the condition is true the output of the expression is true and the output
will be false when both the conditions are false
Not Operator:
(!conditon)
If the condition is true the output of the expression will be false and if the condition is
false the output of the expression is true.
5. ASSIGNMENT OPERATORS:
These ops. are used to assign the result of an expression to a variable . In addition to the
usual assignment op. ‘=’ , C has a set of shorthand assignment ops. of the form.
v op=exp;
The operator op= is known as the shorthand operator. The above statement can be
written as
v=v op (exp);
Ex: x=8; y=5;
x+=y+1; (here += is shorthand op. Similarly we can also use -=, *=, /=, %= as
shorthand ops.)
The above statement is same as
x=x + (y+1); (here x value becomes 14)
conditional expression ?
expression1:expression2
The conditional expression is evaluated first. If the result is nonzero(true)
expression1 is evaluated and returned, otherwise expression 2 is evaluated and its
value is returned.
8. BITWISE OPERATORS:
These ops. are used for manipulation of data at bit level. These ops. are used for testing
the bits or shifting them right or left.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
~ One’s complement
<< Shift left
>> Shift right
(UNIT-2)
*Do-While Loop:
Unlike for and while loops, which test the loop condition at the top of the loop, the do-
while loop in C programming checks its condition at the bottom of the loop.
A do-while loop is similar to a while loop, except the fact that it is guaranteed to execute
at least one time.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
~ One’s complement
<< Shift left
>> Shift right
Ans:
*Break Statement:
If you are using nested loops, the break statement will stop the execution of the
innermost loop and start executing the next line of code after the block.
Syntax
*Continue Statement:
*Goto Statement:
.
.
.
label: statement;
Here label can be any plain text except C keyword and it can be set anywhere in the C
program above or below the goto statement.
*Exit Statement:
Example:
/* Calculator program using switch case */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,n;
clrscr();
printf(“Enter two no’s\n”);
scanf(“%d %d”, &a,&b);
printf(“1.Addition\n2.Substration\n3.Multiplication\n4.Division
scanf(“%d”,&n)
switch(n)
{
case 1:c=a+b;
printf(“%d”,c);
break;
case 2:c=a-b;
printf(“%d”,c);
break;
case 3:c=a*b;
printf(“%d”,c);
break;
case 4:c=a/b;
printf(“%d”,c);
break;
default: printf(“Invalid Number\n”);
break;
}
getch();
}
2. Programs:
i) To check whether a given no is palindrome
or not.
ii) Sin (x) and Cos(x) using series expansion.
Ans:
(i) To check whether a given no is palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev=0,rem,temp;
clrscr();
printf(“enter a number:\n”);
scanf(“%d”,&num);
temp=num;
while(num>0)
{
rem=num%10;
num=num/10;
rev=rev*10+temp;
}
if(rev==temp)
printf(“it is a palindrome”);
else
printf(“it is not a palindrome”);
getch();
}
Output:
enter a number :
101
It is a palindrome
(ii). Sin(x) using sine series:
#include<stdio.h>
#include<conio.h>
void main()
{
int degree,i=1;
double radian,term,nr,dr,sum=0.0;
clrscr();
printf(“Enter a value in degree:\n”);
scanf(“%d”,°ree);
radian=degree*0.01745329;
nr=radian;
dr=1.0;
term=nr/dr;
while(i<=20)
{
sum=sum+term;
nr=(-nr)*radian*radian;
dr=dr*(i+1)*(i+2);
term=nr/dr;
i=i+2;
}
printf(“sine value =%lf”,sum);
getch();
}
Output:
Enter a value in degrees:
90
Sine value=1.000000
(iii). Cos(x) using cos series expansion:
#include<stdio.h>
#include<conio.h>
void main()
{
int degree,i=2;
double radian,term,nr,dr,sum=1.0;
clrscr();
printf(“Enter a value in degree:\n”);
scanf(“%d”,°ree);
radian=degree*0.01745329;
nr= -radian*radian;
dr=2.0;
term=nr/dr;
while(i<=20)
{
sum=sum+term;
nr=(-nr)*radian*radian;
dr=dr*(i+1)*(i+2);
term=nr/dr;
i=i+2;
}
printf(“cosine value =%lf”,sum);
getch();
}
Output:
Enter a value in degrees:
90
Cosine value=0.00000
* Bubble Sort
*Selection Sort:
Selection sort is a simple sorting algorithm. This
sorting algorithm is an in-place comparision based algorithm in
which the list is divided into two parts the sorted part at the left
end and the unsorted part at the right end. Initially the sorted part
is empty and the unsorted part is the entire list.
If the user wants to sort an array in ascending order then the
comparision is made between two elements and the smaller
elements is placed at the first place. The process is repeated untill
last elements are compared.
EX:
2 150 113 19 225 50 71
*Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int A[100];
int small,N,i,j,temp;
clrscr();
printf(“enter the size of array:\n”);
scanf(“%d”,&N);
printf(“Enter array elements:\n”,N);
for(i=0;i<n;i++)
{
small=i;
for(j=i+1;j<N;j++)
{
if(A[j]<A[small])
{
small=j;
}
}
temp=A[i];
A[i]=A[small];
A[small]=temp;
}
printf(“The sorted array list is :\n”);
for(i=0;i<n;i++)
printf(“%d\t”,A[i]);
getch();
}
*Output:
Enter the size of array:
5
Enter array elements:
21
45
67
2
6
The sorted array list is:
2 6 21 45 67
*Bubble Sort:
*Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100];
int trip,N,i,j,temp;
clrscr();
printf(“enter the size of array:\n”);
scanf(“%d”,&N);
printf(“Enter array elements:\n”,N);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(trip=1;trip<N;trip++)
{
for(i=0;i<N-trip;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf(“The sorted array list is :\n”);
for(i=0;i<N;i++)
printf(“%d\t”,a[i]);
getch();
}
*Output:
Enter the size of array:
5
Enter array elements:
21
45
67
2
6
The sorted array list is:
2 6 21 45 67
(UNIT-3)
Multi dimensional array can have three, four or more dimensions. The first dimension
is called plane which consists of rows and columns. The three dimensional array to
be an array of two dimensional arrays. It considers the two dimensional array to be
an array of one dimensional arrays.
We can initialize a three dimensional array in a similar way like a two dimensional array.
Here's an example,
int test[2][3][4] = {
{//plane 0
{3, 4, 2, 3}, //row 0
{0, -3, 9, 11}, //row 1
{23, 12, 23, 2} //row 2
},
{//plane 1
{13, 4, 56, 3}, //row 0
{5, 9, 3, 5}, // row1
{3, 1, 4, 9} //row 2
}
};
2. Function Prototype.
Ans:
Function Prototype
A function prototype tells the compiler the number and type of arguments that are to
passed to function and the type of value that is to be returned by the function. The void is
used if no value is returned by the function.
4) strcmp(string1, string2);
char string1[35]=”Yahoo”,string2[20]=”Hotmail”;
the strcmp function returns zero if strin1=string2, returns negative value if
strin1<string2 or positive value if strin1>string2
Ans:
#include <stdio.h>
#include <string.h>
int main()
gets(a);
else
return 0;
*Output:
Enter a string to check if it is a palindrome
wow
The string is a palindrome.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
scanf("%s", input);
n = strlen(input);
ch = input[c] - 'a';
no[ch]++;
t = 0;
x = ch - 'a';
output[t] = ch;
t++;
output[t] = '\0';
printf("%s\n", output);
return 0;
*Output:
game
aegm
(UNIT-4)
Ans:
A function that calls itself is called as recurssive function and this technique is known as
recurssion. Recurssion continues untill some condition is net to prevent it.
*Ex:
factorial(int n)
int fact;
if(n==1)
return(1);
else
fact=n*factorial(n-1)
return(fact);
Ans:
Structure Definition
type1 member1;
type2 member2;
};
Ans:
1. Traversing- It is used to access each data item exactly once so that it can be processed.
2. Searching- It is used to find out the location of the data item if it exists in the given collection
of data items.
3. Inserting- It is used to add a new data item in the given collection of data items.
4. Deleting- It is used to delete an existing data item from the given collection of data items.
5. Sorting- It is used to arrange the data items in some order i.e. in ascending or descending
order in case of numerical data and in dictionary order in case of alphanumeric data.
6. Merging- It is used to combine the data items of two sorted files into single file in the sorted
form.
Ans:
i) Find Factorial
#include <stdio.h>
#include<conio.h>
int fact(int);
void main()
int num,f;
clrscr();
printf(“Enter a number:\n”);
scanf(“%d”,&num);
f=fact(num);
getch();
int fact(int n)
if(n==1)
return 1;
else
return(n*fact(n-1));
*Output:
Enter a number:
Factorial of is 120
ii)GCD of two numbers
#include <stdio.h>
int hcf(int n1, int n2);
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
#include<include.h>
#include<conio.h>
series:”); scanf(“%d”,&n);
printf(“Fibonacci Series:
printFibonacci(n);
getch();
Void printFibonacci(int n)
“,sum); printFibonacci(n-1);
BASIS FOR
ARRAY STRUCTURE
COMPARISON
Memory Array elements are stored in Structure elements may not be stored
contiguous memory location. in a contiguous memory location.
Pointer Array name points to the first Structure name does not point to the
element in that array so, array first element in that structure so,
name is a pointer. structure name is not a pointer.
Bit filed Bit filed can not be defined in an Bit field can be defined in a structure.
array.
(UNIT-5)
Ans:
In C , every variable must be declared before they are used. Since the pointer variables
contain address that belongs to a separate data type, they must be declared as pointers
before we use them.
Assume the memory address of x is 5003, if px=px+3; is executed then the px will contain
5009 is 5003+3*(size of integer) i.e. 5003+3*2=5009
i.e. base address + number * size of datatype
Ans:
File Mode:
When we open a file, we explicitly define its mode. The mode shows how we will use the
file: for reading, for writing, or for appending.
The read mode (r) opens an existing file for readingIf we try to write a file opened in read
mode, we get an error message.
The write mode (w) opens for writing. If the file doesn’t exist, it is created.
1.malloc function:
• malloc () function is used to allocate space in memory during the execution of the
program.
• malloc () does not initialize the memory allocated during execution. It carries
garbage value.
• malloc () function returns null pointer if it couldn’t able to allocate requested
amount of memory.
Syntax of malloc()
ptr = (cast-type*) malloc(byte-size)
2. calloc function:
• calloc () function is also like malloc () function. But calloc () initializes the allocated
memory to zero. But, malloc() doesn’t.
The name calloc stands for "contiguous allocation".
The only difference between malloc() and calloc() is that, malloc() allocates single
block of memory whereas calloc() allocates multiple blocks of memory each of same
size and sets all bytes to zero. Syntax of calloc()
ptr = (cast-type*)calloc(n, element-size);
3.realloc function:
realloc () function modifies the allocated memory size by malloc () and calloc () functions to
new size. If enough space doesn’t exist in memory of current block to extend, new block is
allocated for the full size of reallocation, then copies the existing data to new block and then
frees the old block.
4. free function:
free () function frees the allocated memory by malloc (), calloc (), realloc () functions and
returns the memory to the system free()
Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on
its own. You must explicitly use free() to release the space. syntax of free() free(ptr);
This statement frees the space allocated in the memory pointed by ptr.
Example: Using C malloc() and free()
File
handling functions Description