1 cs 161 introduction to programming and problem solving chapter 9 c++ program components herbert g....

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

Upload: gwen-garrison

Post on 17-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

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

1

CS 161Introduction to Programming

and Problem Solving

Chapter 9C++ Program Components

Herbert G. Mayer, PSUStatus 10/20/2014

Page 2: 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 C++ Preprocessor main() Function Comments Statements Functions Libraries C++ Sample Program

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

3

General Program LayoutHeader 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 DefinitionsLocal Function PrototypesC code statements

} // OK to have ending comment here

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

4

C++ Preprocessor

• Before 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 compiled

• Example: #include <stdio.h> // 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 source

• Example: #define TRUE 1 // Macro TRUE is 1

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

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

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 )

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

6

CommentsComments 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

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

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

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

8

CommentsC99

/* */ 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 <stdio.h>

int main( void ){ // main

float w; /* Comment */int a; // Comment/*Comment// Comment*/

// /* Comment */// Commentreturn 0;

} //end main

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

9

Statements• 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

• 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

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

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

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

11

IO Functions in C++ from old CInput: entry from console keyboard

scanf( "%c”, &char_variable );

scanf( "%d”, &int_variable );

scanf( "%f”, &float_variable );

scanf( "%lf”,&double_variable );

Output: display to console screenprintf( "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 );

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

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

• 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

• Each library has an associated header file that is referenced with the "#include" directive

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

13

C++ Sample Program/* Example Program #3 *#include <stdio.h>#include <math.h>#define PI 3.14159265

int main( void ){ // main

int angle_deg; // Angle in degreesdouble angle_rad; // Angle in radiansdouble calc_value = 0.0;// Prompt user for the input angleprintf( "Enter angle in degrees:\n” );scanf( "%d", &angle_deg );

// Convert input angle to radiansangle_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 <math.h>

• Variables can be initialized as part of their declaration.

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

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 for

} // end init

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

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 forprintf( "\n" );

} //end forprintf( "\n" );for ( row = N - 10; row < N; row++ ) {

for ( col = N - 10; col < N; col++ ) {printf( "%7u", m[ row ][ col ] ); //

also C++} //end forprintf( "\n" );

} // end for} //end print