sess-3
TRANSCRIPT
-
8/2/2019 Sess-3
1/48
NIIT SEM Q/CPR/CR/SESSION 3/1/VER06/95
REVIEW QUIZ
#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 } }; }
-
8/2/2019 Sess-3
2/48
NIIT SEM Q/CPR/CR/SESSION 3/2/VER06/95
REVIEW QUIZ (contd.)
c. main ()
{
int s_p_array [2] [2];
}
# 2 Each element of a two-dimensional integer arrayhas to be initialized to 0. Which construct isuseful in initialising it ?
-
8/2/2019 Sess-3
3/48
NIIT SEM Q/CPR/CR/SESSION 3/3/VER06/95
REVIEW QUIZ (contd.)
#3 Some portions of the statement used to input thevalue of an element of a two-dimensional integerarray called s_p_array are missing. Fill in these
portions.
__________ ( ____, ___________ [2] [1]);
# 4 Consider the following # defines in a program :
# define SB 8# define PR 5
If the number of subjects (given by SB) were tochange to 6, what will the above statements
change to ?_____________
_____________
-
8/2/2019 Sess-3
4/48
NIIT SEM Q/CPR/CR/SESSION 3/4/VER06/95
REVIEW QUIZ (contd.)
# 5 Is the following declaration-cum-initializationvalid ?
Char limerick [ ] [ ] = {
I sat beside the duchess at tea,
It was just as I feared it would be,Her rumblings abdominal,
Where truly phenomenal,
And everyone thought it was me};
# 6 To print the second string in the above set, thestatements :
-
8/2/2019 Sess-3
5/48
NIIT SEM Q/CPR/CR/SESSION 3/5/VER06/95
REVIEW QUIZ (contd.)
# 7 Will the following statements suffice to convert acharacter to its upper-case equivalent and print itout?
ch=-32; /* ch is of type char */
printf(%c,ch);
# 8 In which of the following assignments is there apossibility 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;
-
8/2/2019 Sess-3
6/48
NIIT SEM Q/CPR/CR/SESSION 3/6/VER06/95
REVIEW QUIZ (contd.)
# 9 Write a statement using the ternary operator todivide two integers a and b and assign the resultto an integer variable quotient, ensuring
that division by zero does not take place.
# 10 State whether true or false.The statement :
total = total + 1;can be written as either :
total + = 1; or
total + + ;
-
8/2/2019 Sess-3
7/48
NIIT SEM Q/CPR/CR/SESSION 3/7/VER06/95
REVIEW QUIZ (contd.)
# 11 State whether true or false
In the snippet below :
if (y = 0)
{
statement x;
}
the statement x will always get executed.
-
8/2/2019 Sess-3
8/48
NIIT SEM Q/CPR/CR/SESSION 3/8/VER06/95
Solution To Review Quiz
# 1 a. Validb. Invalid, Initialization cannot be done at
the time of declaration within main ()
c. Valid
# 2 The for construct
# 3 scanf (%d, &s_p_array [2] [1]);
# 4 # define SB 6
# define PR 5
# 5 No. The second index (36 = length of second(longest) string + 1) has to be specified.
-
8/2/2019 Sess-3
9/48
NIIT SEM Q/CPR/CR/SESSION 3/9/VER06/95
Solution To Review Quiz
# 6 printf (\n%s, limerick [1]); /* 1, NOT 2 */
# 7 No. one possible solution is :
ch = ( ( ch > = a) && (ch < = z) ) ? (ch - 32) :ch;printf (%c, ch);
# 8 a. Loss of data. Int set equal to a double.
b. No loss of data. Double on left-hand side.
c. Loss of data. All variables are convertedto double and the result (a double) is
truncated to a float.
-
8/2/2019 Sess-3
10/48
NIIT SEM Q/CPR/CR/SESSION 3/10/VER06/95
Solution To Review Quiz
# 9 The statement is :
quotient = ( b ) ? ( a / b ) : LARGE_NO;
/* LARGE_NO has been # defined earlier */
# 10 True. Obviously, the second option :
total+ + ;
is more concise and thus commonly used.
# 11 False. Note that the if statement does not checkfor y equal to 0 but always sets y to 0. Thus the if
condition :(y = 0)
-
8/2/2019 Sess-3
11/48
NIIT SEM Q/CPR/CR/SESSION 3/11/VER06/95
Solution To Review Quiz
is always false and statement x will never getexecuted. If the assignment to y was a non-zeronumber, the condition would be true and
statement x would get executed.
-
8/2/2019 Sess-3
12/48
NIIT SEM Q/CPR/CR/SESSION 3/12/VER06/95
SPL SESSIONObjectivesAt 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
-
8/2/2019 Sess-3
13/48
NIIT SEM Q/CPR/CR/SESSION 3/13/VER06/95
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
-
8/2/2019 Sess-3
14/48
NIIT SEM Q/CPR/CR/SESSION 3/14/VER06/95
SPL SESSION(contd.)
Objectives (contd.) Use two-dimensional character arrays in C programs
in terms of :
Declaration
Initialisation
Printing
-
8/2/2019 Sess-3
15/48
NIIT SEM Q/CPR/CR/SESSION 3/15/VER06/95
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 theremainder in integer division
y = x % 5 /* x and y are both ints */
If x = 7, then y = 2
-
8/2/2019 Sess-3
16/48
NIIT SEM Q/CPR/CR/SESSION 3/16/VER06/95
OPERATORS IN C(contd.)
Character Arithmetic Character arithmetic is possible in C
char ch;
ch = S; /* ch set to S */
ch = ch + 32; /* ch now equal to sch= ch - 32; /* ch back to S */
-
8/2/2019 Sess-3
17/48
NIIT SEM Q/CPR/CR/SESSION 3/17/VER06/95
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 dueto truncation f1 = 31 + i1; /* d1 a double, f1 a float and
i1 an int */
-
8/2/2019 Sess-3
18/48
NIIT SEM Q/CPR/CR/SESSION 3/18/VER06/95
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;
-
8/2/2019 Sess-3
19/48
NIIT SEM Q/CPR/CR/SESSION 3/19/VER06/95
OPERATORS IN C(contd.)
Ternary OperatorSyntax
test-expression ? T-expression : F-expression
Short (and sweet) substitute for the verboseif.else construct
Examplequotient = ( b ) ? ( a / b ) : LARGE_ NO;
-
8/2/2019 Sess-3
20/48
NIIT SEM Q/CPR/CR/SESSION 3/20/VER06/95
OPERATORS IN C(contd.)
Compound Assignment Operators+ =, -=, *=, /= and %=
Used for writing shorter code without obscuring the
meaning
Examplesum + = this_ones_value;
is equivalent to :
sum = sum + this_ones_value;
-
8/2/2019 Sess-3
21/48
NIIT SEM Q/CPR/CR/SESSION 3/21/VER06/95
OPERATORS IN C(contd.)
Increment/Decrement Operators+ + (Preincrement / Postincrement)
- - (Predecrement / Postdecrement)
For the commonly occurring situation on incrementing/ decrementing a variable by one
-
8/2/2019 Sess-3
22/48
NIIT SEM Q/CPR/CR/SESSION 3/22/VER06/95
OPERATORS IN C(contd.)
Examplecounter+ + ;
- Note :
counter+ + ;
has the same effect as
+ + counter;
- However :
total = sum+ + ;
/* make total equal to sum */
/* and then increment sum */
-
8/2/2019 Sess-3
23/48
NIIT SEM Q/CPR/CR/SESSION 3/23/VER06/95
OPERATORS IN C(contd.)
does not have the same effect as :
total = + + sum;
/* increment sum and then */
/* make total equal to sum */
TWO DIMENSIONAL INTEGER
-
8/2/2019 Sess-3
24/48
NIIT SEM Q/CPR/CR/SESSION 3/24/VER06/95
TWO-DIMENSIONAL INTEGER
ARRAYSDeclaration
int s_p_array [4] [4];
Declaration-cum-initialisationint s_p_array [4] [4] = {
{ 0, 0, 0, 0 },{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
TWO-DIMENSIONAL INTEGER
-
8/2/2019 Sess-3
25/48
NIIT SEM Q/CPR/CR/SESSION 3/25/VER06/95
TWO-DIMENSIONAL INTEGER
ARRAYS(contd.)
This sort of declaration-cum-initialization can be done
only outside main ()
A better way to initialize a two-dimensional array is to
use the for statement
Examplefor (s_counter = 0; 2_counter < 4 ;
s_counter+ + )
{
for ( p_counter = 0; p_counter < 4 ;
p_counter+ + ){s_p_array [s_counter] [p_counter] = 0; }
}
TWO-DIMENSIONAL INTEGER
-
8/2/2019 Sess-3
26/48
NIIT SEM Q/CPR/CR/SESSION 3/26/VER06/95
TWO-DIMENSIONAL INTEGER
ARRAYS(contd.)
Input The scanf () function is used
scanf (%d, &s_p_array [s_counter] [p_counter]);
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 andthe printing process
THE PREPROCESSOR PHASE - #
-
8/2/2019 Sess-3
27/48
NIIT SEM Q/CPR/CR/SESSION 3/27/VER06/95
THE PREPROCESSOR PHASE - #
define
# define helps in writing more flexible programs# define SB 5
Before compilation all occurrences of SB will be
replaced by its value If the number of subjects ( SB ) were to change to
8, making the change in one place :
# define SB 8
will reflect the change across theprogram
TWO-DIMENSIONAL
-
8/2/2019 Sess-3
28/48
NIIT SEM Q/CPR/CR/SESSION 3/28/VER06/95
TWO DIMENSIONAL
CHARACTER ARRAYS
Declarationchar err_msg [5] [29];
Declaration-cum-Initialisation This sort of declaration can only be made outside
main()
char err_msg [5] [29] = {
Alls well,File not found,
No read permission for file,
Insufficient memory,No write permission for file,
};
-
8/2/2019 Sess-3
29/48
NIIT SEM Q/CPR/CR/SESSION 3/29/VER06/95
SPL SESSION(contd.)
The first index can be left out for a moreflexible declaration
The second index, however, has to bespecified
The second index must be at least one more
than the longest string
PrintingThe fourth error message can be printed using:
printf (\n%s,err_msg [3] );
-
8/2/2019 Sess-3
30/48
NIIT SEM Q/CPR/CR/SESSION 3/30/VER06/95
CLASSROOM EXERCISE
#1 An interesting word puzzle is the acrostic. Anextremely innovative acrostic is:
ROTAS
OPERA
TENET
AREPO
SATRO
-
8/2/2019 Sess-3
31/48
NIIT SEM Q/CPR/CR/SESSION 3/31/VER06/95
CLASSROOM EXERCISE(contd.)
Which in Lating meansArepo the sower hold
the wheels with force.
Observe that the acrostic is the samewhether read horizontally downward from leftto right, horizontally upwards from right to left,vertically downwards from left to right orvertically upwards from right to left.
Write a C program to read a 5x5 acrostic andcheck whether it reads the same in these
directions.
SOLUTION TO CLASSROOM
-
8/2/2019 Sess-3
32/48
NIIT SEM Q/CPR/CR/SESSION 3/32/VER06/95
EXERCISE
#include
char string [5] [6] ;
main()
{
int i, j, k,l;
char flag1=y, flag2=y;
/* Accept 5 string */
for (i=0 ; i
-
8/2/2019 Sess-3
33/48
NIIT SEM Q/CPR/CR/SESSION 3/33/VER06/95
EXERCISE(contd.)
scanf(%s, string[i]) ;
fflush(stdin) ;}
/*Elements are checked horizontally here*/for (i=0, j=0, k=4, I=4 ; i0 ; i++, k--){
for ( j=0, I=4 ; j0 ; j++, l--){
if (string[i][j] ==string[k][l]continue;
else{flag1=n;i=5;break;}
}}
SOLUTION TO CLASSROOM
-
8/2/2019 Sess-3
34/48
NIIT SEM Q/CPR/CR/SESSION 3/34/VER06/95
EXERCISE(contd.)
/*Elements are checked diagonally here */for (i=0, j=0,l=0,k=0; i
-
8/2/2019 Sess-3
35/48
NIIT SEM Q/CPR/CR/SESSION 3/35/VER06/95
EXERCISE(contd.)
if (flag1==n ll flag2==n){
printf (Not an acrostic\n);}
else{printf(Acrostic\n);
}}
CLASSROOM EXERCISE
-
8/2/2019 Sess-3
36/48
NIIT SEM Q/CPR/CR/SESSION 3/36/VER06/95
CLASSROOM EXERCISE
#2 Four cities are connected by routes asshown below :
The arrows between A and B indicate thatthere are flights from both A to B and B to A.Note that there is no direct flight between A andC, or B and D.
B
D
A
C
CLASSROOM EXERCISE
-
8/2/2019 Sess-3
37/48
NIIT SEM Q/CPR/CR/SESSION 3/37/VER06/95
CLASSROOM EXERCISE
Write a program to take in the origin anddestination of the flights along with theirdeparture and arrival times. (For simplicity,
assume that these times are rounded tohours and also that the arrival time is alwaysgreater than the departure time). Somesample data is provided below :
CLASSROOM EXERCISE
-
8/2/2019 Sess-3
38/48
NIIT SEM Q/CPR/CR/SESSION 3/38/VER06/95
CLASSROOM EXERCISE
From To Departure Time Arrival Time
A B 10:00 13:00B C 20:00 22:00C D 11:00 14:00D A 16:00 20:00A D 3:00 7:00D C 20:00 23:00
C B 8:00 10:00B A 12:00 15:00
After entering the data, modify the program sothat queries for durations of direct flightsbetween any two cities can be made throughthe program.
SOLUTION TO CLASSROOM
-
8/2/2019 Sess-3
39/48
NIIT SEM Q/CPR/CR/SESSION 3/39/VER06/95
EXERCISE
# 2# include
#define NO_OF_ROUTES 8#define UPPER_LOWER_DIFF 32
/*difference between ASCII*//*upper and lower case */
#define FIRST_PLACE A#define FIRST_PLACE_S
(FIRST_PLACE+ UPPER_LOWER_DIFF)
#define LAST_PLACE D#define LAST_PLACE_S
(LAST_PLACE + UPPER_LOWER_DIFF)
SOLUTION TO CLASSROOM
-
8/2/2019 Sess-3
40/48
NIIT SEM Q/CPR/CR/SESSION 3/40/VER06/95
EXERCISE(contd.)
main (){
char ch, from_place, to_place, origin[NO_OF_ROUTES],destin [NO_OF_ROUTES];
int counter, time, travel_time, o_d_diff,abs_o_d_diff, dep_time [NO_OF_ROUTES],arr_time [NO_OF_ROUTES];
for (counter= 0; counter < NO_OF_ROUTES;counter + + ){
do { /* Take in a valid origin */
printf (\nPlease input the ORIGIN : ) ;
SOLUTION TO CLASSROOM
EXERCISE( td )
-
8/2/2019 Sess-3
41/48
NIIT SEM Q/CPR/CR/SESSION 3/41/VER06/95
EXERCISE(contd.)
ch = getchar () ;fflush (stdin);
if ( ( ch> = FIRST_PLACE_S) & & (ch LAST_PLACE) );
SOLUTION TO CLASSROOM
EXERCISE( td )
-
8/2/2019 Sess-3
42/48
NIIT SEM Q/CPR/CR/SESSION 3/42/VER06/95
EXERCISE(contd.)
O.:gin [counter] = ch;printf (/n) ;
do /*Take in a valid destination */{
printf (\nPlease input the DESTINATION : ) ;
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 = = origin
[counter] ) );
SOLUTION TO CLASSROOM
EXERCISE( td )
-
8/2/2019 Sess-3
43/48
NIIT SEM Q/CPR/CR/SESSION 3/43/VER06/95
EXERCISE(contd.)
destin [counter] = ch;printf (\n);
do /* Take in a valid departure time */
{
printf (\n Please input the DEPARTURE TIME : );
scanf(%d, & time) ;fflush (stdin) ;
} while ( ( time = 24) ) ;
dep_time [counter} = time;printf (\n) ;
SOLUTION TO CLASSROOM
EXERCISE( td )
-
8/2/2019 Sess-3
44/48
NIIT SEM Q/CPR/CR/SESSION 3/44/VER06/95
EXERCISE(contd.)
do /* Take in a valid arrival time */{
printf (\nPlease input the ARRIVAL TIME: );
scanf (%d, & time);fflush (stdin);
} while ( ( time < = deo_time [counter] ) | |
(time > = 24 ) );arr_time [counter] = time ;printf (\n);
} /* end of input */
SOLUTION TO CLASSROOM
EXERCISE(contd )
-
8/2/2019 Sess-3
45/48
NIIT SEM Q/CPR/CR/SESSION 3/45/VER06/95
EXERCISE(contd.)
do{
printf (\nYour trip is FROM : );
ch = getchar ();fflush (stdin);
if ( ( ch> = FIRST_PLACE_S) & & (ch LAST_PLACE) );
SOLUTION TO CLASSROOM
EXERCISE(contd )
-
8/2/2019 Sess-3
46/48
NIIT SEM Q/CPR/CR/SESSION 3/46/VER06/95
EXERCISE(contd.)
from_place = ch;printf (\n) ;
/*Take in passengers destination */do
{printf (\nYour trip is TO : );
ch = getchar ();fflush ( stdin) ;
if((ch>=FIRST_PLACE_S)&&(ch
-
8/2/2019 Sess-3
47/48
NIIT SEM Q/CPR/CR/SESSION 3/47/VER06/95
EXERCISE(contd.)
to_place=ch;printf(\n);
/* Determining and printing the duration of the flight */
o_d_diff=from_place-to_place;
abs_o_d_diff=(o_d_diff>0)?(o_d_diff):(-o_d_diff);
if(abs_o_d_diff==2)
{
printf(\n Sorry: No flights between the twoplaces);
}
SOLUTION TO CLASSROOM
EXERCISE(contd )
-
8/2/2019 Sess-3
48/48
NIIT SEM Q/CPR/CR/SESSION 3/48/VER06/95
EXERCISE(contd.)
else{
for(counter=0;(origin[counter]!=from_place)||(destin[counter]!=to_place); counter++)
travel_time=arr_time[counter]-dep_time[counter];
printf(\n Travel time between %c and %c is %dhours, from_place, to_place, travel_time);
}
printf(\n);
} /* end of main() */