W 1 L 2 sh 1 C lessons LessonSubjectBook Week 1 lesson 1Objects, names Week 1 lesson 2Statements, layout Week 2 lesson 1Functions, decomposition Week 2.

Slides:



Advertisements
Similar presentations
W 2 L 1 sh 1 Assignments Read the ‘overzicht’ document! OnderwerpV2CCPP1 x y x = student nummer y = nummer.
Advertisements

One Dimensional Arrays
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Computer Science 1620 Loops.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Conditionals, Loops, and Other Statements CS-2301, B-Term Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Chess Merit Badge Chess Basics: Set Up the Board & Basic Rules by Joseph L. Bell © 2011.
W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site:
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
CSC204 – Programming I Lecture 4 August 28, 2002.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
By the end of this session you should be able to...
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Homework / Exam Turn in HW3 today Exam 1 next class –Open Book / Open Notes –Recommended Use of Book / Notes in Exam: Avoids reliance on “rote memorization”
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
W 2 L 1 sh 1 C lessons LessonSubjectBook Week 1 lesson 1Objects, names Week 1 lesson 2Statements, layout Week 2 lesson 1Functions, decomposition Week 2.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Controlling Execution Dong Shao, Nanjing Unviersity.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
1 CS161 Introduction to Computer Science Topic #8.
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
COMP x1 Computing 2 C Outside the Style Guide, Linked Lists and Function Pointers 1.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
COMP Loop Statements Yi Hong May 21, 2015.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
Information and Computer Sciences University of Hawaii, Manoa
‘C’ Programming Khalid Jamal.
A bit of C programming Lecture 3 Uli Raich.
Chapter 4 - Program Control
Chapter 2.1 Control Structures (Selection)
Lecture 2: Logical Problems with Choices
Chapter 6 Decision Making and Looping
Chapter 4 - Program Control
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
2.6 The if/else Selection Structure
Chapter 4 - Program Control
CSE 206 Course Review.
Presentation transcript:

W 1 L 2 sh 1 C lessons LessonSubjectBook Week 1 lesson 1Objects, names Week 1 lesson 2Statements, layout Week 2 lesson 1Functions, decomposition Week 2 lesson 2Lists, specification Week 3 lesson 1Memory, testing Week 3 lesson 2Fagan inspection

W 1 L 2 sh 2 How to make code readable n Names n Names must help the reader to understand the code. n Format (layout) n The layout must match the statement structure. n The layout must be consistent. n Comments n Comments must explain what the reader needs to know but can not easily read from the code. n Structure n The code must be structured to be understandable. n Known concepts n Use concepts and techniques that are familiar (datastructures, patterns, real-world analogues).

W 1 L 2 sh 3 C statements n Declaration, definition n Expression n Compound : {... } n Selection : if/else, switch/case/default/break n Iteration : for, while, do/while, (break, continue) n (labeled statement) – verboten!

W 1 L 2 sh 4 Indentation n Purpose : make reading easy (screen or print?). n Consistency is probably more important than any particular style. n Save trees? // Pascal - ANSI if( a > b ) { max = a; } else { max = b; } // Whitesmith if( a > b ) { max = a; n++; } else { max = b; } // K&R - 1TBS if( a > b ){ max = a; } else { max = b; } if( a > b ){ max = a; } else { max = b; } // Pico (compact) if( a > b ){ max = a; } else { max = b; } // Horstman if( a > b ) { max = a; } else { max = b; }

W 1 L 2 sh 5 Selection - if if( a > b ){ max = a; } if( a > b ){ max = a; } else { max = b; } if( a > b ){ max = a; } else if ( b > a ){ max = b; } else { max = a; equals++; } n If, if else, if … else n Always use { } n Write complex conditions on multiple lines if( ( x > 0 ) && ( x < size_x()) && ( y > 0 ) && ( y < size_y()) ){ VRAM_A[ x, y ] = color; }

W 1 L 2 sh 6 Selection - switch switch( c ){ case ’\n’ : position.x = 0; position.y= minimum( position.y + 1, 23 ); break; case ’\v’ : position.x = 0; position.y = 0; break; default : display[ position.x, position.y ] = c; position.x = minimum( position.x + 1, 79 ); break; } n Indent the cases as if there was a { } pair. n Each case ends with a break.

W 1 L 2 sh 7 Iteration – while, do... while int c; while( EOF != ( c = getchar()) ){ putchar( convert_to_uppercase( c )); } n Test before versus test afterwards. n What is wrong with this do.. while loop? int c; do { c = getchar(); putchar( convert_to_uppercase( c )); } while( c != EOF );

W 1 L 2 sh 8 Iteration – for int i, sum, values[ SIZE ];... sum = 0; for( i = 0; i < SIZE; i++ ){ sum += values[ i ]; } n A for loop is just an easy notation for a while loop i = 0; while( i < SIZE ){ sum += values[ i ]; i++; }

W 1 L 2 sh 9 Iteration – break n Break jumps to the statements after the loop (terminates the loop) n Use this: when a loop needs some ‘starting up’ before the condition can be checked. This is an idiom. while( 1 ) { c = getchar(); if( c == EOF ){ break; } putchar( convert_to_uppercase( c )); chars_printed++; }

W 1 L 2 sh 10 Iteration – continue n Continue jumps to the end of the statements in the loop (terminates this iteration) n Use this: seldom. Maybe when a very complex condition determines that the rest of the loop statements must be skipped. while( 1 ) { c = getchar(); if( c == EOF ){ break; } if( ! char_is_letter( c ){ continue; } putchar( convert_to_uppercase( c )); chars_printed++; }

W 1 L 2 sh 11 Condition n A condition is an integer expression. n 0 is interpreted as ‘not true’. n All other values are interpreted as ‘true’. n Functions that return a condition must adhere to this convention. Statements that check a condition must adhere to this convention.

W 1 L 2 sh 12 Condition woes if( strcmp( ”hello”, ”world” )){ printf( ”hello and world are equal” ); } if( TRUE == isupper( ’A’ )){ printf( ”” ); }  Don’t write semi-predicate functions, or make the return type very very clear.  There is no representation for true.

W 1 L 2 sh 13 Reversi (Othello) Black must place a piece with the black side up on the board, in such a position that there exists at least one straight (horizontal, vertical, or diagonal) occupied line between the new piece and another dark piece, with one or more contiguous white pieces between them. After placing the piece, black turns over (flips) all white pieces lying on a straight line between the new piece and any anchoring black pieces.

W 1 L 2 sh 14 Reversi (Othello) The opposing sides in turn place a piece on the board and chance the pieces on the board according to the rules. When one side can not make a move it must pass. When both sides must pass the black and white pieces are counted and the side having the most pieces wins. Tactics include Selecting good places first (borders, especially corners) Maximizing the number of moves I can make Minimizing the number moves my opponent can make Minimizing (!) my number of stones

W 1 L 2 sh 15 Reversi (Othello) enum square { border = 0, empty = 1, white = 2, black = 3 }; typedef int board[ 100 ]; BBBBBBBBBB BB BB BB BwbB BbwB BB BB BB BBBBBBBBBB Starting position Square indexes in a board array

W 1 L 2 sh 16 Playing the game

W 1 L 2 sh 17 reversi.h // initialize board b to the starting position void reversi_board_init( board b ); // print the board b void reversi_board_print( board b ); // copy the board s to the board d void reversi_board_copy( board d, board s ); // convert a string "cn" to the index ( ) of the square. // c must be a..h (or A..H), n must be // return -1 when the string is not valid int reversi_move_from_string( char *s ); // return the number of squares that contain color int reversi_board_count_color( board b, int color );

W 1 L 2 sh 18 reversi.h // try to move a piece 'color' to the square move. // return the number of opponent squares that would be flipped. // return 0 if the move is not allowed (border, occupied) // this function changes the board! char reversi_flipped_board_color_move( board b, int color, int move ); // try to move a piece 'color' to the square move. // return the number of opponent squares that would be flipped. // consider only squares that lie in direction relative to the move. // return 0 if the move is not allowed (border, occupied) // this function changes the board! char reversi_flipped_board_color_move_direction( board b, int color, int move, int direction ); // return the number of valid moves available for color int reversi_board_n_moves( board b, int color );

W 1 L 2 sh 19 main.c – play game void play_game( board b, int color ){ int passes = 0; int count_black, count_white; while( passes < 2 ){ if( play_move( b, color ) ){ passes = 0; } else { passes++; } color = ( color == white ? black : white ); } count_white = reversi_flipped_board_count_color( b, white ); count_black = reversi_flipped_board_count_color( b, black ); printf( "game over!\n" ); printf( " black has %d pieces\n", count_black ); printf( " white has %d pieces\n", count_white ); if( count_white > count_black ){ printf( "white wins\n" ); } else if( count_black > count_white ){ printf( "black wins\n" ); } else { printf( "it is a draw wins\n" ); } int main(int argc, char *argv[]){ board b; reversi_board_init( b ); play_game( b, black ); }

W 1 L 2 sh 20 main.c – play move int play_move( board b, int color ){ char s[132 ]; int move, flipped, allowed; reversi_board_print( b ); for(;;){ allowed = reversi_board_n_moves( b, color ); printf( "%s to move (%d moves allowed): ", color == white ? "white (O)" : "black (X)", allowed ); if( allowed == 0 ){ printf( "\n so you will have to pass\n" ); return 0; } scanf( "%s", s ); move = reversi_move_from_string( s ); if( move == -1 ){ printf( "illegal format, please try again\n" ); continue; } flipped = reversi_flipped_board_color_move( b, color, move ); if( flipped == 0 ){ printf( "illegal move, please try again\n" ); continue; } return 1; }

W 1 L 2 sh 21 Read Book n Chapter 6 n Chapter 7

W 1 L 2 sh 22 Assignment Write the missing code in reversi.c Adhere to style guide! Verify that you can play a game of reversi.

W 1 L 2 sh 23 Assignment Function# Lines in my version Remarks reversi_board_init 18Is provided reversi_board_print 29Lots of lines but basically very simple. Good place to start. reversi_board_copy 6Trivial reversi_move_from_string 13Lots of checks required, but basically simple. Test this one by itself (print the return value) reversi_flipped_board_color_move_direction 29This is the difficult part. reversi_flipped_board_color_move 16Calls reversi_flipped_board_color_move_direction for each direction. Is provided. reversi_board_n_moves 11 Uses reversi_flipped_board_color_move. Trivial once you have that function. Do not forget to copy the board before each call! reversi_board_count_color 9Trivial

W 1 L 2 sh 24 Assignment – hints  If the square is not empty, the move is illigal.  Walk in the indicated direction  Border or empty  return 0  Enemy piece  continue walking  My own piece  stop walking  Now walk back  Enemy piece  flip it  Back at the square  return # flipped enemies // try to move a piece 'color' to the square move. // return the number of opponent squares that would be flipped. // consider only squares that lie in direction relative to the move. // return 0 if the move is not allowed (border, occupied) // this function changes the board! char reversi_flipped_board_color_move_direction( board b, int color, int move, int direction );