. Welcome to PLAB. Course Staff Teacher:  Nir Friedman Teaching Assistants:  Yoseph Barash  Liad Blumrosen  Michael Okun.

Slides:



Advertisements
Similar presentations
CSE 105 Structured Programming Language (C)
Advertisements

CSE 105 Structured Programming Language Presentation - 2
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
INTRODUCTION T.Najah Al_Subaie Kingdom of Saudi Arabia Prince Norah bint Abdul Rahman University College of Computer Since and Information System CS240.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CSE1301 Computer Programming Lecture 4: C Primitives I.
Three types of computer languages
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
1 CSE 303 Lecture 8 Intro to C programming read C Reference Manual pp. Ch. 1, , 2.6, 3.1, 5.1, , , , Ch. 8 ; Programming.
Guide To UNIX Using Linux Third Edition
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
C programming Language and Data Structure For DIT Students.
CSE : Programming in C Instructor: Lei Wang Office: Dreese Lab 474 Office Hour: Friday.
Introduction to C. A Brief History Created by Dennis Ritchie at AT&T Labs in 1972 Originally created to design and support the Unix operating system.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Computer Science 210 Computer Organization Introduction to C.
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
1 Web Based Programming Section 6 James King 12 August 2003.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Introduction to C Programming. A Brief History u Created by Dennis Ritchie at AT&T Labs in 1972 u Originally created to design and support the Unix operating.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
Instructor: Tina Tian. About me Office: RLC 203A Office Hours: Wednesday 1:30 - 4:30 PM or .
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Programming With C.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Java Programming, Second Edition Chapter One Creating Your First Java Program.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Jaeki Song ISQS6337 Lecture 01 Introduction. Jaeki Song ISQS6337 Instructor Name: Jaeki Song Office: BA 712 Office Hours Tuesday & Thursday 2:00-3:20.
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Monday, Jan 6, 2003Kate Gregory with material from Deitel and Deitel CO 204 Object Oriented Programming 2003 Trent University Kate Gregory.
Introduction to Programming
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
ATS Programming Short Course I INTRODUCTORY CONCEPTS Tuesday, Feb 10th, 2009 Introduction to Programming.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
Welcome to C and Computer Structure #include int main() { printf ("hello class\n"); return 0; }
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
Welcome to Data Structures in C++. Course Staff 2 Teacher : Ofir Pele TA: Teachers of other groups: Roman Yavich.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Basic Data Types & Memory & Representation
Program style.
Computer Science 210 Computer Organization
C #include <stdio.h> int main() { printf ("hello class\n");
C Programming Hardik H. Maheta.
C #include <stdio.h> int main() { printf ("hello class\n");
C #include <stdio.h> int main() { printf ("hello class\n");
Computer Science 210 Computer Organization
The Elements of Programming Style
Introduction to C Topics Compilation Using the gcc Compiler
POWERPOINT PRESENTATION
The Elements of Programming Style
C Programming Getting started Variables Basic C operators Conditionals
Introduction to C Topics Compilation Using the gcc Compiler
An Overview of C.
C Structures and Commands
Presentation transcript:

. Welcome to PLAB

Course Staff Teacher:  Nir Friedman Teaching Assistants:  Yoseph Barash  Liad Blumrosen  Michael Okun

Communications  WWW:  Personal questions should be sent only to u Newsgroups:  local.course.plab.stud  local.course.plab.ta (moderated)

Course Objectives u Procedural programming language (C). u Pointers (C/C++) u Generic programming (C++ templates) u Design patterns (STL, streams, and more) u Practice of programming:  Style  Testing & Debugging  Efficiency & Portability  Modularity

Books u “The C Programming Language”, 2 nd Edition, Brian W. Kernighan & Dennis M.Ritchie u “C++ Programming Language”, 3 rd Edition, Bjarne Strousrtup u “The C++ Primer”, Stanley Lippman u “C++ for Java Programmers”, Timothy Budd u “C++ How to Program”, Harvey Deitel & Paul Deitel u “The Practice of Programming”, Brian W. Kernighan & Rob Pike u “Programming Pearls” 2 nd Edition, Jon Bentley

Course Grading u 6-7 programming exercises u Mid-term exam u Final exam u Final grade 60% exercises and 40% exam. u Shortage in computers - don’t wait for the last minute.

History CC++ JAVA 70 ’ s – Development of UNIX. (Richie+Kernigham – Bell labs) 80 ’ s – Large efficient code. (Stroustrup – Bell labs) 90 ’ s – Language for the web. (Sun Microsystems) Simple to convert to machine code. Advanced Programming Fast and EfficientSecure and Safe Welcome to Plab Easy to avoid bugs Easy to Debug

C – Design Decisions u “Bare bones” – the language leaves maximal flexibility with the programmer  Efficient code (operating systems)  Full control on memory & CPU usage u High-level  Type checking  High-level constructs u Portable  Standard language definition  Standard library

C – Warning Signs u No run-time checks  Array boundary overruns  Illegal pointers u No memory management  Programmer has to manage memory

C++ - OO extension of C u Classes & methods  OO design of classes u Generic programming  Template allow for code reuse u Stricter type system u Some run-time checks & memory control

First Program in C // This is a comment // This line defines standard I/O library #include // main – name of the main part of the program int main() { // {…} define a block printf("Hello class!\n"); return 0; }

Compiling & Running… > g++ -o hello hello.c > hello Hello class! >

Second Program #include int main() { int i; // declares i as an integer int j = 0; // declares j as an integer, and initializes it to 0 // for( initial ; test condition ; update step ) for( i = 0; i < 10; i++ ) { j += i; // shorthand for j = j + i printf("%d %d %d\n", i, j, (i*(i+1))/2); } return 0; }

Running… > g++ -o loop loop.c > loop

Character Input/Output #include int main() { char c; while( (c = getchar()) != EOF ) putchar(c); return 0 }

Print header #include #define HEADER 10 int main() { int n = 0; char c; while( ((c = getchar()) != EOF) && (n < HEADER) ) { putchar(c); if( c == '\n' ) n++; } return 0; }

Functions C allows to define functions Syntax: int power( int a, int b ) { … return 7; } Return type Parameter declaration Return statement

Procedures  Functions that return void void proc( int a, int b ) { … return; } Return w/o value (optional)

Example – printing powers #include int power( int base, int n ) { int i, p; p = 1; for( i = 0; i < n; i++ ) p = p * base; return p; } int main() { int i; for( i = 0; i < 10; i++ ) printf("%d %d %d\n", i, power(2,i), power(-3,i) ); return 0; }

Functions Declaration “Rule 1”: A function “knows” only functions which were defined above it. void funcA() {... } void funcB() { funcA(); } void funcC() { funcB(); funcA(); funcB(); } void funcA() {... } void funcB() { funcC(); } void funcC() { funcA(); } Error: funcC is not known yet.

Amendment to “Rule 1” : Use forward declarations. void funcC(int param); // or void funcC(int); void funcA(); {... } void funcB() { …. funcC(7); } void funcC(int param) { …. } Functions Declaration

Powers revisted #include // Forward decleration int power( int m, int n); int main() { int i; for( i = 0; i < 10;i++ ) printf("%d %d %d\n", i, power(2,i), power(-3,i) ); return 0; } int power( int base, int n ) { int i, p; p = 1; while( n > 0 ) { if( n % 2 ) p *= base; base *= base; n /= 2; } return p; }

Program Style u Readability u Common Sense u Clarity u Right focus

What’s in a name u Example #define ONE 1 #define TEN 10 #define TWENTY 20 u More reasonable #define INPUT_MODE 1 #define INPUT_BUFSIZE 10 #define OUTPUT_BUFSIZE 20

What’s in a name Use descriptive names for global variables int npending = 0; // current length of input queue Naming conventions vary  numPending  num_pending  NumberOfPendingEvents  …

What’s in a name Compare for( theElementIndex = 0; theElementIndex < numberOfElements; theElementIndex++ ) elementArray[theElementIndex] = theElementIndex; and for( i = 0; i < nelems; i++ ) elem[i] = i; Use short names for locals

What’s in a name Consider int noOfItemsInQ; int frontOfTheQueue; int queueCapacity; … The word “queue” appears in 3 different ways Be Consistent  Follow naming guidelines used by your peers

What’s in a name Use active name for functions now = getDate() Compare if( checkdigit(c) ) … to if( isdigit(c) ) … Accurate active names makes bugs apparent

Indentation Use indentation to show structure Compare for(n++; n <100; field[n++] = 0); c = 0; return ‘\n’; to for( n++; n <100; n++) field[n] = 0; c = 0; return ‘\n’;

Expressions Use parens to resolve ambiguity Compare leap_year = y % 4 == 0 && y %100 != 0 || y % 400 == 0; to leap_year = ((y % 4 == 0) && (y %100 != 0)) || (y % 400 == 0);

Statements Use braces to resolve ambiguity Compare if( i < 100 ) x = i; i++; to if( i < 100 ) { x = i; } i++;

Idioms Do not try to make code “interesting” i = 0; while( i <= n – 1 ) array[i++] = 1; … for( i = 0; i < n; ) array[i++] = 1; … for( i = n; --i >= 0; ) array[i] = 1; … for( i = 0; i < n; i++ ) array[i] = 1; This is the common “idiom” that any programmer will recognize

Idioms Use “else if” for multiway decisions if ( cond 1 ) statement 1 else if ( cond 2 ) statement 2 … else if ( cond n ) statement n else default-statement

Idioms if( x > 0 ) if( y > 0 ) if( x+y < 100 ) {... } else printf(“Too large!\n" ); else printf("y too small!\n"); else printf("x too small!\n"); if( x <= 0 ) printf("x too small!\n"); else if( y <= 0 ) printf("y too small!\n"); else if( x+y >= 100 ) printf("Sum too large!\n" ); else {... }

Comments Don’t belabor the obvious // return SUCCESS return SUCCESS; // Initialize “total” to “number_received” total = number_received; Test: does comment add something that is not evident from the code

Comments Introduce each function // random: return a random integer in [0..r] int random( int r ) { return (int)floor(rand()*r); }

Comments A more elaborate function comment // // GammaGreaterThanOne( Alpha ) // // Generate a gamma random variable when alpha > 1. // // Assumption: Alpha > 1 // // Reference: Ripley, Stochastic Simulation, p.90 // Chang and Feast, Appl.Stat. (28) p.290 // double GammaGreaterThanOne( double Alpha ) { …

Comments Don’t comment bad code – rewrite it! … // If result = 0 a match was found so return // true; otherwise return false; return !result; Instead … return matchfound;

Style recap  Descriptive names  Clarity in expressions  Straightforward flow  Readability of code & comments  Consistent conventions & idioms

Why Bother? Good style:  Easy to understand code  Smaller & polished  Makes errors apparent Sloppy code  bad code  Hard to read  Broken flow  Harder to find errors & correct them