ITP - ITP Paper

advertisement
INTRODUCTION TO PROGRAMMING
Q1.(a) Define Algorithm. Why they are used?
Ans. (a) An algoritm is a Step by step process of perform any task. With the help of algorithm
We can get the end result. algorithm refers to the logic of a program. It is a step by
step description of how to arrive at a solution to a given problem. It is defined
as a sequence of instructions that when executed in the specified sequence the
desired results are obtained.
The algorithm should be finite, unambiguous,Definite,
Q1. (b) Explains (i) break (ii) continue.
Ans. (i) The break statement is used to terminate the execution of the nearest
enclosing loop in which it appears.
• The break statement is used with for loop, while loop and do while loop.
• Syntax: break;
• When compiler encounters a break statement, the control passes to the
statement that follows the loop in which the break statement appears.
.....
Example:
for (...)
{
......
if (condition)
{
break;
}
......
} Transfer control out of the For Loop
(ii) Continue Statement: When the compiler encounters a continue statement,
then the rest of the statements in the loop are skipped and the control is unconditionally
transferred to the loop-continuation portion of the nearest loop.
Syntax: continue;
Example :
While (...)
{
............
if (condition)
continue;
............}
Q1. (c )What is the difference between structure and unions in C?
Ans. Structure:
1. Memory is allocated for all variables.
2. We can access all the members of structure at anytime.
3. All members of structure can be initialized
4. 'struct' keyword is used to declare structure.
5.
struct structure_name
{
structure data_element 1;
structure data_element 2;
------------------structure data_element n;
}struct_variable_name;
Union:
1.
2.
3.
4.
Allocates memory for variable which variable require more memory.
Only one member of union can be accessed at anytime.
Only the first member of a union can be initialized.
'union' keyword is used to declare union.
union union_name
{
union data_element 1;
union data_element 2;
------------------union data_element n;
}union_variable_name;
Q1. (d) Explain the use of 2-d array in C.
Ans. 2-D arrays are used in C when me want to store data in the form of matrices
or tables. A 2-D array is specified using two subscripts where one subscript denotes row
& the other denotes column. A 2D array can be viewed as an array of arrays.
Example :
int marks [2] [3];
Each dimension of the 2-D array is indexed from zero to its maximum size minus 1.
The first index selects the row & the second selects the column.
A 2-D array is stored sequentially in memory and not as a rectangular grid form.
Write a program to print the elements of a 2D array.
# include <stdio.h>
# include <conio.h>
main ()
{
int arr [2] [2] = {12, 34, 56, 32};
int i, j;
for (i = 0; i < 2; i++)
{
printf (‘‘/n’’);
for (j = 0; j < 2; j++)
printf (‘‘%d/t’’, arr[i] [j]);
}
return 0;
}
Output
12 34
56 32
Q. 2. What do you mean by Flow Chart. What are various symbols used in making flow
Chart and what are the advantages of flow chart?
Ans. A flowchart is a pictorial representation of an algorithm. It is a program planning
tool for visually organizing a sequence of steps necessary to solve a problem using a
computer. It uses boxes of different shapes to denote different types of instructions.
The main advantage of using flowchart is that a programmer need not pay attention
to the details of the elements of the programming language. The process of drawing a
flowchart for an algorithm is flowcharting.
Flowchart Symbols: A flowchart uses boxes of different shapes to denote different
types of instruction. The use of symbols makes it easier to communicate program logic
through softwares.
The ANSI (American National Standards Institute) has standardized these basic
flowchart symbols.
1. Terminal symbol.
It indicates start, stop in a program’s logical flow.
2. Input/Output symbol.
It denotes any function of an i/p or o/p in a program.
3. Processing Symbol
It represents arithmetic & data movement instructions. Hence all arithmetic
operations like addition, subtraction, multiplication, division are indicated
by a processing symbol in flowchart.
4. Decision symbol
The decision symbol indicates a decision point, i.e., a point at which a branch
to one of two or more alternative points is possible.
5. Flow lines
It indicates the exact sequence in which instructions are executed.
6. Connectors.
Wherever a flowchart becomes so complex that the number & direction of flow
lines.
Advantages:
a) Easy to analyze the program
b) Especially for c language where structure is important it helps a lot.
c) Focus in logic.
d) Without a good flowchart you cannot write software.
e) Conditional statements are easy to analyze.
f) Occupies less paper compared to a large programs having huge statements.
Q. 3. What is an operator? List down different types of operators in C.
Ans: An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations. C language is rich in built-in operators and provides the following
types of operators:






Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Special Operators
This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other
operators one by one.
Arithmetic Operators
Following table shows all the arithmetic operators supported by C language. Assume variable A
holds 10 and variable B holds 20 then:
Operator Description
+
Adds two operands
Subtracts second operand from the first
*
Multiplies both operands
/
Divides numerator by de-numerator
Modulus Operator and remainder of after an integer
%
division
++
Increments operator increases integer value by one
-Decrements operator decreases integer value by one
Relational Operators:
Operator Description
Checks if the values of two operands are equal or
==
not, if yes then condition becomes true.
Checks if the values of two operands are equal or
not, if values are not equal then condition becomes
true.
Checks if the value of left operand is greater than
the value of right operand, if yes then condition
becomes true.
Checks if the value of left operand is less than the
value of right operand, if yes then condition
becomes true.
Checks if the value of left operand is greater than or
equal to the value of right operand, if yes then
condition becomes true.
Checks if the value of left operand is less than or
equal to the value of right operand, if yes then
condition becomes true.
!=
>
<
>=
<=
Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A
holds 1 and variable B holds 0, then:
Operator Description
Called Logical AND operator. If both the operands
&&
are non-zero, then condition becomes true.
Called Logical OR Operator. If any of the two
||
operands is non-zero, then condition becomes true.
Called Logical NOT Operator. Use to reverses the
!
logical state of its operand. If a condition is true
then Logical NOT operator will make false.
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^
are as follows:
A
0
0
1
1
B
0
1
1
0
A&BA|B
0
0
0
1
1
1
0
1
A^B
0
1
0
1
Operator Description
Binary AND Operator copies a bit to the result if it
&
exists in both operands.
Binary OR Operator copies a bit if it exists in either
|
operand.
Binary XOR Operator copies the bit if it is set in
^
one operand but not both.
Binary Ones Complement Operator is unary and
~
has the effect of 'flipping' bits.
Binary Left Shift Operator. The left operands value
<<
is moved left by the number of bits specified by the
right operand.
Binary Right Shift Operator. The left operands
>>
value is moved right by the number of bits
specified by the right operand.
Assignment Operators
There are following assignment operators supported by C language:
Operator Description
Simple assignment operator, Assigns values from
=
right side operands to left side operand
Add AND assignment operator, It adds right
+=
operand to the left operand and assign the result to
left operand
Subtract AND assignment operator, It subtracts
-=
right operand from the left operand and assign the
result to left operand
Multiply AND assignment operator, It multiplies
*=
right operand with the left operand and assign the
result to left operand
Divide AND assignment operator, It divides left
/=
operand with the right operand and assign the result
to left operand
Modulus AND assignment operator, It takes
%=
modulus using two operands and assign the result
to left operand
<<=
Left shift AND assignment operator
>>=
Right shift AND assignment operator
&=
Bitwise AND assignment operator
^=
bitwise exclusive OR and assignment operator
|=
bitwise inclusive OR and assignment operator
Special Operators : sizeof & ternary
There are few other important operators including sizeof and ? : supported by C Language.
Operator Description
sizeof() Returns the size of an variable.
&
Returns the address of an variable.
*
Pointer to a variable.
?:
Conditional Expression
Q. 4. (a) Explain the various Data types in C language.
Ans.
1. Fundamental data Type
Integer
Integer data type is used to store numeric values without any decimal point e.g. 7,
–101, 107, etc.
A variable declared as ‘int’ must contain whole numbers e.g. age is always in number.
Syntax:
Int variable name;
E.g. int roll, marks, age;
Float
Float data type is used to store numeric values with decimal point. In other words,
float data type is used to store-real values, e.g. 3.14, 7.67 etc. A variable declared as
float must contain decimal values e.g. percentage, price, area etc. may contain real
values.
Syntax:
Float variable name;
E.g. float per, area;
Character
Char (Character) data type is used to store single character, within single quotes
e.g. ‘a’, ‘z’, ‘e’ etc. A variable declared as ‘char’ can store only single character e.g. yes or No
Choice requires only ‘y’ or ‘n’ as an answer.
Syntax:
Char variable name;
E.g. char chi=‘a’, cha;
Void
Void data type is used to represent an empty value. It is used as a return type if a
function does not return any value.
Type modifier
Type modifier is used to change the meaning of basic or fundamental data types so
that they can be used in different situations. Various type modifiers are-signed, unsigned,
short and long.
C language offers 3 different ‘int’ type modifiers-short, int, and long that represents
three different integer sizes. C language also offers 3 different floating point type
modifiers-float, double, and long double. Two special ‘char’type modifiers are-signed,
unsigned.
Type Size (in bytes) Range
Int (signed/short) 2-32768 to 32767
In (unsigned) 2 0 to 65535
Long (signed) 4-2, 147,483,648 to 2, 147, 483, 648
Long (unsigned) 4 0 to 4,294,467,295
10 Second Semester Introduction to Programming, May-June 2013
Float 4 3.4*10-34 to 3.4*10-34
Double (long float) 8 1.7*10-308 to 1.7*10-308-1
Long double 10 3.4*10-4932 to 3.4*10-4932-1
Char 1 –128 to 127
Signed char 1 0 to 255
Unsigned char 1 –128 to 127
2. Derived Data Types
Data types that are derived from fundamental data types are called derived data
types. Derived data types don’t create a new data type; instead, they add some
functionality to the basic data types. Two derived data type are - Array & Pointer.
Array
An array is a collection of variables of same type i.e., collection of homogeneous
data referred by a common name. In memory, array elements are stored in a continuous
location.
Syntax:
[ ];
E.g. int a[10];
char ch [20];
According to the rules of C language, 1st element of array is stored at location a[0],
2nd at a[1] & so on.
Pointer
A pointer is a special variable that holds a memory address (location in memory) of
another variable.
E.g. if we want to store the address of an ‘int’ data type variable into a pointer
variable, it is done in the following manner:
int a, *b;
b=&a;
In the above case, variable ‘b’ stores the address of variable ‘a’.
3. User Defined Data Type
User defined data type is used to create new data types. The new data types formed
are fundamental data types. Different user defined data types are; struct, union, enum,
typedef.
Struct
A struct is a user defined data type that stores multiple values of same or different
data types under a single name. In memory, the entire structure variable is stored in
sequence.
Syntax:
Struct< structure name>
{
var1;
Var2;
.....
.....
};
Union
A union is a user defined data type that stores multiple values of same or different
data types under a single name. In memory, union variables are stored in a common
memory location.
Syntax:
Union < tag name>
{
Var1;
Var2;
.....
.....
};
Enum:
An enumeration is a data type similar to a struct or a union. Its members are
constants that are written as variables, though they have signed integer values. These
constant represent values that can be assigned to corresponding enumeration variables.
Syntax:
Enum {value1, value2, ......, value n};
E.g. :Enum colors {black, blue, cyan, green, red, yellow};
Color foreground, background;
Typedef:
The ‘typedef’ allows the user to define new data-types that are equivalent to existing
data types. Once a user defined data type has been established, then new variables,
array, structures, etc. can be declared in terms of this new data type.
A new data type is defined as:
Typedef type new-type;
Type refers to an existing data type,
Q.4. (b) Why we use switch case statement in C? Write a program for selection
of Grading point for given students using switch case statement and break.
Ans. A switch case statement is a multi-way decision statement that is a simplified
version of an if-else block that evaluates only one variable. Switch-case statement in C
is used for :
(a) When there is only one variable to evaluate in the expression.
(b) When many conditions are being tested for.
(c) Executes faster than its equivalent if-else construct.
(d) Easy to read and understand.
(e) Easy to debug.
Syntax of Switch Statement :
Switch (variable)
{
case value 1;
statement block 1;
break;
Case value 2 :
Statement block 2;
break;
.
.
.
.
.
case value N:
statement block N;
break;
default:
statement block;
break;
}
The switch case statement compares the value of the variable given in the switch
statement with the value of each case statement that follows. When the value of the
switch & the case statement matches, the statement block of that particular case is
executed.
Program for Selection of Grading Point
# include <stdio.h>
#include <conio.h>
void main ()
{
clrscr();
int n;
Printf (‘‘Enter the grade of the student’’);
Scanf (‘‘%d’’, &n);
Switch (n)
{
Case 0 :
If (n >=60)
Printf (‘‘The student has got the first division’’);
break;
Case 1 :
if ((n > = 45) && (n < 60))
Printf (‘‘The student has got the second vision’’);
break;
Case 2 :
if (n > 33) && (n < 45))
printf (“The student has got the third division”);
break;
Default:
Printf (‘‘The student got failed’’);
getch ();
}
Q. 5. (a) Write various characteristics of C programming
Ans. C is a robust language which combines the features of assembly language and
high level language which makes it best suited for writing system software as well as
business package.
Characteristics :
1. C has only 32 keywords. This makes it relatively easy to learn as compared to
other languages.
2. It makes extensive use of function calls.
3. C enables the programmer to concentrate on the problem at hand & not worry
about the machine code on which the program would be run.
4. C supports pointer to refer computer memory, array, structures & functions.
5. C is an extensible language as it enable the user to add his own function to the C
library.
6. C is a portable language i.e., a C program written for one computer can be run on
another computer with little or no modification.
7. C facilitates low level (bitwise) programming.
8. C is the core language & many other programming languages (like C++, perl etc.)
are based on C.
Thus, learning other languages becomes easy if you have the knowledge of C.
Q. 6. What is Array in C? Write a program to find the Multiplication of any two
matrices.
Ans. An array is a collection of similar data items or elements. These data elements
have the same data tape.
Declaration of an array: An array can be declared using :
Data type, Name & Size
Syntax :
Data_type name_of_array [size];
e.g.,
int arr[50]; / / This declare an array of 50 items whose data type is integer.
float a[50]; / / This defines an array of 50 items whose data type is float.
char str[50] / / This defines an array of 50 items whose data type is char.
Accessing Element of the Array : To access the elements of the array, FOR loop
is used. Once an array is declared, then it can be referred with subscript. The number in
the bracket following the array name is the subscript.
Program to find multiplication of two matrices :
#include <stdio.h>
#include <conio.h>
void main ()
{
int i, j,k;
int m1 [2] [2], m2[2] [2], mul[2] [2];
clrscr ();
printf (‘‘Enter the elements of 1st Matrix’’);
for (i=0; i<2; i++)
{
for (j=0; j<2; j+=)
{
scanf (‘‘%d’’, &m1[i][j];
printf (‘‘Enter the elements of I2nd Matrix’’);
for (i=0; i<2; i++)
{
for (j=0, j<2; j++)
{
Scanf (‘‘%d’’, &m2[i][j]);
}
}
for (i = 0; i < 2; i++)
{
for (j=0; j<2; J++)
{
mul[i][j]=0;
for(k=0;k<2;k<++)
mul[i][j]=mul[i][j]+m1[i][k]*m2[k][j];
}
}
printf (‘‘\n Result after multiplication\n’’);
for (i = 0; i < 2; i++)
{printf(“\n”);
for (j = 0; j < 2; j++)
{
printf (‘‘\t%d’’, sub [i] [j]);
}
}
getch();
}
Output
Enter elements of Ist Matrix
56
78
Enter elements of IInd Matrix
12
34
The elements of Resultant Matrix are
44
44
Q. 7. What is Function in C? Explain the ‘‘Call by value’’ and ‘‘Call by
reference’’ with the help of swapping two integer values without using third variable.
Ans. A function is a group of statements that together perform a task. Every C
program has at least one function, which is main(), and all the most trivial programs
can define additional functions.
You can divide up your code into separate functions. How you divide up your code
among different functions is up to you, but logically the division usually is so each
function performs a specific task.
A function declaration tells the compiler about a function’s name, return type, and
parameters. A function definition provides the actual body of the function.
The call by value method of passing arguments to a function copies the actual value
of an argument into the formal parameter of the function. In this case, changes made to
the parameter inside the function have no effect on the argument.
By default, C programming language uses call by value method to pass arguments.
In general, this means that code within a function cannot alter the arguments used to
call the function. Consider the function swap() definition as follows.
/* function definition to swap the values */
void swap(int x, int y)
{
x=x+y;
y=x-y;
x=x-y;
}
The call by reference method of passing arguments to a function copies the address
of an argument into the formal parameter. Inside the function, the address is used to
access the actual argument used in the call. This means that changes made to the
parameter affect the passed argument.
To pass the value by reference, argument pointers are passed to the functions just
like any other value. So accordingly you need to declare the function parameters as
pointer types as in the following function swap( ), which exchanges the values of the two
integer variables pointed to by its arguments.
/* function definition to swap the values */
void swapp(int *x, int *y)
{
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf(“\n Enter the value of a & b”);
scanf(“%d %d”,&a,&b);
swap(a,b);
printf(“\n Values after swapping a= %d b=&d”,a,b);
swapp(&a,&b);
printf(“\n Values after swapping using reference a=%d b=%d”,a,b);
getch();
}
Download