cs11001 cs11002 programming and data structures pds
play

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: - PowerPoint PPT Presentation

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Teacher: Sourangshu Bha@acharya sourangshu@gmail.com h@p://cse.iitkgp.ac.in/~sourangshu/ Department of Computer Science and Engineering Indian InsJtute of Technology


  1. CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Teacher: Sourangshu Bha@acharya sourangshu@gmail.com h@p://cse.iitkgp.ac.in/~sourangshu/ Department of Computer Science and Engineering Indian InsJtute of Technology Kharagpur

  2. RelaJonal Operators • Used to compare two quan11es. < is less than > is greater than <= is less than or equal to >= is greater than or equal to == is equal to != is not equal to

  3. RelaJonal Operators int x = 20; int y = 3; float a=20.3; /* 20 > 3 è True */ if ( x > y ) printf (“%d is larger\n”, x); if ( x + x > y * 6 ) /* 20+20 > 3*6 è (20+20)>(3*6) è True */ printf(“Double of %d is larger than 6 times %d”,x,y); if ( x > a ) /* Type cast??? */ printf(“%d is larger than %f”,x, a); else printf(“%d is smaller than %f”,x, a);

  4. Logical Operators • Unary and Binary Operators ! è Logical NOT, logical nega1on (True if the operand is False.) && è Logical AND (True if both the operands are True.) || è Logical OR (True if either one of the operands is True.) int x = 20; int y=3;float a=20.3; X !X FALSE TRUE if( (x>y) && (x>a) ) /* FALSE */ TRUE FALSE printf(“X is largest.”); if( (x>y) || (x>a) ) /* TRUE */ X Y X && Y X || Y printf(“X is not smallest.”); FALSE FALSE FALSE FALSE if( !(x==y) ) /* TRUE */ FALSE TRUE FALSE TRUE printf(“X is not same as Y.”); TRUE FALSE FALSE TRUE if( x!=y) /* TRUE */ printf(“X is not same as Y.”); TRUE TRUE TRUE TRUE

  5. Control Statements Control Statements Branching Looping Statement takes more than one Some set of statements are branches based upon a being executed iteraJvely un1l condiJon test comprising of a condiJon test comprising of rela1onal and/or logical (may be rela1onal and/or logical (may arithme1c) operators. be arithme1c) operators are not being sa1sfied.

  6. CondiJons • Using rela1onal operators. – Four rela1on operators: <, <=, >, >= – Two equality opera1ons: ==, != • Using logical operators / connec1ves. – Two logical connec1ves: &&, | | – Unary nega1on operator: !

  7. CondiJon Tests if(count <= 100) /* Relational */ if((math+phys+chem)/3 >= 60) /* Arithmetic, Relational */ if((sex==‘M’) && (age>=21)) /* Relational, Logical */ if((marks>=80) && (marks<90) ) /* Relational, Logical */ if((balance>5000) | | (no_of_trans>25)) /* Relational, Logical */ if(! (grade==‘A’)) /* Relational, Logical */ CondiJon EvaluaJon True False (Non-zero, preferably 1) (Zero)

  8. Operator confusion Equality (==) and Assignment (=) Operators What is expected in condi1on? • – Nonzero values are true, zero values are false – Any expression that produces a value can be used in control structures int age=20; if ( age > 18 ) /* Logical Operator; Evaluated as TRUE */ printf( "You are not a minor!\n" ); if ( age >= 18 ) /* Logical Operator; Evaluated as TRUE */ printf( "You are not a minor!\n" ); if ( age == 20 ) /* Logical Operator; Evaluated as TRUE */ printf( "You are not a minor!\n" ); if ( age = 18 ) /* Arithmetic Operator; Evaluated as TRUE */ printf( "You are not a minor!\n" ); if ( age = 17 ) /* Arithmetic Operator; Evaluated as TRUE */ printf( "You are a minor!\n" );

  9. Operator confusion Equality (==) and Assignment (=) Operators Be@er is avoid. int age=20; if ( age > 18 ) /* Logical Operator; Evaluated as TRUE */ printf( "You are not a minor!\n" ); if ( age >= 18 ) /* Logical Operator; Evaluated as TRUE */ printf( "You are not a minor!\n" ); if ( age == 20 ) /* Logical Operator; Evaluated as TRUE */ printf( "You are not a minor!\n" ); if ( age = 18 ) /* Arithmetic Operator; Evaluated as TRUE */ printf( "You are not a minor!\n" ); Value of age will be 18 if ( age = 17 ) /* Arithmetic Operator; Evaluated as TRUE */ printf( "You are a minor!\n" ); Value of age will be 17 There will be no These statements are not syntax error. logically correct!!!

  10. Operator confusion Equality (==) and Assignment (=) Operators #include <stdio.h> int main() { int x,y; scanf(“%d”,&x); y=x%2; /* y will be 1 or zero based on value entered and stored as x */ if(y=1) { /* y will be assigned with 1, condition will be evaluated as TRUE */ printf(“Entered number is odd.”); } else { printf(“Entered number is even.”); } return 0; }

  11. Unary Operator • Increment (++) Opera1on means i = i + 1; – Prefix opera1on (++i) or Pos`ix opera1on (i++) • Decrement (--) Opera1on means i = i - 1; – Prefix opera1on (--i) or Pos`ix opera1on (i--) • Precedence – Prefix opera1on : First increment / decrement and then used in evalua1on – Pos`ix opera1on : Increment / decrement opera1on aaer being used in evalua1on • Example int t, m=1; int t,m=1; t=++m; t=m++; m=2 m=2 t=1 t=2

  12. More Examples on Unary Operator Ini1al values :: a = 10; b = 20 x = 50 + ++a; a = 11, x = 61 Ini1al values :: a = 10; b = 20; x = 50 + a++; x = 60, a = 11 Ini1al values :: a = 10; b = 20; x = a++ + --b; b = 19, x = 29, a = 11 Ini1al values :: a = 10; b = 20; x = a++ – ++a; Undefined value (implementa1on dependent)

  13. Shortcuts in Assignment Statements • A+=C à A=A+C • A-=B à A=A-B • A*=D à A=A*D • A/=E à A=A/E

  14. Input scanf (“control string”,arg1,arg2, …, argn); • Performs input from the standard input device, which is the keyboard by default. • It requires a control string refers to a string typically containing data types of the arguments to be read in. • And the (arguments) address or pointers of the list of variables into which the value received from the input device will be stored. • The address of the variables in memory are required to men1on (& before the variable name) to store the data. • The control string consists of individual groups of characters (one character group for each input data item). Typically, a ‘%’ sign, followed by a conversion character. int size,a,b; float length; scanf ("%d", &size) ; scanf ("%f", &length) ; scanf (“%d %d”, &a, &b);

  15. Input Conversion Character Data Item meaning c Single charcater d Decimal integer e Floa1ng point value f Floa1ng point value g Floa1ng point value h Short int i Decimal/hexadecimal/octal integer o Octal integer s String u Unsigned decimal integer X Hexadecimal integer We can also specify the maximum field-width of a data item, by specifying a number indica1ng the field width before the conversion character. Example: scanf (“%3d %5d”, &a, &b);

  16. Output printf (“control string”,arg1,arg2, …, argn); – Performs output to the standard output device (typically defined to be the screen). – Control string refers to a string containing formaqng informa1on and data types of the arguments to be output; – The arguments arg1, arg2, … represent the individual output data items. – The conversion characters are the same as in scanf. int size,a,b; float length; scanf ("%d", &size) ; printf(“%d”,size); scanf ("%f", &length) ; printf(“%f”,length); scanf (“%d %d”, &a, &b); printf(“%d %d”,a,b);

  17. Forma@ed Output float a=3.0, b=7.0; printf(“%f %f %f %f”,a,b,a+b,sqrt(a+b)); 3.000000 7.000000 10.000000 3.162278 Will be wrisen Total Space exactly. printf(“%4.2f %5.1f\na+b=%3.2f\tSquare Root=%-6.3f”,a,b,a+b,sqrt(a+b)); 3.00 7.0 a+b=10.00 Square Root=3.162 Lea Align Aaer decimal Tab place For integer, character and string, no decimal point.

  18. Character I/O char ch1; scanf(“%c”,&ch1); /* Reads a character */ printf(“%c”,ch1); /* Prints a character */ ch1=getchar(); /* Reads a character */ putchar(ch1); /* Prints a character */ char name[20]; scanf(“%s”,name); /* Reads a string */ printf(“%s”,name); /* Prints a string */ gets(name); /* Reads a string */ puts(name); /* Prints a string */ Help for any command: $ man gets

  19. Problem solving • Step 1: – Clearly specify the problem to be solved. • Step 2: – Draw flowchart or write algorithm. • Step 3: – Convert flowchart (algorithm) into program code. • Step 4: – Compile the program into executable file. • Step 5: – For any compila1on error, go back to step 3 for debugging. • Step 6: – Execute the executable file (program).

  20. Flowchart: basic symbols Predefined Process Process Terminal Prepara1on Decision I/O Data Flow Line Connector

  21. Example 2: find the largest among three numbers START READ X, Y, Z IS YES NO X > Y? LARGE = X LARGE = Y YES NO IS LARGE > Z? OUTPUT Z OUTPUT LARGE STOP STOP

  22. Branching: if Statement • General syntax: if (condiJon) { …….. } True • Test the condi1on, and follow appropriate path. condi1on Statement • Contains an expression that can be TRUE or FALSE. False • Single-entry / single-exit structure. • If there is a single statement in the block, the braces can be omised. if (basicPay<18000) if (basicPay<18000) { prin`(“Bonus Applicable”); int bonus; bonus=basicPay*0.30; prin`(“Bonus is %d”,bonus); }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend