Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#1 Which of the following declarations are valid ? a. int s_p_array [2][2] = { { 0, 0 }, { 0, 0 } }; main () { .. } b. main () { int s_p_array [2] [2] = { { 0, 0 }, { 0, 0 } }; }
NIIT SEM Q/CPR/CR/SESSION 3/1/VER06/95
NIIT
NIIT
#6
NIIT
#8
In which of the following assignments is there a possibility of loss of data ? (I is an int, f a float and d a double) a. I = d; b. d = f + I; c. f = d + I;
NIIT
# 10
NIIT
# 11
State whether true or false In the snippet below : if (y = 0) { statement x; } the statement x will always get executed.
NIIT
The for construct scanf (%d, &s_p_array [2] [1]); # define SB 6 # define PR 5 No. The second index (36 = length of second (longest) string + 1) has to be specified.
SEM Q/CPR/CR/SESSION 3/8/VER06/95
#5
NIIT
#7
#8
a. b. c.
Loss of data. Int set equal to a double. No loss of data. Double on left-hand side. Loss of data. All variables are converted to double and the result (a double) is truncated to a float.
NIIT
total+ + ;
is more concise and thus commonly used. # 11 False. Note that the if statement does not check for y equal to 0 but always sets y to 0. Thus the if condition : (y = 0)
NIIT SEM Q/CPR/CR/SESSION 3/10/VER06/95
NIIT
SPL SESSION
Objectives At the end of this session, you will be able to : Use the following operators in C programs : Unary operators Binary operators Ternary operators Compound assignment operators Increment / decrement operators
Use character arithmetic and state the rules of conversion between different data types
NIIT
SPL SESSION(contd.)
Objectives (contd.) Use two-dimensional integer arrays in C programs in terms of : Declaration Initialisation Input to these arrays Manipulation Printing State the advantages of # define statements in the
preprocessor phase
NIIT
SPL SESSION(contd.)
Objectives (contd.) Use two-dimensional character arrays in C programs in terms of : Declaration Initialisation Printing
NIIT
OPERATORS IN C
Unary Operator
The minus sign (-) changes the sign of a value If j - 1 -j is equal to -1 If i = -3, -i is equal to 3
Binary Operators +, -, *, /, and % The modulo operator % is used for finding the remainder in integer division y=x%5 If x = 7, then y = 2 /* x and y are both ints */
NIIT
OPERATORS IN C(contd.)
Character Arithmetic Character arithmetic is possible in C char ch; ch = S; ch = ch + 32; ch= ch - 32; /* ch set to S */ /* ch now equal to s /* ch back to S */
NIIT
OPERATORS IN C(contd.)
Type Conversions
Rules of conversion of mixed types in an assignment d1 = f1 + i1; /* d1 a double, f1 a float and i1 an int */ All operands of type char are converted to int All floats are converted to doubles If either operand is double, the other is converted to a double, giving a double result Loss of data could occur during the conversions due to truncation f1 = 31 + i1; /* d1 a double, f1 a float and i1 an int */
NIIT
OPERATORS IN C(contd.)
Type Cast
Variables/values can be type cast to forcibly change the result of an expression before making an assignment int i, j ; double d;
d = (double) i/j;
NIIT
OPERATORS IN C(contd.)
Ternary Operator Syntax test-expression ? T-expression : F-expression Short (and sweet) substitute for the verbose if.else construct Example quotient = ( b ) ? ( a / b ) : LARGE_ NO;
NIIT
OPERATORS IN C(contd.)
Compound Assignment Operators
Example
sum + = this_ones_value; is equivalent to :
NIIT
OPERATORS IN C(contd.)
Increment/Decrement Operators ++ -(Preincrement / Postincrement) (Predecrement / Postdecrement)
NIIT
OPERATORS IN C(contd.)
Example counter+ + ; Note : counter+ + ; has the same effect as + + counter; However : total = sum+ + ; /* make total equal to sum */ /* and then increment sum */
NIIT SEM Q/CPR/CR/SESSION 3/22/VER06/95
OPERATORS IN C(contd.)
does not have the same effect as : total = + + sum; /* increment sum and then */ /* make total equal to sum */
NIIT
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
NIIT
Printing The printf () function can be used printf (\n%d, s_p_array [s_counter] [p_counter]);
It is best to use the for statement during the input and the printing process
NIIT
NIIT
SPL SESSION(contd.)
The first index can be left out for a more flexible declaration
NIIT
CLASSROOM EXERCISE
#1 An interesting word puzzle is the acrostic. An extremely innovative acrostic is: ROTAS OPERA TENET AREPO SATRO
NIIT
CLASSROOM EXERCISE(contd.)
Which in Lating meansArepo the sower hold the wheels with force. Observe that the acrostic is the same whether read horizontally downward from left to right, horizontally upwards from right to left, vertically downwards from left to right or vertically upwards from right to left.
Write a C program to read a 5x5 acrostic and check whether it reads the same in these directions.
NIIT
}
NIIT
NIIT
NIIT
CLASSROOM EXERCISE
#2 Four cities are connected by routes as shown below :
B A
C
D
The arrows between A and B indicate that there are flights from both A to B and B to A. Note that there is no direct flight between A and C, or B and D.
NIIT SEM Q/CPR/CR/SESSION 3/36/VER06/95
CLASSROOM EXERCISE
Write a program to take in the origin and destination of the flights along with their departure and arrival times. (For simplicity, assume that these times are rounded to hours and also that the arrival time is always greater than the departure time). Some sample data is provided below :
NIIT
CLASSROOM EXERCISE
From To A B C D A D C B B C D A D C B A
Departure Time 10:00 20:00 11:00 16:00 3:00 20:00 8:00 12:00
Arrival Time 13:00 22:00 14:00 20:00 7:00 23:00 10:00 15:00
After entering the data, modify the program so that queries for durations of direct flights between any two cities can be made through the program.
NIIT SEM Q/CPR/CR/SESSION 3/38/VER06/95
NIIT
NIIT
NIIT
do {
NIIT
do
{
NIIT
NIIT
}
} while ( ( ch < FIRST_PLACE) | | (ch > LAST_PLACE) );
NIIT SEM Q/CPR/CR/SESSION 3/45/VER06/95
/*Take in passengers destination */ do { printf (\nYour trip is TO : ); ch = getchar (); fflush ( stdin) ;
if((ch>=FIRST_PLACE_S)&&(ch<=LAST_PLACE_S))
{
ch-=UPPER_LOWER_DIFF;
}while((ch<FIRST_PLACE)
|| (ch>LAST_PLACE) ||(ch==from_place));
SEM Q/CPR/CR/SESSION 3/46/VER06/95
NIIT
NIIT
printf(\n);
} /* end of main() */
NIIT