4 CHAPTER 1.INTRODUCTION TO THE COURSE How are digital computers programmed? You can either start off the computer with the 0s and 1s in the right place,or,you use a pro- gramming language.Programming languages provide the prescription,the pre-determined steps that instruct the computer how to respond to the various input it gets.We will learn two languages in this course. C++is a very popular (one of the most popular and common)language,a general-purpose language that can be used for all aspects of computing.It is the best language to use if one wants to teach many of the basic operations that a computer can perform. MATLAB is a language that is suited for scientific and engineering applications,designed for getting answers quickly,with the minimum of effort,with reasonable guarantee that the answer is correct.It also provides graphical output with a minimum of effort. What this course is REALLY about? The hidden agenda This course is all about putting ideas into action.It is about taking understood notions of how things work and building something,developing a numerical solution for some specific problem.This is what engineering is all about.The course will try to put your knowledge of mathematics and physics to practical use.Some of the problems we may solve in this course... How can I program a computer to play a simple game? How far can a golf ball fly into a 50 mile/hour wind? How can one find the shortest distance between two points when the landscape is not flat? If I take 10,000 steps in random directions,how far,on average,will I be from where I started? How can I do calculus on a computer (integrate,differentiate,find maxima and min- ima)? Where should I place a radiator in my room so that I am warm and do not have to pay too much for the heat? ·How can I make a vacuum?
4 CHAPTER 1. INTRODUCTION TO THE COURSE How are digital computers programmed? You can either start off the computer with the 0s and1s in the right place, or, you use a programming language. Programming languages provide the prescription, the pre-determined steps that instruct the computer how to respondto the various input it gets. We will learn two languages in this course. C++ is a very popular (one of the most popular andcommon) language, a general-purpose language that can be usedfor all aspects of computing. It is the best language to use if one wants to teach many of the basic operations that a computer can perform. MATLAB is a language that is suitedfor scientific andengineering applications, designed for getting answers quickly, with the minimum of effort, with reasonable guarantee that the answer is correct. It also provides graphical output with a minimum of effort. What this course is REALLY about? The hidden agenda This course is all about putting ideas into action. It is about taking understood notions of how things work and building something, developing a numerical solution for some specific problem. This is what engineering is all about. The course will try to put your knowledge of mathematics andphysics to practical use. Some of the problems we may solve in this course... • How can I program a computer to play a simple game? • How far can a golf ball fly into a 50 mile/hour wind? • How can one find the shortest distance between two points when the landscape is not flat? • If I take 10,000 steps in random directions, how far, on average, will I be from where I started? • How can I do calculus on a computer (integrate, differentiate, find maxima and minima)? • Where shouldI place a radiator in my room so that I am warm anddo not have to pay too much for the heat? • How can I make a vacuum?
1.1.WHAT THIS COURSE IS ABOUT 5 What this course is NOT REALLY about? The ugly stuff It is not primarily a course on computer language syntax.It may seem like it at times, but it really isn't.Learning syntax is just a means to an end. It is not really a course on operating systems,computer hardware,or the fine detail of how computers work.We will discuss this but only enough to get the job done. Educational Objectives Engineering 101,Introduction to Computers and Programming,is intended to give you an exposure to,and a chance to practice,algorithmic thinking.It does so by asking you to actually solve problems using algorithms.This may be different from other courses that you have taken,because we do not tell you exactly how to solve the assigned problems.Rather, we provide you with tools that will allow you to solve them and some examples of how problems are solved.But you must creatively assemble the solution yourself. Note that neither C++nor MATLAB languages per se are the keys to the course.We teach you these languages so that you will have a means to express algorithms for execution on a computer,but a great many other computer languages could have been selected to support our central purpose of getting you to think about solving problems through a set of procedural steps.Because programming languages such as C++and MATLAB contain many constructs and demand great attention to detail,it is easy to lose sight of the true intellectual center of the course.But keeping a focus on algorithmic thinking will make writing programs less difficult and less time consuming The educational objectives of Engineering 101 include: To introduce students to algorithmic thinking in their approach to problem solving. To teach students to implement algorithms in ANSI C++. To teach students to implement algorithms in MATLAB To have students apply their knowledge of elementary physics and calculus. To provide students with a foundation on which to base their later applications of computers in engineering. Students who successfully complete Engineering 101 (receive a grade of C or better)should: Design algorithms to accomplish clearly specified tasks
1.1. WHAT THIS COURSE IS ABOUT 5 What this course is NOT REALLY about? The ugly stuff • It is not primarily a course on computer language syntax. It may seem like it at times, but it really isn’t. Learning syntax is just a means to an end. • It is not really a course on operating systems, computer hardware, or the fine detail of how computers work. We will discuss this but only enough to get the job done. Educational Objectives Engineering 101, Introduction to Computers and Programming, is intended to give you an exposure to, anda chance to practice, algorithmic thinking. It does so by asking you to actually solve problems using algorithms. This may be different from other courses that you have taken, because we do not tell you exactly how to solve the assigned problems. Rather, we provide you with tools that will allow you to solve them and some examples of how problems are solved. But you must creatively assemble the solution yourself. Note that neither C++ nor MATLAB languages per se are the keys to the course. We teach you these languages so that you will have a means to express algorithms for execution on a computer, but a great many other computer languages couldhave been selectedto support our central purpose of getting you to think about solving problems through a set of procedural steps. Because programming languages such as C++ and MATLAB contain many constructs and demand great attention to detail, it is easy to lose sight of the true intellectual center of the course. But keeping a focus on algorithmic thinking will make writing programs less difficult and less time consuming. The educational objectives of Engineering 101 include: • To introduce students to algorithmic thinking in their approach to problem solving. • To teach students to implement algorithms in ANSI C++. • To teach students to implement algorithms in MATLAB. • To have students apply their knowledge of elementary physics and calculus. • To provide students with a foundation on which to base their later applications of computers in engineering. Students who successfully complete Engineering 101 (receive a grade of C or better) should: • Design algorithms to accomplish clearly specifiedtasks
6 CHAPTER 1.INTRODUCTION TO THE COURSE Display knowledge of ANSI C+syntax and semantics Display knowledge of MATLAB syntax and semantics Successfully implement clearly stated algorithms in C++. Successfully implement clearly stated algorithms in MATLAB. Be prepared to engage in discipline-specific instruction in computing. 1.2 Problems 1.In the broad definition of "computer"in this chapter,how many computers can you find:On yourself?In your room?In a car?In your home? 2.A typical 3rd grader is a lot smarter than any computer.They can read,do simple math,play a musical instrument,memorize poetry and write stories.Generally,with sufficient motivation,they will also do as they are told.How many ways could you instruct a 3rd grader to determine the value of m without the aid of a computer or calculating device?
6 CHAPTER 1. INTRODUCTION TO THE COURSE • Display knowledge of ANSI C+ syntax and semantics • Display knowledge of MATLAB syntax and semantics. • Successfully implement clearly statedalgorithms in C++. • Successfully implement clearly statedalgorithms in MATLAB. • Be preparedto engage in discipline-specific instruction in computing. 1.2 Problems 1. In the broaddefinition of “computer” in this chapter, how many computers can you find: On yourself? In your room? In a car? In your home? 2. A typical 3rd grader is a lot smarter than any computer. They can read, do simple math, play a musical instrument, memorize poetry andwrite stories. Generally, with sufficient motivation, they will also do as they are told. How many ways could you instruct a 3rd grader to determine the value of π without the aidof a computer or calculating device?
Chapter 2 Data representations 2.1 Bits,nibbles,bytes and words bit The digital computer processing hardware is made up of transistors,each of which can exist in only one of two possible states,.a“state”of "high voltage'”or "low voltage”. This state of high/low (or on/off)is represented abstractly as 1/0(one/zero).A "bit" is some piece of microscopic (usually!)hardware that can exist in either of these two states as determined by a programmer or computer designer.In addition to the voltage states in a transistor in a computer's memory chips or processing chips,there are many others forms:magnetic domains in a floppy disk,hard disk or magnetic tape,"pits" in a CD or DVD,holes in paper tape or computer cards.Nowadays,there are billions or trillions of bits of various forms in a typical computer.A bit may be simple,but it is usually so small,typically well below a micrometer across(1 micrometer =10-6 m),that we can conceive of cramming billions of them into a relatively small volume of space.Some bits,particularly those in the processing chips of a computer,can also be made to change states very rapidly,well under one nanosecond.(1 nanosecond 10-9s.)So,we can do a lot of different things with lots of bits in a very short span of time. nibble A "nibble"is a collection of 4 bits.A nibble can be in one of 24=16 separate combinations of bits. byte A "byte"is a collection of 2 nibbles,or 8 bits.A byte can be in one of 28=256 separate combinations of bits.This is enough to,for example,associate with each unique pattern one of the characters of the English alphabet (upper and lower case) plus all the common special characters we may use,for example,in writing English prose or a technical document,e.g.'!$&(),:"?And,there would be lots of room left over to make other interesting associations. 7
Chapter 2 Data representations 2.1 Bits, nibbles, bytes and words bit The digital computer processing hardware is made up of transistors, each of which can exist in only one of two possible states, a “state” of “high voltage” or “low voltage”. This state of high/low (or on/off) is representedabstractly as 1/0 (one/zero). A “bit” is some piece of microscopic (usually!) hardware that can exist in either of these two states as determined by a programmer or computer designer. In addition to the voltage states in a transistor in a computer’s memory chips or processing chips, there are many others forms: magnetic domains in a floppy disk, hard disk or magnetic tape, “pits” in a CD or DVD, holes in paper tape or computer cards. Nowadays, there are billions or trillions of bits of various forms in a typical computer. A bit may be simple, but it is usually so small, typically well below a micrometer across (1 micrometer = 10−6 m), that we can conceive of cramming billions of them into a relatively small volume of space. Some bits, particularly those in the processing chips of a computer, can also be made to change states very rapidly, well under one nanosecond. (1 nanosecond = 10−9 s.) So, we can do a lot of different things with lots of bits in a very short span of time. nibble A “nibble” is a collection of 4 bits. A nibble can be in one of 24 = 16 separate combinations of bits. byte A “byte” is a collection of 2 nibbles, or 8 bits. A byte can be in one of 28 = 256 separate combinations of bits. This is enough to, for example, associate with each unique pattern one of the characters of the English alphabet (upper andlower case) plus all the common special characters we may use, for example, in writing English prose or a technical document, e.g. ‘!$&(),:;“’?. And, there would be lots of room left over to make other interesting associations. 7
8 CHAPTER 2.DATA REPRESENTATIONS word A "word"is a collection of bytes-typically 4 for 232=4,294,672,296 possible com- binations.We can do a lot with all the different representations of "words".We will see,later in the lecture,how whole numbers can be associated with the various word patterns. The notion that many bits can conspire,in very large numbers,to produce wonderfully intricate objects is illustrated in the following pictures of my dog,Matilda Skip Bielajew II. (Yes,that is her real name!)The first picture was generated from a file that was about 12 Megabytes,containing 96 million bits.It looks like real life.Yet,even when we zoom a little, we start to see the effects of the underlying bit structure.Look at how her whiskers appear jagged.Under extreme magnification (one of her whiskers,again)we see that the picture is made up of flat,uniformly colored "tiles".Each one of these tiles is represented by 24 bits, 8 bits each for red,green and blue intensity,that,to all appearances,affords an apparently uniform transition of color from tile to tile
8 CHAPTER 2. DATA REPRESENTATIONS word A “word” is a collection of bytes—typically 4 for 232 = 4, 294, 672, 296 possible combinations. We can do a lot with all the different representations of “words”. We will see, later in the lecture, how whole numbers can be associatedwith the various word patterns. The notion that many bits can conspire, in very large numbers, to produce wonderfully intricate objects is illustrated in the following pictures of my dog, Matilda Skip Bielajew II. (Yes, that is her real name!) The first picture was generatedfrom a file that was about 12 Megabytes, containing 96 million bits. It looks like real life. Yet, even when we zoom a little, we start to see the effects of the underlying bit structure. Look at how her whiskers appear jagged. Under extreme magnification (one of her whiskers, again) we see that the picture is made up of flat, uniformly colored “tiles”. Each one of these tiles is represented by 24 bits, 8 bits each for red, green and blue intensity, that, to all appearances, affords an apparently uniform transition of color from tile to tile