CONTENTS ix 13.4 Summary of the Course 404 14 Programming Style Guide for C++ 405 15 Syntax reference for beginning C++ 413 16 Syntax reference for more advanced C+ 417 17 Syntax reference for MATLAB 421
CONTENTS ix 13.4 Summary of the Course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 14 Programming Style Guide for C++ 405 15 Syntax reference for beginning C++ 413 16 Syntax reference for more advanced C++ 417 17 Syntax reference for MATLAB 421
X CONTENTS
x CONTENTS
Chapter 1 Introduction to the course Computers are stupid.They only give answers. Computers are blindly faithful.They only do what you tell them to do. Computers are dumb.They do not do what you want them to do. 1.1 What this course is about This course is called "ENG101:Introduction to Computers and Programming". The College of Engineering Bulletin describes it as follows (emphasis is mine) Introduction to the organization of digital computers,computing (i.e.programming)as a tool in engineering,and programming in C++and MATLAB Let's define a few things first... What is an engineer?What is engineering? The American Heritage Dictionary defines engineering as The application of science to prac- tical ends,such as the design,manufacture,and operation of structures,machines,and systems.An engineer is defined as someone who practices engineering as a profession Our definition is somewhat broader.Engineering is a broad range of technical endeavor that starts with the basic sciences (trying to characterize and understand the basic laws of nature),integrates and systematizes this knowledge and builds things based on these principles,manufactures these "things"on a large scale,and makes it available for societal benefit.If we broaden our definition of "things"to ideas,methods,algorithms(a prescription for doing something,like a recipe in cooking),as well as physical devices,then that captures the essence of what engineering is today. 1
Chapter 1 Introduction to the course Computers are stupid. They only give answers. Computers are blindly faithful. They only do what you tell them to do. Computers are dumb. They do not do what you want them to do. 1.1 What this course is about This course is called“ENG101: Introduction to Computers andProgramming”. The College of Engineering Bulletin describes it as follows (emphasis is mine) Introduction to the organization of digital computers, computing (i.e. programming) as a tool in engineering, and programming in C++ and MATLAB Let’s define a few things first... What is an engineer? What is engineering? The American Heritage Dictionary defines engineering as The application of science to practical ends, such as the design, manufacture, and operation of structures, machines, and systems. An engineer is defined as someone who practices engineering as a profession. Our definition is somewhat broader. Engineering is a broad range of technical endeavor that starts with the basic sciences (trying to characterize andunderstandthe basic laws of nature), integrates and systematizes this knowledge and builds things based on these principles, manufactures these “things” on a large scale, andmakes it available for societal benefit. If we broaden our definition of “things” to ideas, methods, algorithms (a prescription for doing something, like a recipe in cooking), as well as physical devices, then that captures the essence of what engineering is today. 1
2 CHAPTER 1.INTRODUCTION TO THE COURSE In our College of Engineering you will encounter faculty and staff who might otherwise have been called Mathematicians,Physicists,Chemists and Biologists. What is a computer? Again,we appeal at first to some standard reference texts. American Heritage Dictionary 1.One (a person)who computes. Some of the best examples: -Richard Feynman's computers-teams of people working out calculations during the Manhattan Project Calculation of ballistics tables for WWIl -Calculation of sine/cosine/logarithm tables 2.A device (a machine)that computes,especially a programmable electronic machine that performs high-speed mathematical or logical operations or that assembles,stores correlates,or otherwise processes information. Oxford English Dictionary 1.One who computes;a calculator,reckoner;spec.a person employed to make calcu- lations in an observatory,in surveying,etc.(Earliest reference 1646). 2.A calculating-machine;esp.an automatic electronic device for performing mathe- matical or logical operations;freg.with defining word prefixed,as analogue,digital, electronic computer (see these words).(Earliest reference 1897). McGraw-Hill Encyclopedia of Science and Technology A device that receives,processes,and presents information.The two basic types of computers are analog and digital.Although generally not regarded as such,the most prevalent computer is the simple mechanical analog computer,in which gears,levers, ratchets,and pawls perform mathematical operations-for example,the speedometer and the watt-hour meter (used to measure accumulated electrical usage).The general public has become much more aware of the digital computer with the rapid proliferation of the hand-held calculator and a large variety of intelligent devices,ranging from typewriters to washing machines. Our definition A“device”that 1.accepts some form of input (usually disorganized or of some highly specific nature), 2.responds to this input information in a "pre-determined"(programmed)way, 3.produces some form of output (usually more organized or coherent than the input) Some common examples of computers
2 CHAPTER 1. INTRODUCTION TO THE COURSE In our College of Engineering you will encounter faculty andstaff who might otherwise have been calledMathematicians, Physicists, Chemists andBiologists. What is a computer? Again, we appeal at first to some standard reference texts. American Heritage Dictionary 1. One (a person) who computes. Some of the best examples: - RichardFeynman’s computers—teams of people working out calculations during the Manhattan Project - Calculation of ballistics tables for WWII - Calculation of sine/cosine/logarithm tables 2. A device (a machine) that computes, especially a programmable electronic machine that performs high-speedmathematical or logical operations or that assembles, stores, correlates, or otherwise processes information. Oxford English Dictionary 1. One who computes; a calculator, reckoner; spec. a person employedto make calculations in an observatory, in surveying, etc. (Earliest reference 1646). 2. A calculating-machine; esp. an automatic electronic device for performing mathematical or logical operations; freq. with defining word prefixed, as analogue, digital, electronic computer (see these words). (Earliest reference 1897). McGraw-Hill Encyclopedia of Science and Technology A device that receives, processes, and presents information. The two basic types of computers are analog and digital. Although generally not regarded as such, the most prevalent computer is the simple mechanical analog computer, in which gears, levers, ratchets, andpawls perform mathematical operations – for example, the speedometer andthe watt-hour meter (usedto measure accumulatedelectrical usage). The general public has become much more aware of the digital computer with the rapid proliferation of the hand-heldcalculator anda large variety of intelligent devices, ranging from typewriters to washing machines. Our definition A “device” that 1. accepts some form of input (usually disorganized or of some highly specific nature), 2. responds to this input information in a “pre-determined” (programmed) way, 3. produces some form of output (usually more organized or coherent than the input) Some common examples of computers
1.1.WHAT THIS COURSE IS ABOUT 3 Personal:Wristwatch,electronic car keys,phone,radio,GPS device,calculator,lap- top… In the home:Stove,refrigerator,freezer,microwave oven,clocks and timers,toaster, coffee machines,thermostats,washer and dryer,TV's,VCR's,audio equipment... Automotive:fuel delivery,air/fuel mixture,spark advance,brakes(ABS),traction con- trol,suspension adjustment,climate control,navigation,computer-controlled driving (coming soon)... Mechanical analog computers:Wind-up clocks,toys,mechanical calculators... Electrical analog computers:Integrators,differentiators,differential equation solvers, Christmas light flashers (cheap ones!)... Digital computers:Calculators,PDAs,Palm computers,laptops,desktops,mini- computers,mainframe computers.... Digital computers? Digital computers are the simplest of all!They only understand two things,which we label“1”and0”,or“on”or“off',but more precisely,states of“high”or "low”voltage. Computers switch between these two states very,VERY quickly.Modern "run-of-the-mill" digital computers can throw about 100 billion switches per second!That's enough to write about 10 million pages of text in one second. The input is rarely a collection of Os and 1s,011110000110...It can be in the form of text that is converted to 0s and 1s or voltage that is "digitized"with an "analog-to- digital"converter.(Musical recording,for example.) The output is similarly rarely a collection of 0s and 1s.It can be 1D data (words, numbers),2D data(functions,charts,pictures,music),3D data(video,surfaces,time- evolving functions)... The processing or computing or programming part is what the bulk of the course is concerned with.The programming responds to the input in some pre-determined way.In some digital computers the programming is fixed,for example,a digital clock or a digital thermometer.In the most interesting cases,the program can be changed. In this course we will learn to program programmable digital computers
1.1. WHAT THIS COURSE IS ABOUT 3 • Personal: Wristwatch, electronic car keys, phone, radio, GPS device, calculator, laptop... • In the home: Stove, refrigerator, freezer, microwave oven, clocks andtimers, toaster, coffee machines, thermostats, washer and dryer, TV’s, VCR’s, audio equipment... • Automotive: fuel delivery, air/fuel mixture, spark advance, brakes (ABS), traction control, suspension adjustment, climate control, navigation, computer-controlled driving (coming soon)... • Mechanical analog computers: Wind-up clocks, toys, mechanical calculators... • Electrical analog computers: Integrators, differentiators, differential equation solvers, Christmas light flashers (cheap ones!)... • Digital computers: Calculators, PDAs, Palm computers, laptops, desktops, minicomputers, mainframe computers.... Digital computers? Digital computers are the simplest of all! They only understand two things, which we label “1” and“0”, or “on” or “off”, but more precisely, states of “high” or “low” voltage. Computers switch between these two states very, VERY quickly. Modern “run-of-the-mill” digital computers can throw about 100 billion switches per second! That’s enough to write about 10 million pages of text in one second. • The input is rarely a collection of 0s and1s, 011110000110... It can be in the form of text that is converted to 0s and 1s or voltage that is “digitized” with an “analog-todigital” converter. (Musical recording, for example.) • The output is similarly rarely a collection of 0s and 1s. It can be 1D data (words, numbers), 2D data (functions, charts, pictures, music), 3D data (video, surfaces, timeevolving functions)... • The processing or computing or programming part is what the bulk of the course is concerned with. The programming responds to the input in some pre-determined way. In some digital computers the programming is fixed, for example, a digital clock or a digital thermometer. In the most interesting cases, the program can be changed. In this course we will learn to program programmable digital computers