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

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Programming in C Chapter 10 Structures and Unions
C Language.
Lectures 10 & 11.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming and Data Structure
Programming Languages and Paradigms The C Programming Language.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures in C.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers in C Rohit Khokher
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
CS Winter 2011 Introduction to the C programming language.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Programming in C Structs and Unions. No Classes in C Because C is not an OOP language, there is no way to combine data and code into a single entity.Because.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
C Course Lecture 4 This lecture we'll talk about: Multi-dimensional arrays. Pointer arithmetic. Pointers to structures. Multi-file programming. What is.
W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site:
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
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.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
CS 261 – Data Structures Introduction to C Programming.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
CSC Programming for Science Lecture 36: Structures.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Aquarium Lab Series Developed by Alyce BradyAlyce Brady of Kalamazoo CollegeKalamazoo College.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Introduction to C Programming CE Lecture 6 Functions, Parameters and Arguments.
Graphics Lab: MyPaint Dan Maselko 1. MyPaint Description  For this assignment you will be implementing a very simple paint program.  You should be able.
C++ Functions A bit of review (things we’ve covered so far)
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Pointer to an Object Can define a pointer to an object:
User-Written Functions
Function: Declaration
Introduction to the C programming language
Functions CIS 40 – Introduction to Programming in Python
Pointers and References
Basic Graphics Drawing Shapes 1.
Pointers, Dynamic Data, and Reference Types
CS149D Elements of Computer Science
SPL – PS3 C++ Classes.
Introduction to Methods and Interfaces
Presentation transcript:

W 2 L 1 sh 1 Assignments Read the ‘overzicht’ document! OnderwerpV2CCPP1 x y x = student nummer y = nummer van de opgave, BijlageDe uitwerking. Als de uitwerking meerdere file beslaat moet de bijlage een.zip file zijn. Geen andere compressie formaten gebruiken! BodyGeen eisen

W 2 L 1 sh 2 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 n The most important way to structure your program n The most important things to name n The most important things to reuse

W 2 L 1 sh 3 C function n Interface: n name n argument list n return type n Interface only  declaration n With body  definition (== implementation) n The name identifies the function: two visible functions can not have the same name (!= C++) n The argument list must match the actual arguments passed in each call (in number and type) int give_me_eight( void ){ return 8; } int give_me_eight( void );

W 2 L 1 sh 4 n Result n Is what is after the executed ‘return’ statement n Return behaves as an assignment to an invisible variable. n Is the result of the function call Parameters and result n Formal parameters n are mentioned in the interface n Used in the body (implementation) int add( int a, int b ){ return a + b; } int x = add( a, 8 ); n Actual parameters n Is what you (the caller) supply when you can a function n Are (sort-of) assigned to the formal parameters

W 2 L 1 sh 5 Parameter passing – by value Basic types are passed ‘by value’ void g( char c, int i, float f, int *p ){ c = 'a'; i = 5; f = 10.0; p = NULL; } int main( void ){ char c = 'x'; int i = 0; float f = 0.0; int *p = &i; printf( "%c %d %f %d \n", c, i, f, (int)p ); g( c, i, f, p ); printf( "%c %d %f %d \n", c, i, f, (int)p ); }

W 2 L 1 sh 6 Parameter passing - pointers To change a basic type: pass a pointer to it void g( char *c, int *i, float *f, int **p ){ *c = 'a'; *i = 5; *f = 10.0; *p = NULL; } int main( void ){ char c = 'x'; int i = 0; float f = 0.0; int *p = &i; printf( "%c %d %f %d \n", c, i, f, (int)p ); g( &c, &i, &f, &p ); printf( "%c %d %f %d \n", c, i, f, (int)p ); }

W 2 L 1 sh 7 Parameter passing - arrays Arrays are invisibly passed ‘by reference’ == ‘by address’ void f( int a[ 5 ] ){ a[ 0 ] = 5; a[ 1 ] = 4; a[ 2 ] = 3; a[ 3 ] = 2; a[ 4 ] = 1; } int main( void ){ int a[ 5 ] = { 1, 2, 3, 4, 5 }; printf( "%d %d %d %d %d \n", a[0], a[1], a[2], a[3], a[4] ); f( a ); printf( "%d %d %d %d %d \n", a[0], a[1], a[2], a[3], a[4] ); }

W 2 L 1 sh 8 Parameter passing – array size Array parameters can either be n Of a specific size n Without a specific size Without a specific size, you will need some other way to find out how large the array is (or you will create the next buffer overrun exploit..) void f( int a[ 5 ] ){ int i; for( i = 0; i < 5; i++ ){... } void f( int a[] ){ int i; for( i = 0; i < ??; i++ ){... }

W 2 L 1 sh 9 Function pointers – pass ‘work’ around #include typedef int (*int_filter)( int ); int add1( int x ){ return x + 1; } int clip10( int x ){ return x > 10 ? 10 : x; } int absolute( int x ){ return x > 0 ? x : -x; } void apply_int_filter( int a[], int size, int_filter f ){ int i; for( i = 0; i < size; i++ ){ a[ i ] = f( a[ i ] ); } void array_print( int a[], int size ){ int i; for( i = 0; i < size; i++ ){ printf( "%4d ", a[ i ] ); } printf( "\n" ); }

W 2 L 1 sh 10 Function pointers – pass ‘work’ around int main( void ){ #define a_size 7 int a[ a_size ] = { -1, -2, 0, 1, 2, 10, 100 }; array_print( a, a_size ); apply_int_filter( a, a_size, add1 ); array_print( a, a_size ); apply_int_filter( a, a_size, clip10 ); array_print( a, a_size ); apply_int_filter( a, a_size, absolute ); array_print( a, a_size ); system("PAUSE"); return 0; }

W 2 L 1 sh 11 A simple NDS paint application #configure board ndsi #configure emulator desmume #include #include "lcd.h" int main( void ){ struct touchPosition position; nintendo_screen lcd; lcd.clear( colors::red ); for(;;){ scanKeys(); int held = keysHeld(); if( held & KEY_TOUCH ){ touchRead( & position ); lcd.pixel_write( position.px, position.py, colors::black ); } swiWaitForVBlank(); } return 0; } mkt.py info libnds graphics lib Lcd lib ‘init’ poll the keys read stylus f = 60 Hz

W 2 L 1 sh 12 Paint within an area #define rx0 10 #define ry0 10 #define rx1 200 #define ry1 180 int position_is_in_rectangle( int x, int y, int x0, int y0, int x1, int y1 ){ return( x > x0 && x y0 && y < y1 ); } int main( void ){ struct touchPosition position; nintendo_screen lcd; lcd.clear( colors::red ); lcd.rectangle_write( rx0, ry0, rx1, ry1 ); for(;;){ scanKeys(); int held = keysHeld(); if( held & KEY_TOUCH ){ touchRead( & position ); if( position_is_in_rectangle( position.px, position.py, rx0, ry0, rx1, ry1 )){ lcd.pixel_write( position.px, position.py, colors::blue ); } swiWaitForVBlank(); } return 0; } rectangle check function show paint only when within

W 2 L 1 sh 13 But we want more The paint area is just one ‘object’ on the screen. We want more, like changing the brush color. Because (re)drawing and the action are separated we must specify the coordinates twice .  Build a list of ‘active objects’.

W 2 L 1 sh 14 Paint - a list of objects typedef struct area_struct area; typedef void ( *action )( int x, int y, area *a ); struct area_struct { area *next; int x0, y0, x1, y1; action f; }; area *first = NULL; void area_add( int x0, int y0, int x1, int y1, color fill, action f ){ area *p = (area*) malloc( sizeof( area )); p->x0 = x0; p->x1 = x1; p->y0 = y0; p->y1 = y1; p->f = f; lcd.rectangle_write( x0, y0, x1, y1, colors::black, fill ); p->next = first; first = p; } ‘forward’ object linked list of objects Create an object and chain it. Q: this is not quality code. Why?

W 2 L 1 sh 15 Paint – create the list void execute( int x, int y ){ area *p; for( p = first; p!= NULL; p = p->next ){ if( position_is_in_rectangle( x, y, p->x0, p->y0, p->x1, p->y1 )){ p->f( x, y, p ); } color active_color = colors::blue; void paint( int x, int y, area *a ){ lcd.pixel_write( x, y, active_color ); } void select_red( int x, int y, area *a ){ active_color = colors::red; } void select_green( int x, int y, area *a ){ active_color = colors::green; } void select_blue( int x, int y, area *a ){ active_color = colors::blue; } Check, maybe excute global change-color actions draw-box action

W 2 L 1 sh 16 Paint - main int main( void ){ struct touchPosition position; lcd.clear( colors::yellow ); area_add( 10, 10, 200, 180, colors::white, paint ); area_add( 210, 10, 220, 20, colors::red, select_red ); area_add( 210, 25, 220, 35, colors::green, select_green ); area_add( 210, 40, 220, 50, colors::blue, select_blue ); for(;;){ scanKeys(); int held = keysHeld(); if( held & KEY_TOUCH ){ touchRead( & position ); execute( position.px, position.py ); } swiWaitForVBlank(); } return 0; } Create list Execute list

W 2 L 1 sh 17 IDE n Make sure you have copied or installed n devkitPro (in C:\devkitPro) n DeSuME (in C:\devkitPro\sim\DeSmuME) n Python (2.5, 26, 3.0, 3.1, etc) n PSPad n get the week-2-1-paint.zip, unzip to a directory, no spaces in the path name !! n Double-click Project.ppr devkitPro.zip

W 2 L 1 sh 18 IDE Compile, link, build, run simulator

W 2 L 1 sh 19 Assignment n Create an NDS GUI for the reversi code. n Use week-2-1-reversi.zip n Use you own reversi code

W 2 L 1 sh 20 Assignment - list struct area_struct { area *next; int field; int x0, y0, x1, y1; action f; }; void area_add( int field, int x0, int y0, int x1, int y1, action f ){ area *p = (area*) malloc( sizeof( area )); p->x0 = x0; p->x1 = x1; p->y0 = y0; p->y1 = y1; p->field = field; p->next = first; p->f = f; first = p; } Field (0..99)

W 2 L 1 sh 21 Assignment - main int main( void ){ struct touchPosition position; init_reversi(); redraw(); for(;;){ scanKeys(); int held = keysHeld(); if( held & KEY_TOUCH ){ touchRead( & position ); execute( position.px, position.py ); redraw(); } swiWaitForVBlank(); } return 0; }

W 2 L 1 sh 22 Assignment – reversi GUI nintendo_screen lcd; int side_to_move = black; board global_board; int other_side( int x ){ return x == black ? white : black; } color side_color( int side ){ return side == black ? colors::blue : colors :: red; } void try_move( int x, int y, area * a ){ } void redraw_square( area * a ){ } void redraw( void ){ } void init_reversi( void ){ int i, x, y; const int width = 18; reversi_board_init( global_board ); for( i = 0; i < 100; i++ ){ x = 5 + (( i / 10 ) * width ); y = 5 + (( i % 10 ) * width ); area_add( i, x, y, x + width, y + width, try_move ); }

W 2 L 1 sh 23 Assignment Function# Lines in my versionRemarks try_move 8If the move is allowed (for the current side-to-move) do it, and update side_to_move. redraw_square 26 (lots of folded lines) Redraw the border around the square. Draw fill for border, empty square. Draw the tile within an occupied square. redraw 12Clear LCD Redraw all squares (call redraw_square to do the work) Redraw ‘side to move’ indicator What if the current side-to-move has no legal moves?