1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.

Slides:



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

Spring Semester 2013 Lecture 5
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Introduction to C Programming
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS 201 Functions Debzani Deb.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Chapter 3 Getting Started with C++
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
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.
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter Topics The Basics of a C++ Program Data Types
CSC201: Computer Programming
Introduction to the C Language
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Basic Elements of C++.
Getting Started with C.
Chapter 2 - Introduction to C Programming
Programmazione I a.a. 2017/2018.
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
1) C program development 2) Selection structure
Chapter 2 - Introduction to C Programming
ECE 103 Engineering Programming Chapter 12 More C Statements
Chapter 2 - Introduction to C Programming
Compiler vs linker The compiler translates one .c file into a .o file
Introduction to C Programming
CPS125.
Introduction to C Programming
Presentation transcript:

1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014

2 Syllabus General Program Layout General Program Layout C++ Preprocessor C++ Preprocessor main() Function main() Function Comments Comments Statements Statements Functions Functions Libraries Libraries C++ Sample Program C++ Sample Program

3 General Program Layout Header Files (#include) Global Macro Definitions Global Enumerated Types Global Structure Declarations Global Variable Definitions Global Function Prototypes Function Definitions main (…) { … } fun_2 (…) { … } fun_3 (…) { … } fun_k (…) { … } The compiler scans the source code from the beginning of the file to the end of the file. Rule: If component “A” accesses component “B”, then “B” must be defined (or declared) before “A”. A C++ function is a named block of code that performs a task. It can receive or return data values. A program contains one or more function definitions. Every C++ program must have one, and only one, function named “main”. return_type function_name ( formal_params) { Local Variable Definitions Local Function Prototypes C code statements } // OK to have ending comment here

4 C++ Preprocessor Before a C++ program is compiled, the preprocessor scans the source, looking for directives and macros; filters out commentsBefore a C++ program is compiled, the preprocessor scans the source, looking for directives and macros; filters out comments directive → embedded instructions for handling the program before it is compileddirective → embedded instructions for handling the program before it is compiled Example: #include // Loads file stdio.hExample: #include // Loads file stdio.h macro → a named code fragment that is substituted into the program wherever that name is found; may not be proper C, but after inclusion must yield proper C sourcemacro → a named code fragment that is substituted into the program wherever that name is found; may not be proper C, but after inclusion must yield proper C source Example: #define TRUE 1 // Macro TRUE is 1Example: #define TRUE 1 // Macro TRUE is 1

5 main() Function When a C++ program is run, the very first function executed is always the one named main(); and main() must be present main() contains the start up code for the program main() contains the start up code for the program Like other functions, main() can receive input values, compute new values, and return an output value Like other functions, main() can receive input values, compute new values, and return an output value  Input:Data passed from command line to the program  Output:Integer representing the termination status Example: main header with no input arguments needed int main( void )

6 Comments Comments help to document a program Block comments are defined by paired /* and */ with all text in between skipped Example: /* Example Program #1 */ int main( void ) { // main /* This program does nothing */ return 0; } //end main After the program is done, it can return a value that indicates the termination status C is free-form in the formatting of the source code. It is good practice to use a consistent style of spacing, indentation, and blank lines to improve readability

7 Comments /*…*/ - style comments cannot be “nested” within other /*…*/ comments! Wrong Examples: Area = L * W /* /* Area of rectangle */ */ /* x = r * cos(a); /* x component */ y = r * sin(a); /* y component */ */ A section of code with embedded comments can be commented out using preprocessor directives

8 Comments Comments C99 /* */ is still available for block comments C++ allows support for line comments. The characters // mark the start of a comment that extends just to the end of the line. // most C+ versions allow all comments #include int main( void ) { // main float w;/* Comment */ int a;// Comment /* Comment // Comment */ // /* Comment */ // Comment return 0; } //end main

9 Statements Strictly speaking, C++ code consists of expressions, yet we’ll treat operations as statements, similar to other programming languages; just politics Strictly speaking, C++ code consists of expressions, yet we’ll treat operations as statements, similar to other programming languages; just politics A statement is a combination of keywords, expressions, and other statements that forms and performs a complete action A statement is a combination of keywords, expressions, and other statements that forms and performs a complete action Individual statements are terminated by the semicolon ; Individual statements are terminated by the semicolon ; A spurious semicolon by itself is a valid C++ statement, known as the "null statement”, or the empty statement; it does nothing A spurious semicolon by itself is a valid C++ statement, known as the "null statement”, or the empty statement; it does nothing

10 Functions A function definition consists of the function header, a possibly empty formal parameter list, and the function body enclosed in a pair of { and } braces Header → Describes the function name, parameter list, and return type Body → Contains local declarations and statements Braces mark the extent of the body: »Left brace { marks the beginning »Right brace } marks the end »A semicolon is not needed after the ending brace

11 IO Functions in C++ from old C Input: entry from console keyboard scanf( "%c”, &char_variable ); scanf( "%d”, &int_variable ); scanf( "%f”, &float_variable ); scanf( "%lf”,&double_variable ); Output: display to console screen printf( "literal_text” ); printf( "%c", single_character_value ); printf( "%d", integer_value ); printf( "%f", single_precision_floating_point_value ); printf( "%f", double_precision_floating_point_value ); printf( "%s", string_value );

12 Libraries The core C language was designed to be concise and minimalistic; e.g. no array assignments; no nested functions; don’t think this is necessarily good. Even C++ has the same limitation The core C language was designed to be concise and minimalistic; e.g. no array assignments; no nested functions; don’t think this is necessarily good. Even C++ has the same limitation I/O functions, math functions, string functions, etc. are provided via external library packages I/O functions, math functions, string functions, etc. are provided via external library packages A common set of libraries evolved to become part of the ISO C standard A common set of libraries evolved to become part of the ISO C standard Each library has an associated header file that is referenced with the " #include " directive Each library has an associated header file that is referenced with the " #include " directive

13 C++ Sample Program /* Example Program #3 * #include #include #define PI int main( void ) { // main int angle_deg; // Angle in degrees double angle_rad; // Angle in radians double calc_value = 0.0; // Prompt user for the input angle printf( "Enter angle in degrees:\n” ); scanf( "%d", &angle_deg ); // Convert input angle to radians angle_rad = angle_deg * PI / 180; calc_value = sin ( angle_rad ) + cos ( angle_rad ); printf( "Result = %f\n", calc_value ); return 0; } //end main To use trig functions, the math library’s header file must be loaded: #include Variables can be initialized as part of their declaration.

14 More Complex For Loop in C++ // referencing a “user-defined type” mat_t void init( mat_t m ) { // init int row, col; for ( row = 0; row < N; row++ ) { for ( col = 0; col < N; col++ ) { m[ row ][ col ] = row + col; } // end for } // end init

15 Even More Complex For Loop in C++ void print( mat_t m ) { // print int row, col; for ( row = 0; row < 10; row++ ) { for ( col = 0; col < 10; col++ ) { printf( "%7u", m[ row ][ col ] ); // also C++ } // end for printf( "\n" ); } //end for printf( "\n" ); for ( row = N - 10; row < N; row++ ) { for ( col = N - 10; col < N; col++ ) { printf( "%7u", m[ row ][ col ] ); // also C++ } //end for printf( "\n" ); } // end for } //end print