Q.
No.1 Write an algorithm to check whether a given number is even or odd number
and after that draw the flowchart.
Answer:
Step
1
|
Start
|
Step
2
|
Input
A
|
Step
3
|
IF
A%2=0 THEN
|
|
Print
“The Number is Even”
|
|
Else
|
|
Print
“The Number is Odd”
|
|
[End
of if Structure]
|
Step
4
|
End
|
Flow cha
Q.
No.2 Explain the structure of the C program.
Answer: Please follow the sequence of instruction that
should be maintained to be a C Programmer.
Let us start with the compulsory portion, that is, the main function body. In the
main function, there are a number of parts out of which the basic parts are as
follows:
(1) Starting
of main function: The compulsion function is the main function that would be
done a
// program for addition of two integer
numbers
void main()
{
|
(2) Variable
declaration
To declare variable wse must have the
awareness of variable name convention, data type and syntax for declaration
etc.
int a,b,c;
Or can initialize value at the time of
declaration as:
int a=20,b=40,c;
|
(3) Message for accepting
value
To display the values or message into
the screen or to file we have several functions called output functions. The
functions are given below:
printf(),sprint(),cprintf(),puts(),putw(),fwrite(),fprintf()
etc.
The syntax of print() is given below:
printf(“message/scan format”,
[list of variables]);
Note: The scan format varies upon the type of
variables. The following statement will show user a message that prompt the
user to enter values through keyboard.
printf(“Please enter two values:”);
|
(4)Inputs from the
Keyboard
To input the data into the program,
we have a number of function called input function which are given below:
scanf(), gets(), getch(), getche(),
getc(), getw() etc.
The syntax of scan() is given below
and others will be discussed later.
Scan(“scan format”, &variables);
Note: The scan format varies upon the
type of variables. The following statement will accept two integer values from
keyboard and store to variables a and b [enter two values in the same line
separated by space].
scanf(“%d %d, &a,&b”);
|
(5) Process
according to problem given
Now we need to add the two numbers.
So we need the knowledge of the operation and expression. Here we need the
arithmetic operator ‘+’ for addition.
c=a+b
|
(6)Show Output on monitor
To display the output, we again need
the output function which has been discussed earlier. The statement is
printf(“The sum of %d and %d is = %d”,
a,b,c);
|
(7)Closing of the main
function
Now the time to close the main
program and run the program for desired output. Close the main function with a
‘}’ as given below:
}
|
Q.
No.3 What is structure? What is its importance? Explain with an
example
how to declare and initialize structure.
Answer: A structure is a collection of one or more
variable, possibly of different data-types, grouped together under one name.
Structure provides a means to organize related data into single unit. Structure
are suitable for managing arrays of related variables.
When we require to handle large amount of
heterogeneous data which has some relationship among themselves we go for
structures. Structures help to place the related data element into a single
unit. Disadvantage of arrays is element must be of same type. If we want to
store data of different types, we need to create arrays of different types.
This forces to store the related data across different array variables. These
kinds of problem can be solved by making use of structure. A structures
definition creates a format that may be used to declare structure variables.
Declaration
of structure
Each and every structures must be defined or declared
before it appears in the program. It does not occupy any memory space.
Syntax:
struct<
structure name>
{
<type1><field/data1>;
<type2><field/data2>;
<type3><field/data3>;
………………………
<type
n><field/data n>;
};
Syntax to declare
structure type variable:
struct< structure
name><variable name>;
Example 1:
struct student
{
int roll;
char name[30];
char address[30];
char city[15];
int marks;
};
struct student stu;
Initialization OF
Structure
Initialization a structure data type, while
declaration, is similar to that of a static data type.
Example 2:
struct student rec = {10,”Avinda”,
“Ray Apartment”, “Bangalore”,560};
Sometimes we can create
structure type variable with the keyword typedef
Syntex:
typedef struct<structure name>
{
<type1><field/data1>;
<type2><field/data2>;
<type3><field/data3>;
………………………
<type n><field/data n>;
}<variable>;
Q.
No.4 What do you mean by dynamic memory allocation? Explain the three functions
for dynamic memory allocation in C language.
Answer: C language requires the number of element in
an array to be specified at compile time. Many languages permits a programmer
to specify an array’s size at run time . Such language have the ability to
calculate and assign, during execution, the memory space required by the
variable in a program. The process of allocating memory management functions so
that memory can be allocated dynamically.
The three different functions used in dynamic memory
allocation are:
·
Malloc()
·
Calloc()
·
Realloc()
malloc(): Allocates request size of bytes and return a
pointer to the first byte of the allocated space. In the other sense, this
function reserves a block of money of specified size and return a pointer of
type void. This means that we can assign it to any type of pointer.
Syntax:
<pointer>=(type_cast*)malloc(size in bytes);
Example:
int n = 5
int*p =
(int*)malloc (n*sizeof(int));
p
|
|
|
|
|
p+0 p+1 p+2 p+3 p+4
calloc(): allocates space for an array of elements,
initializes them to zero and then returns a pointer to the memory. In other
sense, this function allocates a single block of storage spaces, calloc()
allocates multiple blocks of storages, each of the same size, and then sets all
the bytes to zero.
Syntax: <pointer>=(type_cast*)calloc (n,size in
bytes in each row);
Example: int n=4, m=5,*p;
p=(int*) calloc(n,m*sizeof(int));
p+0+0
|
p+0+1
|
p+0+2
|
p+0+3
|
p+0+4
|
p+1+0
|
p+1+1
|
p+1+2
|
p+1+3
|
p+1+4
|
p+2+0
|
p+2+1
|
p+2+2
|
p+2+3
|
p+2+4
|
p+3+0
|
p+3+1
|
p+3+2
|
p+3+3
|
p+3+4
|
Realloc(): modifies the size of previously allocated
space
Syntax: <pointer>=(data type) realloc (new size
in bytes);
Example: int n=4,*p;
p=(int*)realloc(n*sizeof(int));
p
|
|
|
|
p+0 p+1 p+2 p+3
in case of realloc() function, the memory allocation
can be changed i.e. increased or decreased. The previous value will preserve,
if new size is greater than the previous one. We can use “à”
to access the element of structure through pointer.
Q.
No.5 Write C program to implement stack using arrays.
Answer:
#include<stdio.h>
#include<process.h>
#define STACK_SIZE 5
Void main()
{
/***************function prototype************/
void push(int, int*, int*);
int pop(int*,int*);
void display(int,int*);
/************************/
int top;
/*Points to top of the stack*/
int s[10];
/*Holds the stack items*/
int item; /*Items
to be inserted or deleted item*/
int choice;
/*user choice for push, pop and display*/
top = -1;
/*Stack is emty to start with*/
for(;;)
/*Infinite loop*/
{
clrscr();
printf(“\t\t STACK OPERATION\n”);
printf(“\t\t ~~~~~~~~~~~~~~~~~\n”);
printf(“\t\t 1: Push\n”);
printf(“\t\t 2: Pop\n”);
printf(“\t\t 3:Display\n”);
printf(“\t\t 4:Enter the choice: ”);scanf(“%d”,
&choice);
switch(choice)
{
Case 1:
// push operation
Printf(“\n\nEnter the item to be inserted: ”); scanf(“%d”,&item);
Push(item,
&top, s);
continue;
case2: // pop operation
item =
pop(&top, s);
if
(item==0)
printf(“stack is empty\n”);
else
printf(“POP Successfully, Deleted Item=%d\n”,item”);
break;
case3: //display
display(top, s);
break;
case4:
// exit from program
exit(0);
default:
// invalid input
printf(“Invalid input- try again”);
}
getch();
} /*End of the loop*/
/*********************end of
push()*******************/
int pop(int*top, int s[])
{
int
item;
if
(*top==-1)
{
Return 0; /*indicates
empty stack*/
}
item =
s[(*top)-];/* Access the item and delete */
return item;
/* Sends the deleted to the calling function*/
}
/******************end of pop()*********************/
void display(int top, int s[])
{ int
i;
if(TOP == -1)
{
Printf(“Stack is empty\n”);
Return;
}
Printf(“\n\n\n\t\t Contents of the STACK\n\n”);
For (I =top;
i>0; i-)
{
printf(“\t\t\t|%5d |\n, s[i]”);
}
printf(“\t\t\t--------”);
}/***************end of the
displays()**********************/
Q.
No.6 What do you mean by binary tree? Explain the storage representations of
binary tree.
Answer: In computer science, a binary tree is a tree
data structure in which each node has at most two children. Typically, the
child nodes are called left and right. Binary tree are commonly used to
implement binary search trees and binary heaps.
A Binary tree T is defined as a finite set of elements,
called nodes such that,
a. T
is empty, called NULL tree or empty tree
b. T
contains a distinguished node R, called Root of 1 and the remaining nodes of T
from an ordered pair of disjoint binary tree T1 and T2.
c. The
node contains max two children.
The binary tree is a tree
in which no node can have more than two subtrees. In order words, a node can
have 0, 1, or 2 subtrees, in other words, a tree in which every parent has one
or two children(but not mare than that) is call as binary
Fig. binary
tree with any number of nodes
Storage
Representation:
The tree can be represented using sequential
allocation technique(using arrays) or by allocating the memory for a node
dynamically (using linked allocation technique). In a linked allocation
technique a node in a tree has three fields:
·
info which contains the actual information
·
link which contains address of the subtree
·
rlink - contains address of the right
subtree.
So, a node can be represented using structure as shown
below:
struct
node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef
struct node* NODE;
Memory can be allocated or de-allocated using the
function getnode() and freenode (). A tree can also be represented using the
array, which is called sequential representation(array representation).
Consider the following fig. as example:
Fig. A Sequential representation of a tree
The different ways in which a tree can be represented using
an array is shown below.
·
In the first representation shown below,
some of the location may be used just some may not be used. For this, a flag
field namely, used is used just to indicate whether a memory location is used
to represent a node or not. If flag field used is 0, the corresponding memory
location is not used and indicates the absence of node at that position. So,
each node contains two fields:
·
info
where the information is stored
·
used
indicates the performance or the absence of a node.
No comments:
Post a Comment