Bba Iv A
Bba Iv A
IVth Semester
Computer Programming
Decision Table
Types of Decision Tables
Limited Entry Form
Extended EntryForm
Mixed Entry Form
Else Form
Factorial Algorithm-
(I) Step I - Start
Step II _ assign value 1 to Fact i.e. fact = 1
Step III - Read value of N
Step IV if value of N is greater then 1 i.e. N > 1 then goto
Step V otherwise goto step VI
Step V - Compute fact = Fact * N
N = N - 1 and
goto Step IV
Step VI - Print value of Fact.
Step VII - Stop.
Q. 2. (b) Write a program to calculate area of right traiangle using formula
after entering the values of sides of triangle.
Ans.
void main()
{
clrscr ();
getch ():
} /*end of void main () function */
void triangle ()
{
float b, h, area;
clrscr ();
void main ()
{
int a, b, c, n, k = 1;
clrscr ();
a=0;
b=0;
c=1;
while (k<=n)
{
printf (``%d\t'', c);
a=b;
b=c;
c= a + b;
k++;
}/* end of while loop */
getch ();
} /* end of void main () function */
Q. 4 (b) How break and continue statements are used? Explain with
examples.
Ans. Please Refer to Question No. 7 (b) May-June 2006.
void main()
{
clrscr ();
getch ();
} */end of void main () function */
void circle ()
{
area = r*r*PI;
void triangle ()
{
float b, h, area;
clrscr ();
area = h*b*(1.0/2.0);
void square ()
{
float 1, area;
clrscr () ;
printf (``\n Calculating area of square'');
area = l*l;
getch () ;
} /* end of void square () function */
Q. 7. (a) Explain the model of a digital computer with the help of a diagram.
Also state how it is different from computers based on vaccum tubes.
Ans. Please Refer to Question No. 1 (a) May-June 2006.
Q. 3. (b) How break and continue statements are used? Explain with
example.
Ans. Please Refer to Question No. 7 (b) May-June 2006.
max = a[0];
getch ();
} /* end of void main () function */
Q. 6. (a) Explain different types of if statements with examples.
Ans. Please Refer to Question No. 6 (b) May-June 2006.
void main()
{
int a, b;
clrscr ();
getch ();
} /*end of void main () */
z=x;
x=y;
y=z;
x = x + y;
y = x - y;
x = x - y;
Q. 1. (b) Write a program in C that counts blanks, tabs and new lines.
Ans.
void main ()
{
int b = 0, t = 0, n = 0, k, z;
char str [100];
clrscr ();
printf (``\nProgram for count Tabs, Blanks and New lines in string....'');
for (k = 0; k .. z; k++)
{
getch ();
} /*end of void main () */
void main ()
{
p=s;
printf (``\n String longer then 80 characters per line are ........\ n'');
while (*s! = '\0')
{
s++;
count++;
} /* end of for loop */
getch () ;
} /*end of void main () function */
Q. 3. (a) What are standard library functions explain at least 5 with the help
of examples?
Ans. A character string is stored in an array of character type, one ASCII
character per location. In other words, string is a group of characters. C
does not provide any operator, which which manipulate entire strings at
once.
Char text [100]; /* String text */
Strings are manipulated either via pointers or via special routines
available from the standard string function library stringh. The standard
``string'' library contains many useful functions to manipulate strings.
Functions are easy to use; they allow complicated programs into small
blocks, each of which is easier to Write, Read, Maintain and Modify.
Library is a collection of functions contained within a single file. Each
library typically has a header file, which contains the prototypes of the
functions.
In order for a program to use a library, the header file from that library
must be declared at the top of a source file.
Example :
#include<string.h>
#include<conio.h>
#include<stdio.h>
void main ()
{
char str [100], str1 [100];
int n, k;
clrscr ();
k = strlen (str);
printf (``\n 1) Length of string entered : %dd '', k);
strlwr (str);
print (``\n 3) String after using strlwr ( ); %s '', str);
strupr (str);
printf (``n 4) String aftng strupr () : % '',str);
strrev (str);
printf (``\n 5) String after using strrev () : %s '',str);
getch ();
} /* end of void main () */
Q. 4. (b) Write a fuction lower, which converts upper ease letters to lower
case, with conditional expression instead of it else.
Ans.
void main ()
{
puts (str);
getch ();
} /* end of void main () function */
***
May 2002
IV Semester
Computer Programming
#include<stdio.h>
#include<conio.h>
void main ( )
{
int y, l;
clrscr () ;
if (y%4 == 0)
printf (``\n Year %d is Leap Year'');
else
printf (``\n Year %d is Not Leap Year'');
getch ();
}
Q. 4. (a) What are arrays? Explain how one dimensional and two
dimensional array elements are stored.
Ans. Please Refer to Question No. 5 (a) May-June 2006.
if (f < = 1)
return (1);
else
return (f* (fact (f-1)));
} /* end of recursive function long fact (
FUNCTION PROTOTYPE
When function body is define after main ( ) function then function
prototype declaration is required.
Function prototype given three type of information-
(i) Function Return type i.e. value data type return by function (if any)
(ii) Name of function (Function Identifier)
(iii) Formal parameters i.e. Number of parameters accept by function
when it is called. Function defination or Body of function i.e. set of
statements, which is part of function & exeuted when function is called.
Function can be
(i) Buit-In / Library function.
(ii) Used Define function.
For using library function required to include related header file in your
program. For ex. to use printf ( ) or scanf ( ) function need to include
stdio.h in program.
PROGRAM
//+ Thoery part
/* Function Example */
long Sum (int x, int y); /* Prototype Required because function body is define after main ( )
function */
void main ( )
{
int a, b;
long c;
clrscr ( );
printf (''\nAddition of two values, Using Function.....``)
printf (''\nEnter first Integer value : ``);
scanf (''d``, &a);
printf (''\n Enter second Integer value : ``);
scanf (''%d``, &b);
c = Sum (a, b); /* Function calling with two actual parameters i.e. a & b */
printf (''\nResute after addition : %ld``, c);
getch ( );
} /* end of void main ( ) */
long Sum (int x, int y);
{ /* Function Name - Sum
Return Type - long
Formal Paramter List with two parameters x & y
*/ function return type is not void */
void main ()
{
getch ();
} /* end of void main () function */
(ii)
void main ()
{
int x, z, a [3] [3], b[3] [3];
int row = 4, col = 4;
clrscr ();
row = row + 2;
row = row + 2;
col = 4;
} /* end of 1st for loop */
getch ();
} /* end of void main () function */
Q. 6. (a) Explain call by value and call by reference by giving
examples.
Ans. Please Refer to Question No. 4 (a) May-June 2006.
int n, m;
long sum;
clrscr ();
print (``\n Addition of all numbers between %d and %d is : %ld '', n, m, sum);
getch () ;
} /* end of void main () function */
return (k);
} /* end of long add () function */
void main ()
{
int s = 0, v = 0, c = 0, b = 0, k, z;
char str [100];
clrscr ();
printf (``\nProgram for count Blanks, Vowels and Consonants in string....'');
if ((str [k] == `A') ll (str [k] == `E') ll (str [k] == `I') ll (str [k] == `o') ll (str [k] ==`U')) v++;
else
s++;
getch ();
} /* end of void main () */
void main ()
{
/* Searching element */
if (flag ==0)
printf (``\n Element Not Found......'');
else
printf (``\n Element Found at Position %d and value is %d '', post + 1, s);
getch ();
} /* end of void main () function */
clrscr ();
sum (a);
sum1 (a);
sum2 (a);
getch ();
} /* end f void main () */
(i) Void sum (int * p)
{
––––
––––
––––
} /* end void sum () */
void main ()
{
int k;
clrscr () ;
getch ();
} /* end of void main () funnction */
IMPORTANT QUESTIONS
Q. 1. What is C Language?
Ans. (Features)
(i) C is High Level Programming Language.
(ii) C is Functional Language i.e. near about every instruction
is in Form of Function.
(iii) C is case sensitive Programming Language i.e. it differentiate
between upper case [Capital Letter] & Lower Case [Small Letter]
alphabate.
(iv) In C every instrutction given in lowercase.
(v) Every C Program have at least one function i.e. main C).
(vi) Program execution start from main C) function.
(i) C is structured Programming Language.
(ii) C is code Block Programming Language.
(iii) Initially, C was used for system programming i.e. for complier,
Interpreters, operating systems etc.
(iv) C Programming Language allows the manipulation of bits, bytes
and addresses.
MEANING OF PROGRAMMING LANGUAGE
A formal language in which computer programs are written. The
definition of a particular language consists of both syntax (how the
various symbols of the language may be combined) and semantics (the
meaning of the language constructs).
An artificial language that enables people to instruct machines.
Computer commands that form procedures by which software
programmers design and implement computer software programs. A
program is a sequence of insructions that are executed by a CPU. While
simple processors execute instructions one after the other.
The language a programmer uses to create an application will depend
on the desired properties of the program. Some programming languages
lend themselves to mathematical and analytical functions while others
are better suited for creating business or data processing applcations.
EXECUTION OF PROGRAM
A program that executes instrctions written in a high-level language.
There are two ways to run programs written in a high-level language.
1. Complier
2. Interpreter
Complier
Source Code In Input Input Object Code In
High Level Complier Machine
Language [System s/w] Language
Output Output
System Software that translates a program written in a high-level
programming language (C/C ++, COBOL, etc.) into machine language. A
complier usually generates assembly language first and then translates
the assembly language into machine language.
A utility known as a ``linker'' then combines all required machine
language modules into an executable program that can run in the
computer.
The original sequence of High Level Language is usually called the
source code and the output in Machine Level Language is called object
code. The most common reason to translate source code is to create an
executable program.
In a production environment where throughput is more critical, a
complied language is preferred.
Complier spends some time evaluating the entire program and then
translates all the programming statements of a program into a machine
language program, which is then executed at once.
Compliers are generally platform-independent.
A program that translates from a low level language to a high level
Language is called Decompiler.
Interpreter
Input Input
Source Code In Interpreter Object Code In
High Level [System s/w] Machine
Language Output Output Language
**
puter Draw block diagram of computer and explain each part of it.
Q. 4. Write a program to perform the following functions using functions for calculating area
of circle, triangle and square.
(b) Write a program that will swap value of two variables without using third variable
and using third value.
puter Draw block diagram of computer and explain each part of it.
Q. 4. Write a program to perform the following functions using functions for calculating area
of circle, triangle and square.
(b) Write a program that will swap value of two variables without using third variable
and using third value.
New 2
(b) Discuss concept of flow-charts. Draw a flow chart and also write
algorithm to generate series like 1, 4, 7, ....
Ans.
Introduction to Flowchart
The flowchart is a means of visually presenting the flow of data through an
information processing systems, the operations performed within the system and the
sequence in which they are performed.
As we know a designer draws a blueprint before starting construction on a
building. Similarly, a programmer prefers to draw a flowchart prior to writing a
computer program. As in the case of the drawing of a blueprint, the flowchart is drawn
according to defined rules and using standard flowchart symbols prescribed by the
American National Standard Institute.
Meaning of a Flowchart
Flowcharts Understanding and Communicating. How a Process Works (Also
called Process Maps and process Flow Diagrams)
A flowchart is a diagrammatic representation that illustrates the sequence of
operations to be performed to get the solution of a problem. Once the flowchart id
drawn, it becomes easy to write the program in any high level language.
Flowcharts facilitate communication between programmers and busines people.
These flowcharts play a vital role in the programming of a problem and are quite
helpful in understanding the logic of complicated and lengthy problems.
A flowchart can be used for-
l Defining and analyzing processes;
l Build a step-by-step picture of the process for
analysis, discussion, or communication purpose.
Magnetic Disk
Off-page connector
Flow line
Annotation
Display
a In drawing a proper flowchart, all necessary requirements should be listed out
in logical order.
a The flowchart should be clear, neat and easy to follow. There should not be
any room for ambiguity in understanding the flowchart.
a The usual direction of the flow of a procedure or system is from left to right or
top to bottom.
a Within each symbol, write down what the symbol represents. This could be the
start or finish of the process, the action to be taken, or the decision to be
made.
a Only one flow line should come out from a process symbol.
or
a Only one flow line should enter a decision symbol, but two or three flow lines,
one for each possible answer, should leave the decision symbol.
a Start the flow chart by drawing the elongated circle shape, and labeling it
``Start''.
a Then move to the first action or question, and draw a rectangle or diamond
appropriately. Write the action or question down, and draw an arrow from the
start symbol to this shape.
a Finally, challenge your flow chart. Work from step to step asking yourself if
you have correctly represented the sequence of actions and decisions involved
in the process.
a Flow charts can quickly become so complicated that you can't show them on
one piece of paper. This is where you can use ``connectors'' (shown as
numbered circles) where the flow moves off one page, to another. By using the
same number for the off-page connector and the on-page connector, you show
that the flow is moving from one page to the next.
Step II - Read N
Step IV - Print K
Step V - Assign K = K + 3,
If K < = N go to Step IV otherwise go to Step VI
Step VI - Stop
Start
Flowchart K=1
Read N
Print K
K=K+3
Yes K < = N
No
Stop
***
Q. 2. (a) Differentiate between constant and variables with example.
(or)
Explain following :
(i) Identifiers (ii) Variables
(iii) Constants (iv) Reserve words.
Ans.
Identifiers :
Name of variable, constant, function, Labels and various other user define
objects are called identifier.
Following are rules for constructing Identifiers :
(i) Identifier Name must start with alphabate or underscore sign.
(ii) Identifier Name may contain alphabate {a to z or A to Z}
Number {0 to 9} and under score sign only.
(iii) Special symbol {Like #, $ etc.} are not allowed in identifer name.
(iv) Maximum 8 character are allowed in identifier name.
(v) Two or more identifier should not have same name.
(vi) Blank space is not allowed within identifier name.
Example :
N of valid Identifier Name-
Count
Abc
xy2123
Add11
Sum-13
Example of some Invalid Identifier Names-
1New
NM 14
Total $ 43
Variables
A variable is named location in memory that is used for hold a value that may be
modify by program.
Variable must be declare before any executable statement.
A data type associate with variable define the set of values that may be store by
that variable and set of operation possible on that variable.
int a, b, c;
Float x, z = 10.75;
A variable may be declared on following basic places :
(i) At starting of main ( ) function or any other function defination.
(ii) As a function parameter in header of function.
(iii) Out side of any function i.e. global variable.
Types of Variables :
(i) Local Variables
(ii) Global variables / External.
Constant
Like a variable, a constant is named location in memory that is used to hold
value.
But value of a constant can not be change by program at Run Time.
Constants are also called literals.
For declaration of constant we should use const keyword with datatype and
constant identifier name.
Const int. k = 15;
Const float n = 12.67;
Constant can be divided into two categories-
(i) Primary Constant
(ii) Secondary constant
Const keyword is access modifier that stop to modify value of memory location.
Like variable, constant may be declare at three locations.
Reserve Words
Reserve words are also called keywords.
Reserve words are identifiers whose meaning has already been define C compiler.
Reserve words can not be used as variable or constant name because assigning a
new meaning to the reserve word is not possible.
There are 32 Reserve words/keywords in C programming Language.
27 keyword were define by the original version of C and 5 were added by the
ANSI C Committee.
Some complier may have some additional keywords also.
(i) It creation statements are also called loop statement.
(ii) Almost every modern programing language support loop statements.
(iii) Loop statement allow a set of instructions to be executed repeatedly Until a
certain condition is reached.
(iv) These condition may be :
(A) Predefined
(B) Open Ended.
***
Q. 2. (b) Briefly describe various types of data types used in C.
(or)
Explain Different data type available in C language.
Ans. A data type defines a set of values that a variable can store along with a set
of operation that can be performed on that variable. A variable is named location in
main memory that is used for holding a value that may be modify by the program.
There are five basic datatype in C Language.
(i) char [character]
(ii) int [integer]
(iii) Float [float]
(iv) double
(v) void
The size & range of these data type may vary between processor.
The range of float & double is also depend on method used to represent it.
Different data types are required because program requires to read (Input) &
write (output) different type of values.
int (integer)
It is used to accept Positive & Negative Numeric values.
Integer variable does not accept decimal part of value.
Generally Range of int. is from -32768 to + 32767.
Format Specifier for int. is %d.
int k, m = 17;
float
It also accepts positive & Negative Real type Numeric values.
Float type variable contains value with decimal point.
Format specifier for float is % f.
Normally range of Float is from
+ 38
3.4 × (10)-38 To 3.4 × (10)
Float K, j = 15.27;
Char (Character)
Char type variable accepts single Non -Numeric value, Numeric value or any
special character.
Char type variable treat each value as Non Numeric value.
Format Secifier for char is % C.
Char a, n = `A';
Double
Double type variable accept Real type Numeric value.
This contain value with decimal point.
Format Specifier for double is % 1F Range of double is from 3.4 × (10)-308 To 3.4 ×
(10)+ 308
***
Q. 3. (a) What are operators? Discuss all operators in detail.
(or)
Explain different types of operators available in C Programming Language.
(or)
Explain Following :
(i) Assignment operators
(ii) Arthmetic operators
(iii) Relational & Logical operators.
(iv) Bitwise operators.
(v) Increment & Decrement operators.
(or)
Explain Binary & Unary operators.
Ans. C is very rich in built in operators.
In any programming language operators are required to perform different type of
operations.
There are five main classes of operators-
(i) Assignment Operator
(ii) Arithmetic Operator
(iii) Relational and Logical Operators.
(iv) Bitwise Operator.
(v) Increment and Decrement Operator.
On the basis of operands required for execution of a operator, We can categorize
operators in two catagories :
(A) Binary operators
(B) Unary Operators.
(A) When a operator required two operands (on both left & right side) then it s
called Binary operator.
(B) When a operator required only one operand (on either side) then it is called
Unary Operators.
(1) Assignment Operator- Assignment Operator is Binary Operator.
Assignment Operator can be used with any valid expression.
Left side part of assignment operator must be variable or pointer name.
Constant or function name of left side of assignment operator is not allowed.
Right side of assignment operator may be constant, function, value, variable or
expression.
Variable Name = expression;
int k;
k = a + b;
k = 17;
Two terms Lvalue & Rvalue are used with assignment operator.
LValue means any object that can be used on left side of assignment operator.
Rvalue means any object that can be used on right side of assignment operator.
int a, b, c;
a = b = c = 75;
above is example of multiple assignment that means single value can be assign
to multiple variable within single statement.
Assignment operator works from right to left order.
Arithmetic Operators
C Programming Language Provide Five arithmetic operators. These are :
%/*
+-
All these operators are Binary operators.
Arithmetic operators can apply to almost any built in data type.
% operator can not be used with float data type.
Arithmetic operators required for performing different arithmetic operations are :
% Modulus operator
/ Division
* Multiplication
+ Addition
- Subtraction [Also known as Unary Minus]
Precedence of Arithmetic Operators-
Highest %/*
Lowest +-
Precedence of % / * operators are equal and + - operators are equal.
Arithmetic operator evaluate from left to right order.
Relational Operators
Operators Meaning
> is Greater than
>= is Greater than or equal to
< is Less than
<= is less than or equal to
== is equal to
!= is Not equal to
Logical Operators
Operators Meaning
&& And
¦¦ OR
! Not
Bitwise Operator
C Programing Language is a a bit oriented language.
Bitwise operator are used to perform setting or shifting the actual bits in a byte
or word.
Bitwise operator can be used with int, long & char types.
Bitwise operator cannot be used with float, double or void data types.
Bitwise operator operates on bit level. Bit level operation are important when
direct hardware interaction is required.
Bit Manipulation operations are very powerful feature of C programming
language.
BITWISE OPERATOR
Operator Meaning
& And
¦ OR
^ Exclusive OR [XOR]
~ Not
***
Q. 3. (b) Generate a table for a given no. (to be given by user) with the help
of while, do while and for loop.
/*.... Working note; For explanation only not type or Write.... */
Ans. Program
#include<conio.h>
#include<stdio.h>
void main ( )
{
int k;
clrscr () ;
Table1 (k); /* FUNCTION Calling for Table with help for loop */
Table2 (k); /* FUNCTION Calling for Table with help while loop */
Table 3 (k); /* fUNCTION Calling for Table with help do-while loop */
getch ();
}/* end of void main () function */
void Table1 (int j)
{
int res, n;
do
{
res = n*j;
printf (``%d\t'', res);
n++;
}
TABLE
while (n ... = 10); /* end of do-while loop */
} /* end of void Table3 () function */
***
Q. 4. (a) Differentiate between call by value and call by reference concept
with suitable example.
Ans. Differentiation between Call by Value and call by Reference
Function can be called by two ways-
1. Call By Value
2. Call By Referene
This difference is based on how values are passed [parameter / argument
passing] during function calling. Parameter passing is a mechanism for
communication of data and information between Function Caller and Function Called.
Parameter pasing can be possible either by Value Passing or by Address Passing.
Parameter can be calssified in two categories-
(I) Formal Parameters / Arguments
(II) Actual Parameters / Arguments
Formal Parameters- Parameters within function definition [before body of
function] are called Formal Parameters. Variables within formal parameters and
function body local variable for that function and scope of that variables are limited to
a particular function.
Actual Parameters- Parameters used with Function Calling are called Actual
Parameters. Number & Types of parameters with Formal Parameters and Actual
Parameters must be same for a particular function.
1. Call By Value
In Call By Value content of actual parameters are copied in formal parameters.
By using this mechanism the content of actual parameters do not change even if
content of formal parameters are changed within function called.
Changes of content are only applicable to formal parameters. No effect to the
actual parameters.
Variables in actual and formal parameters are two different entities in Call by
value.
2. Call By Reference
Instead of passing values, addresses of actual parameters are passed during
function calling. In Call By Reference Mechanism actual parameters addresses are
passed to formal parameters not values during function calling. i.e. function calling is
perform by using pointers.
By using this mechanism change in the content of formal parameters are also
effected to the content of actual parameters. Any changes are applicable to both sides.
This mechanism is used when multiple values return is required from function
called because by return statement a function can return at most one value at a time.
CALL BY VALUE
Example
void SwapV (int x, int y);/* prototype */
void main()
{
int a,b;
Clrscr ();
printf(''\nExchange two values, call By value.... '');
printf (''\n\nEnter first Integer value : '');
scanf (''%d'', & a);
printf (''\n Enter second Integer value : '');
scanf (''%d'', & b);
SwapV (a, b); /* Function calling with help of call by value */
getch();
} /*end of void main() */
void Swapv (int x, int y)
{
int z;
z = x;
x = y;
y = z;
printf(``\n Values after exchange '');
printf (``\n a = %d And b = %d'', x, y);
} /* end of void SwapV() */
CALLR BY REFRENCE
void SwapR (int *x, int *y); /* Prototype */
void main) ()
{
int a, b;
clrscr ();
printf (''\nExchange two values, call By Reference Example....'');
printf (''Enter first Integer value : '');
scanf (''%d'', &a);
printf (''\n Enter second Integer value : '');
scanf (''%d'', &b);
SwapR (&a, &b) ; /* Function calling, call by reference */
printf (''\n Values after exchange '');
printf (''\n a = %d And b = %d'', a, b) ;
getch () ;
} /* end of void main () */
void SwapR (int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
} /* end of void SwapR () */
***
Q. 4. (b) Calculate factorial of a number using recursion.
Ans.
/*.... Working note; For explanation only not type or Write.... */
PROGRAM
#include<conio.h>
#include<stdio.h>
void main ()
{
long f, n;
clrscr ();
getch ();
}/* end of void main () function */
if (f < = 1)
return (1);
else
return (f* (fact (f-1)));/* Function calling itself i.e. Recursion */
***
Q. 5. (a) What is an array? How it is different from a single variable, also
differentiate between one dimension and multidimension array.
Ans.
ARRAY :
An array is a collection of variables of the same type and they are referred
through a common name.
A specific array element is accessed by using array name and Index Value of that
element.
Index value of array elements always starts from zero, i.e. Index value of first
array element is zero. All array elements are located at continous memory location.
The lowest address corresponds to the first array element and the highest
address of the last array element.
Array may be :
(i) One Dimension Array
(ii) Two Dimension Array
(iii) Multi Dimension Array
Array declaration is required when multiple variable declaraion requires those
having the same data type. Like if we want to store marks of 100 students, for such
purpose we require 100 variables and we can do this using array of size 100.
String is a array of character.
(1) One Dimension Array-
int a{20} ;
in above example, a is array name. Size of a is 20 i.e. array a has 20 elements.
a[4] = 75;
value 75 is assigned to element number 4 i.e. 5th element of a named array.
because 1st element is Number O [zero] in array.
a[20] = 100 ; (/* Invalid */)
above statement is invalid because a array have element Number a [0] to a [19]
and we are trying to assign value 100 to element number 20 thus it is in valid
statement.
In C language, array element number and boundation checking is responsibility
of program.
long n [50] ;
array n have element n[0] to n [4]
Single Dimension arrays are essentially list of information of the same data type
stored in contiguous memor locations in index order.
(ii) Two Dimension Array- [2-D Array or Two-D Array is simplest form of
multidimension Array.
Two Dimension array are stored in row -column matrix format. The generalized
form of 2-D array is-
datatype array name [m] [n] ;
Where m is number of Row &
n is number of Column
long k[5] [3] ;
K is name of 2-D array, having 05 rows & 03 columns. Array k have total 5 x 3 =
15 elements.
Array indexing in 2-D array is also start from O [zero].
K [2] [1] = 175 ;
Value 175 is assigned to element of Row Number 2 i.e. 3rd row and column
number 1 i.e. 2nd column of array k.
K[5] [3] Columns
Array K
No of Row x No. of Column = Total No of elements
Total Element in 2-D array K is - 5 x 3 = 15 total elements.
(iii) Multi Dimension Array [N-D Array]- More then 2-D array is known as Multi
Dimension Array. it may be 3-D, 4-D, 5-D or 50 on.
Normally more then 3-D array is not used because that requires more time to
access element values.
The generalize form of N-D array is - datatype array name [Siz 1] [Size 2] [Size
3] [Size 4] ........ [Size N] ;
Array indexing always start from 0.
Within Set of [ ] We can mention either constant or value in array declaration.
During Declaration, In 1-D array there is
1 set of brackets,
In 2-D array there is
2 set of brackets and so on.
***
Q. 5. (b) Write a Program to add 2 m × n matrix.
Ans.
/*.... Working note; For explanation only not to type or Write.... */
PROGRAM
# include<stdio.h>
# include<conio.h>
void main ()
{
int row = 6, col = 2, m, n, x, z;
int a[5] [5], b[5] [5], c[5] [5], /* Array Declaration*/
clrscr ();
col = 2;
row = row + 2;
} /* end of 1st for loop */
row = 4;
col = 40;
col = 4;
row = 12;
getch ();
}/* end of main () function */
***
Q. 6. (a) Write a program that will swap value of two variables without using
third variable and using third value.
Ans.
/*.... Working note; For explanation only not to type or Write.... */
PROGRAM
void Swap (int x, int y); /* Prototype */
void Swap1 (int x, int y); /* Prototype */
void main ()
{
int a, b;
clrscr ();
z=x;
x=y;
y=z;
x=x+y;
y=x-y;
x=x-y;
***
Q. 6. (b) Write benefits of switch over if else statement with example.
(or)
Explain different selection statements.
(or)
Explain Different conditional statement.
(or)
Explain following :
* if() statement
* if () - else statement
* Switch statement, Nested if.
(or)
Explain how we can execute any statement conditionally.
Ans. If (), if ()-else & Switch are conditional statement.
This conditional statement is also called selection statement.
Conditional / Selection statements are used to control execution flow of program.
Sometimes it is required for execution of a particular statement if certain
condition is true & otherwise another statement execution is required in such case
conditional statements are required.
It is called conditional / selection statement because it depend on condition that
what statement will execute next or selection of execution path is condition based.
Following conditional statements are available in C Language-
(i) If (Condition)
{
Statement 1;
Statment 2;
}
(ii) If (Condition)
{
Statement 1;
Statement 2;
Statement N;
}
else
{
statement 1;
Statement 2;
Statement N;
}
Cases condition 2;
Statement;
break;
Case Condition N :
Statement ;
break ;
default ;
statement;
} /* end of switch */
If Conditional expression evaluation is true then related statement is excuted
other wise next statement.
Generally Conditional expression consist Relational & Logical Operators.
Relational & Logical operators return value either true or false.
(1) if (condition)
{
Statement 1;
Statement 2;
Statement N;
}
Condition is normally conditional expression, that returns either true of false.
if condition evaluates true then related statement block is executed otherwise
skip.
if () condition may contain zero, one or more statements.
Normally if () statement is used if there is only one condition to check.
The condition is always enclosed within a pair of parentheses.
Example
(i) int k = 15;
if (k ... 10)
{
printf (``\n k is greater then 10'')
printf (``\n value of k = %d'', k);
}
Value k is 15 i.e. k > 10 so condition is true & both statement will execute.
Sequentially.
(ii) if (k > = 10)
{
Printf (``\n This will not execute)
}
Now condition is false so related statement will not executes.
(iii) int a = 7, b = 15;
if (( a < 10) && (b > = 10)
{
Printf (``\n value of a = %d'' a);
Printf (``\n Value of b = %d'', b);
}
both condition are true hence total condition generate true value & both
statements will execute that print value of a & b display.
if (Cond) - else
if (Condition)
{
Statement 1;
Statement 2;
Statement N;
}
else
{
Statement 1;
Statement 2;
Statement N;
}
if() else format of conditional statement is used when there are only two
conditions & one of them is true.
In other words we can say when one condition is true that ensure that another
one is false and first one is false that ensure that second is true then we use if () else
format of conditional statement.
Else is always used after if & with else condition is never specified. We can use if
without else but vice versa is not true.
If condition is true then statement related with if will executes and statement
related with else skips.
If condition is false then if statement will skip and Else statement will execute.
In short execution of else depend on if. If is true then else will not execute
otherwise else executes.
Else part is optional if & Else is not used then it is a simple if conditional
statement.
Example-
int a = 10, b = 25;
if (a > = b)
{
Printf (" Value of a = %d is greater ''; a);
3 ( * end of if condition */)
else
{
printf (''\n value of b = %d is greater'', b);
3/* end of else part */
Here are only two conditions either value of a is greater or b. So if else format is
used.
In this example condition specified with if is false, so if part is skiped and Else
part will execute.
if- else if - else ladder-
When there are more then two conditions and only one is true then if - else if-else
format is used.
if (condition)
{
Statement block;
}
else if (condition)
{
Statement block;
}
else if (condition)
{
Statement block;
}
else
{
Statement block;
}
The conditions are evaluated from the top to down approach looking for first true
condition.
If all conditions in ladder are false then else part will execute.
Else part is optional to use. If else part is not present and none condition is true
then no statement will execute.
int n = 25;
if (n > = 40)
{
printf (" Value of n = %d'', n);
}
else if (n > = 30)
{
Printf (" Value of n = %d'', n);
}
else if (n > = 20)
{
Printf (" Value of n = %d'', n);
}
else
{
Printf (" Value of n = %d'', n);
}
Conditions are checked in Top to Down way & looking for true condition. This
Condition i.e. (n > = 20) is true so related statement will executes & others are
skipped.
With if (Condition), if (Condition)- else or if (Condition)- else if (Condition)- else
ladder, we can use any loop. Statement, Conditional Statement, Jumping Statement or
I/O Statement as a valid Statement.
Switch Statement
C Programming Language has a built in multiple branch selection statement
called switch statement.
Switch tests for equality condition in top to down approach. When condition is
statisfied associate statements are execute and remain part switch is skiped.
Switch only checks for equality and so no Relational operator be can used with
Switch-case statements.
For multiple conditions even we can use if -else if-else ladder but increasing depth
of if-else if-else ladder makes it difficult to understand so in place of this we can use
switch statement.
According to standard C specification; switch can have at least 257 case
statement, but if may vary from complier to complier.
Switch (expression)
{
Case Constants 1;
Statement block 1;
break;
case constant 2;
Statement block 2;
break;
Case constant 3;
Statement block 3;
break;
case constant 4;
statement block 4;
break;
default ;
statement block;
3 /* end of switch statement */
The value of expression is tested against the value of the constant in the case
statement. When match is found the statement block associate with case will execute.
3 /* end of switch statement */
The value of expression is tested against the value of the constant in the case
statement. When match is found the statement block associated with case will
execute.
If none case statement will execute then default execution is performed.
Use of default is optional.
With case we can only use constant or value, i.e. No variable can be used with
case.
Expression must be integer or character Floating point expression is not allowed.
No, two case constant in the same switch can have identical values.
***
Q. 7. (a) What are pointers? With the help of a program explain how to pass
points in a function?
Ans.
***
Q. 7.(b) Discuss go to, break and continue statements giving suitable
example.
(or)
Explain various Jump Statements.
(or)
Explain various unconditional Jump statements.
(or)
Explain following :
(i) break
(ii) continue
(iii) goto
(iv) return.
Ans. Jumping Statements are used to transfer execution control to any other
point within the program during exectuion time.
After transfering execution control program execution flow. resume sequential
from that point.
Break Statement-
Break Statement can be used within any loop statement or with Switch -Case
statement.
Keyword break forces to immediate termination of a loop or switch -case
statement. Normal loop/switch termination condition is by passed when break is
used.
Break statement sends execution control outside of loop/switch statement &
resume sequential execution flow after loop/switch-case part.
int k;
for (k = O; k < 75; k++)
{
printf (``\n value = %d'', k);
if (k = = 15)
break;
} /* end of for loop */
Above loop is executed & printed till value of k is 15 and after that if ( ) condition
is true & break executed that causes immediate termination of loop & bypass the
normal termination condition of loop.
Continue Statement
Continue is a keyword in C language. Continue statement can be used with :
(i) For loop
(ii) While loop
(iii) Do-while loop
When continue statement execute; remaining part of loop is skipped & execution
control pass to starting of loop for next iteration of loop.
Void main ( )
{
long k, m = 1;
clrscr ( );
Printf (''Program for multiplication '').
Printf (''\n for exit press O '');
For (; ;)
{
Printf (``\n Enter any positive integer value '');
Scanf (``%/d ''value & k);
if (k = = 1)
Continue;
if (k = = O)
break ;
m = m * k;
3 /* end of for loop */
Printf (''/n Multiplication = %/d'', m)
getch ( ) ;
} /* end of void main () */
Goto Statement
goto is one of the 32 keywords available in C language.
goto statement can be used anywhere in program for transfer execution control
within the function.
The goto requires a label for execution. A label is any valid identifier.
Label with goto statement indicates that where to transfer execution control.
Goto statment can not transfer execution control outside of a function.
Structured languages like C discourage use of goto statement because goto
increase times & memory complexity of program.
Structured languages like C directly support various loop and conditional
statement for control & transfer execution flow.
Void main ( )
{ int k = O;
clrscr ( );
indore : /* label indore */
k++ ;
Printf (''\n %d = Indore'', k);
if (k = = 10)
exit (0); /* terminate Prog. execution */
goto indore ; /* jump to label indore */
getch ( );
3 /*end of join main () */
Return Statement
The keyword return is used to return execution control from a function.
Return statement can be categorized as a jump statement because it returns
[Jump back] from a function to point of function calling.
The return statement can be used within Non-void function.
In other words return statement can be used within a function if function data
type is not void.
Void function can not be used with return statement.
In a non-void function if return is not used, function returns garbage value.
The return statement can be used anywhere within a non -void function and as
soon as return statement function execution stop & exectuion control returns to
function calling point.
general form of return statement-
return value/variable/constant/expression/
We can use as many as required return statement.
A return statement can return only one value.
Exit Statement
exit ( ) is not a kind of jump statement.
On execution of exit ( ) statement entire program execution termintes
immediately.
exit ( ), statement returns execution control from program to operating system.
The exit ( ) is library function of C languge.
Jeneral form of exit ( ) is-
void exit (int return value);
stdlib.h header file is required to includes for exit ( ) function.
exit ( ) return O to operating system on normal terminator.
***
ed out in logical order.
i The flowchart should be clear, neat nd easy to follow. There should not be any room
for ambiguity in understanding the flowchart.
i The usual direction of the flow of a procedure or system is from left to right or top to
bottom.
i Within each symbol, write down what the symbol represents. This could be the start or
finish of the process, the action to be taken, or the decision to be made.
i Only one flow line should come o