Sei sulla pagina 1di 48

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 } }; }
NIIT SEM Q/CPR/CR/SESSION 3/1/VER06/95

REVIEW QUIZ (contd.)


c. main () {

int s_p_array [2] [2];


} #2 Each element of a two-dimensional integer array has to be initialized to 0. Which construct is useful in initialising it ?

NIIT

SEM Q/CPR/CR/SESSION 3/2/VER06/95

REVIEW QUIZ (contd.)


#3 Some portions of the statement used to input the value of an element of a two-dimensional integer array 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 to change to 6, what will the above statements change to ? _____________ _____________
SEM Q/CPR/CR/SESSION 3/3/VER06/95

NIIT

REVIEW QUIZ (contd.)


#5 Is the following declaration-cum-initialization valid ? 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}; To print the second string in the above set, the statements :

#6

NIIT

SEM Q/CPR/CR/SESSION 3/4/VER06/95

REVIEW QUIZ (contd.)


#7 Will the following statements suffice to convert a character to its upper-case equivalent and print it out? ch=-32; /* ch is of type char */ printf(%c,ch);

#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

SEM Q/CPR/CR/SESSION 3/5/VER06/95

REVIEW QUIZ (contd.)


#9 Write a statement using the ternary operator to divide two integers a and b and assign the result to an integer variable quotient, ensuring that division by zero does not take place. State whether true or false. The statement : total = total + 1; can be written as either : total + = 1; or total + + ;

# 10

NIIT

SEM Q/CPR/CR/SESSION 3/6/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.

NIIT

SEM Q/CPR/CR/SESSION 3/7/VER06/95

Solution To Review Quiz


#1 a. b. c. #2 #3 #4 Valid Invalid, Initialization cannot be done at the time of declaration within main () Valid

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

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. 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

SEM Q/CPR/CR/SESSION 3/9/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 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

Solution To Review Quiz


is always false and statement x will never get executed. If the assignment to y was a non-zero number, the condition would be true and statement x would get executed.

NIIT

SEM Q/CPR/CR/SESSION 3/11/VER06/95

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

SEM Q/CPR/CR/SESSION 3/12/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

NIIT

SEM Q/CPR/CR/SESSION 3/13/VER06/95

SPL SESSION(contd.)
Objectives (contd.) Use two-dimensional character arrays in C programs in terms of : Declaration Initialisation Printing

NIIT

SEM Q/CPR/CR/SESSION 3/14/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 the remainder in integer division y=x%5 If x = 7, then y = 2 /* x and y are both ints */

NIIT

SEM Q/CPR/CR/SESSION 3/15/VER06/95

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

SEM Q/CPR/CR/SESSION 3/16/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 due to truncation f1 = 31 + i1; /* d1 a double, f1 a float and i1 an int */

NIIT

SEM Q/CPR/CR/SESSION 3/17/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;

NIIT

SEM Q/CPR/CR/SESSION 3/18/VER06/95

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

SEM Q/CPR/CR/SESSION 3/19/VER06/95

OPERATORS IN C(contd.)
Compound Assignment Operators

+ =, -=, *=, /= and %=


Used for writing shorter code without obscuring the meaning

Example
sum + = this_ones_value; is equivalent to :

sum = sum + this_ones_value;

NIIT

SEM Q/CPR/CR/SESSION 3/20/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

NIIT

SEM Q/CPR/CR/SESSION 3/21/VER06/95

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

SEM Q/CPR/CR/SESSION 3/23/VER06/95

TWO-DIMENSIONAL INTEGER ARRAYS


Declaration int s_p_array [4] [4]; Declaration-cum-initialisation

int s_p_array [4] [4] = {


{ 0, 0, 0, 0 }, { 0, 0, 0, 0 },

{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };

NIIT

SEM Q/CPR/CR/SESSION 3/24/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 Example for (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; }
NIIT

SEM Q/CPR/CR/SESSION 3/25/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 and the printing process

NIIT

SEM Q/CPR/CR/SESSION 3/26/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 the program

NIIT

SEM Q/CPR/CR/SESSION 3/27/VER06/95

TWO-DIMENSIONAL CHARACTER ARRAYS


Declaration char 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, };
NIIT SEM Q/CPR/CR/SESSION 3/28/VER06/95

SPL SESSION(contd.)
The first index can be left out for a more flexible declaration

The second index, however, has to be specified


The second index must be at least one more than the longest string Printing The fourth error message can be printed using:

printf (\n%s,err_msg [3] );

NIIT

SEM Q/CPR/CR/SESSION 3/29/VER06/95

CLASSROOM EXERCISE
#1 An interesting word puzzle is the acrostic. An extremely innovative acrostic is: ROTAS OPERA TENET AREPO SATRO

NIIT

SEM Q/CPR/CR/SESSION 3/30/VER06/95

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

SEM Q/CPR/CR/SESSION 3/31/VER06/95

SOLUTION TO CLASSROOM EXERCISE


#include <stdio.h> char string [5] [6] ; main() { int i, j, k,l; char flag1=y, flag2=y; /* Accept 5 string */ for (i=0 ; i<5 ; i++) { printf(Enter string %d : ,i+1);
NIIT SEM Q/CPR/CR/SESSION 3/32/VER06/95

SOLUTION TO CLASSROOM EXERCISE(contd.)


scanf(%s, string[i]) ; fflush(stdin) ; } /*Elements are checked horizontally here*/ for (i=0, j=0, k=4, I=4 ; i<5 &&k>0 ; i++, k--) { for ( j=0, I=4 ; j<5 &&l>0 ; j++, l--) { if (string[i][j] ==string[k][l] continue; else { flag1=n; i=5; break; } }

}
NIIT

SEM Q/CPR/CR/SESSION 3/33/VER06/95

SOLUTION TO CLASSROOM EXERCISE(contd.)


/*Elements are checked diagonally here */ for (i=0, j=0,l=0,k=0; i<5 ll I<5 ; i++, l++) { for (j=0,k=0; j<5 ll k<5 ; j++, k++) { if (string [i][j] ==string[k][l]) continue; else { flag2=n; i=5; l=5; break; } } }

NIIT

SEM Q/CPR/CR/SESSION 3/34/VER06/95

SOLUTION TO CLASSROOM EXERCISE(contd.)


if (flag1==n ll flag2==n) { printf (Not an acrostic\n); } else { printf(Acrostic\n); } }

NIIT

SEM Q/CPR/CR/SESSION 3/35/VER06/95

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

SEM Q/CPR/CR/SESSION 3/37/VER06/95

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

SOLUTION TO CLASSROOM EXERCISE


#2 # include <stdio.h> #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)

NIIT

SEM Q/CPR/CR/SESSION 3/39/VER06/95

SOLUTION TO CLASSROOM 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 : ) ;

NIIT

SEM Q/CPR/CR/SESSION 3/40/VER06/95

SOLUTION TO CLASSROOM EXERCISE(contd.)


ch = getchar () ; fflush (stdin);

if ( ( ch> = FIRST_PLACE_S) & & (ch < = LAST_PLACE_S) )


{ ch - = UPPER_LOWER_DIFF; } } while ( ( ch < FIRST_PLACE) | | ( ch > LAST_PLACE) );

NIIT

SEM Q/CPR/CR/SESSION 3/41/VER06/95

SOLUTION TO CLASSROOM 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] ) );

NIIT

SEM Q/CPR/CR/SESSION 3/42/VER06/95

SOLUTION TO CLASSROOM 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<0) | | (time> = 24) ) ;

dep_time [counter} = time; printf (\n) ;

NIIT

SEM Q/CPR/CR/SESSION 3/43/VER06/95

SOLUTION TO CLASSROOM 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 */

NIIT

SEM Q/CPR/CR/SESSION 3/44/VER06/95

SOLUTION TO CLASSROOM EXERCISE(contd.)


do {

printf (\nYour trip is FROM : );


ch = getchar (); fflush (stdin); if ( ( ch> = FIRST_PLACE_S) & & (ch < = LAST_PLACE_S) ) { ch - = UPPER_LOWER_DIFF;

}
} while ( ( ch < FIRST_PLACE) | | (ch > LAST_PLACE) );
NIIT SEM Q/CPR/CR/SESSION 3/45/VER06/95

SOLUTION TO CLASSROOM 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<=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

SOLUTION TO CLASSROOM 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 two places); }

NIIT

SEM Q/CPR/CR/SESSION 3/47/VER06/95

SOLUTION TO CLASSROOM 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 %d hours, from_place, to_place, travel_time);


}

printf(\n);
} /* end of main() */

NIIT

SEM Q/CPR/CR/SESSION 3/48/VER06/95

Potrebbero piacerti anche