Learning Computer Programming with Ch for the Absolute Beginner
Total Page:16
File Type:pdf, Size:1020Kb
Learning Computer Programming with Ch for the Absolute Beginner Harry H. Cheng UC Davis Center for Integrated Computing and STEM Education (C-STEM) University of California-Davis http://c-stem.ucdavis.edu Copyright ⃝c June, 2016 by Harry H. Cheng, All rights reserved. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written consent of the author, including, but not limited to, in any network or other electronic storage or transmission, or broadcast for distance learning. Table of Contents Preface vii 1 Getting Started with the First Ch Program 1 1.1 Introduction ........................................... 1 1.2 Getting Started with ChIDE ................................... 2 1.2.1 The First Ch Program ................................. 2 1.2.2 Opening Programs in ChIDE from Windows Explorer ................ 3 1.2.3 Editing Programs .................................... 3 1.2.4 Running Programs and Stopping Their Execution ................... 4 1.2.5 Output from Execution of Programs .......................... 4 1.2.6 Correcting Errors in Programs ............................. 5 1.2.7 Browsing Files ..................................... 5 1.3 Executing Programming Statements and Expressions in Ch .................. 7 1.3.1 Interactive Execution of Programming Statements and Expressions ......... 8 1.3.2 Repeating Commands in Ch Command Window ................... 8 1.4 Documentation about ChIDE and Ch Command Window ................... 9 1.5 Chapter Summary ........................................ 9 1.5.1 Programming Tips ................................... 9 1.5.2 Terminology ...................................... 10 1.6 Exercises ............................................ 10 2 Variables, Data Types, and Input/Output 12 2.1 Commonly Used Data Types and Declaration of Variables .................. 12 2.1.1 Declaration of Variables and Integral Type int ..................... 13 2.1.2 Initialization ...................................... 14 2.1.3 Floating-Point Numbers and Type double ....................... 14 2.1.4 Strings ......................................... 16 2.2 Formatted Input and Output ................................... 17 2.2.1 The Output Function printf() ............................. 17 iii 2.2.2 The Input Function scanf() ............................... 21 2.3 Structures of a Program for Solving Applied Problems .................... 24 2.3.1 Copying Contents in a Program to Create a New Program in ChIDE ......... 26 2.4 Debugging Programs in ChIDE ................................. 27 2.4.1 Executing Programs in Debug Mode .......................... 28 2.4.2 Monitoring Local Variables and Their Values in the Debug Pane ........... 28 2.4.3 Changing Values of Variables in the Debug Command Pane ............. 28 2.5 Chapter Summary ........................................ 30 2.5.1 Programming Tips ................................... 30 2.5.2 Terminology ...................................... 32 2.6 Exercises ............................................ 32 3 Operators and Expressions 34 3.1 Expressions ........................................... 34 3.2 Assignment Operator ...................................... 35 3.3 Arithmetic Operators ...................................... 35 3.4 Precedence and Associativity of Operators ........................... 38 3.5 Exponential Expressions and Square Root ........................... 41 3.6 Chapter Summary ........................................ 42 3.6.1 Programming Tips ................................... 42 3.6.2 Terminology ...................................... 43 3.7 Exercises ............................................ 43 4 Graphical Visualization 46 4.1 Plotting Points for Scatter Plots and Trend Lines ........................ 46 4.1.1 Copying and Printing Plots ............................... 48 4.2 Plotting Lines Using Two Points ................................ 49 4.3 Plotting Expressions ...................................... 51 4.4 Obtaining the Coordinates for Points on a Plot ......................... 52 4.5 Plotting Rectangles, Squares, and Circles ........................... 53 4.6 Setting Axis Ranges and Tick Marks in Plots ......................... 55 4.7 Plotting Multiple Lines and Curves in a Single Graph ..................... 57 4.8 Setting Stroke Color and Width, Line and Point Styles .................... 59 4.8.1 Setting Stroke Color and Width ............................ 59 4.8.2 Setting Line Style .................................... 59 4.8.3 Setting Point Style ................................... 59 4.9 Filling Objects with Colors ................................... 63 4.10 Plotting Texts and Arrows ................................... 65 4.11 Saving Plot in a File ....................................... 66 4.12 Chapter Summary ........................................ 68 4.12.1 Programming Tips ................................... 68 4.12.2 Terminology ...................................... 68 4.13 Exercises ............................................ 69 iv 5 Relations and Making Decisions 74 5.1 Relational Operators ...................................... 74 5.2 Selection Statements for Decision Making ........................... 77 5.2.1 The if Statement .................................... 77 5.2.2 The if-else Statement .................................. 77 5.2.3 Debugging Programs with Selection Statements Using ChIDE ............ 79 5.2.4 Programming Style and Indenting Programs Using ChIDE .............. 80 5.3 Logical Operators ........................................ 80 5.4 The else if Selection Statement and Early Exit of a Program .................. 85 5.5 Chapter Summary ........................................ 86 5.5.1 Programming Tips ................................... 86 5.5.2 Terminology ...................................... 90 5.6 Exercises ............................................ 90 6 Loops for Repetition and Random Numbers 93 6.1 The while Loop ......................................... 93 6.2 Debugging Programs with Repetition Statements Using ChIDE ................ 94 6.3 Increment and Decrement Operators .............................. 95 6.4 Control of Repetition ...................................... 95 6.4.1 Counter-Controlled Repetition ............................. 95 6.4.2 Sentinel-Controlled Repetition ............................. 98 6.5 The for Loop .......................................... 100 6.6 Formatting Output with a Specified Field Width ........................ 102 6.7 The break Statement ...................................... 104 6.8 Random Number Generation .................................. 105 6.9 Chapter Summary ........................................ 110 6.9.1 Programming Tips ................................... 110 6.9.2 Terminology ...................................... 111 6.10 Exercises ............................................ 111 7 Modular Programming with Functions 118 7.1 Function Definitions ...................................... 118 7.2 Debugging Programs with Functions in ChIDE ........................ 121 7.3 Functions for Mathematical Formulas ............................. 123 7.4 Functions without Arguments or Return Values ........................ 124 7.5 Plotting Functions for Graphical Display ............................ 127 7.5.1 Plotting a Curve Using the Member Function func2D() ................ 127 7.5.2 Zooming in to Find the Coordinates of a Point on a Plot ............... 128 7.5.3 Plotting Multiple Curves Using Different Functions in a Single Plot ......... 131 7.5.4 Plotting a Scatter Plot Using the Member Function func2DPoint() .......... 131 7.5.5 Filling an Area with Color ............................... 133 7.6 Passing Arguments of Function by References ......................... 136 7.6.1 z Solving Quadratic Equations ............................. 138 7.7 zMathematical Functions in the Standard C Library ...................... 139 7.8 Chapter Summary ........................................ 141 7.8.1 Programming Tips ................................... 141 v 7.8.2 Terminology ...................................... 141 7.9 Exercises ............................................ 142 8 Arrays for Data Processing 147 8.1 Declaration, Reference, Input, and Output for Arrays ..................... 147 8.2 How Arrays Are Stored in Memory .............................. 149 8.3 Initialization of Arrays ..................................... 150 8.4 Debugging Programs with Arrays ............................... 151 8.5 Processing Data in Arrays ................................... 152 8.6 Passing Arrays to Functions .................................. 155 8.7 Plotting Data in Arrays for Graphical Display ......................... 158 8.7.1 Plotting a Curve Using the Member Function data2DCurve() ............ 158 8.7.2 Plotting Points for Scatter Plots and Trend Lines ................... 159 8.7.3 Plotting Multiple Curves in Different Arrays in a Single Plot ............. 162 8.8 Chapter Summary ........................................ 164 8.8.1 Programming Tips ................................... 164 8.8.2 Terminology ...................................... 164 8.9 Exercises ............................................ 164 9 Working with Data Files 167 9.1 Writing Data Into Files ..................................... 167 9.2 Reading Data From Files .................................... 170 9.2.1 Using Function feof() .................................. 172 9.3 Plotting Data