Exercise 1

profilegina123
exercise_ch.1.docx

HARDWARE.HMTL

INTRODUCTION

If you are in the habit of reading computer science books to your dates, you have discovered that when it comes to describing the hardware parts of a computer, there is a diversity in terms of how many different parts make up a computer. Some books divide up the hardware into 3 parts, some 4, some 5, and others 6. In the end, these books really do not disagree; rather, they just organize the same material in different ways.

We will divide the computer up into 5 parts in this ebook. Those parts consist the

1 input devices

2 output devices

3 main memory

4 the arithmetic logic unit (ALU)

5 the control unit (CU)

Please note that, in this module, we will often be simplifying what is actually in each part of the computer's hardware. We will neglect to fully describe the connections between the different parts of the computer, the containers used to hold the different parts of the computer (i.e., the motherboard), the different kinds of chips used in memory, the control unit, and the ALU. The reason for our simplification is because at this stage of programming, we do not need to learn the details of the hardware; we only need to learn the parts of the computer that a programmer needs to know in order to write basic C++ programs.

One way of categorizing computer hardware is to describe the parts that perform each computer function. Again, please note that we will be simplifying the description of these functions. The computer must be able to perform 4 basic functions.

1 A computer must be able to get input; that is, get information from the outside world.

2 A computer must be able to store information; that is, to store information it is working with for future reference.

3 A computer must be able to process information; that is, to perform operations on the information it is storing.

4 A computer must be able to report to the outside world the information that it has stored.

These operations will include of the arithmetic operations of addition, subtraction, multiplication and division. There are other operations that the computer must be able to perform and they will be explained elsewhere. The point here is that for the computer to work, is must be able modify the information given to it.

The computer performs the above functions by means of the above mentioned devices.

INPUT DEVICES

The input devices of a computer allow the computer to get information from the outside world. Input devices are to a computer what the senses are to a person. Take away the input devices and there is no way that one can communicate with the computer.

Two of the most often used input devices on a computer are the keyboard and the mouse. Looking at how these devices are mostly used tells us what kind of input is entered into a computer. The keyboard is primarily used to type in data in specific fields. Examples of such data include one's name, one's address, one's Social Security number, a date, and a simple number. All of this is counted as data. Some data is processed in a program while the rest stays the same.

The mouse is primarily used to access computer resources, such as a file, a device, or a programs. (omputer programs are commands that tell the computer what to do). Thus, we can say that a computer accepts two kinds of input: data information and commands. The accessing of computer resources often causes the computer to start a program.

Other input devices could include disk drives, microphones, cameras, scanners, touchscreens, devices that allow us to communicate with a network such as a modem or a network interface card (NIC), and any other device that allows the computer to bring information from the outside world into the computer.

OUTPUT DEVICES

Another part of a computer's hardware consists of output devices. The computer uses its output devices to communicate information to the outside world. We should note that the outside world does not have to be the human world though many times it is. Just as we could not communicate with the outside world without speaking, writing, using gestures and such, the computer cannot communicate with the outside world without using an output device.

Examples of output devices include the monitor, printer, speaker, touchscreen, devices that allow us to communicate with a network, and disk drives, which are output devices for stored files. We should note that some output devices can also serve as input devices and thus we want to avoid being too rigid in categorizing these devices.

MAIN MEMORY

The computer uses main memory to store information it is working on. We will define main memory as the electronic memory that is used to store all of the programs currently running. This is a simplified definition for two reasons. The computer has electronic memory not dedicated to storing programs currently in use and some parts of some of the programs the computer is running are not always stored in main memory. Other kinds of electronic memory includes read only memory (ROM) chips and cache which is not read only but is not located where the main memory is located. We will write more on them later on.

Each data item and instruction is kept in its own little part of memory and each part of memory has its own unique address. As a result, when any instruction or data item is needed by a program currently running, the computer can find the data or instruction. So main memory is only concerned with programs that are currently in use.

ARITHMETIC LOGIC UNIT (ALU)

If the input devices, output devices, and main memory were the only devices a computer had, then the computer could qualify as an expensive electronic parrot. For the computer to be useful, it must be able to do something with the information it receives from the outside world. What the computer actually does to this information is to process it. That means that the computer can perform 3 kinds of operations on information and then that processed information can be reported back to the user.

The first kind of operation a computer can perform is an arithmetic operation. The arithmetic operations consist of:

· addition

· subtraction

· multiplication

· division

· modulus

All other arithmetic processing of information can be broken down to these 5 operations.

Most people are very familiar with 4 of these operations: addition, subtraction, multiplication and division. But many will not immediately recognize the fifth operation: modulus. What modulus does is to perform an integer division but then, instead of returning the quotient as the answer, it returns the integer remainder. For example, suppose we perform the integer division of 21 divided by 4. The integer answer is 5 because when we multiply 4 times 5, we get the integer answer that is closest to 21 without going over. But when we perform 21 modulus 4, the answer is 1 because the remainder from dividing 21 by 4 is 1 (4 * 5 = 20 and 21 - 20 = 1).

Logic operations are the second kind or operations a computer performs. We will cover the details of logic operations later on. Suffice it to say that logic operations allow the computer to test conditions. For example, we might want to see if a test score is passing so we would check to see if the test is above a 59. Or we might want to see if a test is in the B range, then we would check if the test is between an 80 and an 89 inclusive. This is an simplification of all of the logic operations that the ALU performs. For the present time, the only thing we need to know is that logic operations are also performed by the ALU.

The final operation that the ALU can perform is the shift operation. Shift takes the binary digits and move them to the left or to the right. This changes the value of the memory location. For example, if we shift the digits in the base 10 number 12 two places to the left, we get 1,200. When the ALU performs a shift, it uses base 2. All of this will be more thoroughly explained later on.

CONTROL UNIT (CU)

The control unit is part of a larger unit (the Central Processing Unit, or CPU) that includes the ALU and other parts. The control unit takes a machine instruction, which is part of a translation a C++ instruction, and tells the computer how to implement it by sending out the appropriate sequence of signals that enable the computer to carry out the instruction.

REVIEW EXERCISES

The exercises marked with an asterisk (*) are meant for everybody. Two asterisks indicates exercises of intermediate difficulty, and three asterisks indicated advanced exercises.

1 * What are the various parts of the CPU and what are their functions?

2 * What are the main specifications of your computer at home or a computer at school? In particular, what operating system is the computer running and what is its version, how many CPUs are there, what is the speed (in Hertz, or Hz), and how much memory does the computer have? For Windows operating systems go to the Control Panel, select the System icon, and the General tab. For Macs click on the Apple icon (upper-left), and select About This Mac.

3 ** What is a motherboard and what does it contain? What are some of the devices that a personal computer has other that those on the motherboard?

4 *** What was the architecture of the Intel 8080 CPU? What registers were used, and for what purposes?

PROBLEM_ENV.HTML

INTRODUCTION

We will define the Environment as all of the software used to solve a problem. Software is term for the collection of programs and data files. Programs tell the computer what to do. Data files contain values that were stored by programs that were previously run and will be read and processed by programs to be run.

KINDS OF SOFTWARE

We should note that there are two kinds of software. There is system software and there is application software. System software is concerned with controlling access to computer resources while application software is written to solve user problems.

There are two kinds of system software. The first kind is the Operating System (OS) and the second kind is Network Operating System (NOS). The OS controls direct access to the resources of a single computer. Another view of the operating system is that it makes available commonly used services. This latter definition is a more user-oriented than a computer-oriented definition.

The NOS controls access to shared computer resources. An example of a shared computer resource is a computer lab's printer where more than one computer has access to the printer.

What we see with the two aforementioned kinds of system software is that they are bundled together so that it comes in one package. Regardless, we can say that there are two kinds of system software.

We should note that the user's interface to the OS can be one of two kinds: a graphical user interface (GUI) or a command-line interface. Before defining these two interfaces, we should define what an interface is. An interface is an entity that allows two parties to communicate. In this case, the two parties are the user and the computer. The reason why the user and the computer cannot naturally communicate is because they do not speak the same language.

A GUI uses pictures and drawings to allow the user access to the computer resources that the user will want to access. Each screen access in a GUI interface is a pixel or point. For the computer to write a letter or draw a picture using GUI, it must access the screen multiple times to place a dot at the appropriate position so that the composite of the pixels forms the desired image.

The command-line interface can only display text. The computer displays a character, either alphanumeric or special, in each screen access. The first user interfaces to the OS were command line interfaces. Examples include the job control language (JCL) of early mainframes, the UNIX shell for minicomputers, and MS-DOS for the first Intel-based PCs. GUIs were first commercially available on the 1980s.

The result is that the GUI interface demands more from the computer and less from the user. Because, in this interface, resources are represented by graphics, which are often accompanied by labels the user only needs to read the label or recognize the picture to access a desired resource. The drawing of such pictures and labels requires that the computer access the screen many more times than in a command-line interface, but the user has an easier time accessing the resource as a result. In addition to the number of screen accesses, programs written for GUI tend to have many more additional features, especially visual features that make the user interface programs MANY times bigger.

On the other hand, the command-line interface demands more from the user and less from the computer. For the computer geek, that is not a bad deal. The personal computer's success is mainly due to the fact that they have become easy to use, "user friendly," for non-computer experts. This allows those who are experts in other disciplines to spend more time in their discipline than learning how to more efficiently use a powerful informational tool.

KINDS OF ENVIRONMENTS FOR PROGRAMMERS

Regarding our working environment, there are two kinds of such environments. The software in the working environment is often packaged together, along with other tools, in what is called an Integrated Development Environment (IDE). There are advantages and disadvantages to programming students using IDEs. A key advantage is that the IDE frees the beginning student to focus on task of writing code. A disadvantage to relying on IDEs is that IDEs tend to hide from the beginning student important details about transforming C++ code into machine code.

An alternative to using an IDE is to use a collection of standalone programs. A standalone program must be called separately from other programs. At the least, a programmer needs a text editor, compiler and linker to produce computer programs that can run by the computer. Learning how to use the compiler and linker can becomes more complicated as programming becomes more complex.

Our target environment (in which our programs will be run) can be defined as all of the programs that will run in conjunction with the programs that we write. Minimally speaking, the target environment will consist of the program that we write along with the OS. What we should note here is that the visual user interface for which out programs are written will be a command-line interface, as writing programs for a GUI is considerably more complicated.

(Note: only Microsoft considers the user interface part of the OS. To all other OSs, the user interface is just another application.)

SOLUTION REPRESENTATIONS

Algorithms are abstract solutions to problems, and may be expressed in various forms such as:

6 an outline

7 a diagram, or model

8 or a program; a C++ program for example

PROBLEM ANALYSIS

Given a problem, its solution may not readily occur to you. Good organization is vital to good programming and so is problem analysis.

Problem Analysis consists of the following:

5 Identify the givens

Identify the elements of the problem

Identify the initial values of the elements of the problem

Identify the final values of the elements of the problem

6 State the problem

7 Develop the algorithm

Top–Down Design or Stepwise Refinement is an approach to designing an algorithm consisting of the following:

Step 1:

Understand the problem by thoroughly analyzing it.

Step 2:

Formulate the algorithm by determining and writing the main tasks to be carried out as a sequence of general steps.

Step 3:

Fill in more detail by determining the sub–tasks, if any, for each main task.

Step 4:

Repeat step 3 until you arrive at a precise algorithm expressed in terms of basic executable instructions.

Validation is the analysis of an algorithm for correctness at each stage of the design process. It is an important aspect of top–down design.

EXAMPLE

Consider the problem of AVERAGING A SET OF SCORES

GIVEN:

scores

NEED:

formula for calculating an average

FIND:

· LEVEL ONE OF DEVELOPMENT

Input the SCOREs

Calculate AVERAGE

Output AVERAGE

·

· LEVEL TWO OF DEVELOPMENT

As each SCORE is entered, COUNT and SUM the SCOREs

Calculate AVERAGE using SUM and COUNT

Output AVERAGE

·

· LEVEL THREE OF DEVELOPMENT

Initialize COUNT and SUM to be equal to 0

Input SCORE

If SCORE is valid

Increment COUNT by 1

Add the value of SCORE to SUM

Input SCORE, and repeat this sub-part until all SCORES have been input

If COUNT is not 0, then AVERAGE is equal to SUM divided by COUNT otherwise AVERAGE is equal to 0

Output AVERAGE

OBSERVATIONS:

5 Use of identifiers SCORE, COUNT, SUM, and AVERAGE

6 Initialization of identifiers COUNT and SUM

7 Use of repetition or repetitive construct (the repeat part into the above)

8 Use of selection or selective construct (the if parts in the above)

REVIEW EXERCISES

The method of problem analysis presented in this module can be used to design algorithms not all of which are programmable into a computer. In the following exercises use this method to design an algorithm to solve the problem.

1 * The end of the year holiday season is approaching, and you have to get gifts for various people. What are the inputs, what processing has to be done to generate the output: each gift recipient joyfully opens his or her gifts?

2 ** You are an instructor for a course that evaluates the students by means of 4 quizzes and 2 exams; each quiz is worth 10% of the grade, and each exam is worth 30% of the grade. Design an algorithm that produces final grades for each student in the course.

3 *** Now that you have the final grades for each student in the course, design an algorithms that computes the statistical mean and standard deviation of the set of final grades.

PROGRAM_INTRO.HTML

INTRODUCTION--The 4 Prerequisites of Programming

Computer programming involves nothing more than telling the computer how to solve a problem. Thus writing a program consists of solving the problem and translating our solution into a language that the computer will eventually understand. Also, since the problems to be solved often require human interaction, enabling user-computer communication is part of many solutions and computer programs.

There are two approaches to writing programs. One can either take a sandlot approach to programming or a systematic one. The sandlot approach to writing programs consists of reading the problem and then typing the code on the keyboard until the program works. This brute force reliance on trial and error is often mistakenly confused with Computer Programming. But all that this approach accomplishes is to measure a person's initial programming skills; it does not help one reach one's potential or learn either how to write programs for complex problems.

Part of developing such a systematic approach to programming is to recognize that there are requirements to meet before we start programming. Just as there are prerequisites to taking certain courses in college, there are prerequisites to meet if you are to write real programs. There are 4 such prerequisites. They consist of: 1) knowing the hardware; 2) knowing the environment; 3) learning the kind of application required; and 4) having a systematic approach to programming. Without meeting all of these prerequisites, especially the last one, we are condemning ourselves to being sandlot programmers who will only be able to write programs for simple problems.

KNOWING THE HARDWARE

A general description of any computer's hardware can be found in the Hardware module of this ebook. What is discussed here is how knowing the hardware relates to programming. We first want to use the standard definition of a computer's hardware as the physical components of the computer. Hardware consists of all of those components that we can see and touch.

Every programmer will need to know some information about the hardware, but how much information a programmer needs to know depends on the problem. Some problems demand cursory information. That is the programmer needs to know what the keyboard and/or mouse do and what the screen does. Here, what is necessary to know about the hardware can be learned by observation.

But sometimes, a programmer needs know a great deal of information about the hardware. This is especially true when the programmer must write code that enables the computer to communicate with other devices. In such cases, programmers must know details about the computer that can only learned from the hardware's reference manuals. The programmer may also have to know about the parts of the computer that allow a connection with the device(s) that the computer is communicating with. The programmer may also have to learn about the parts of the device(s) that the computer is communicating with.

In short, what the programmer must know about the hardware before she or he starts writing code to solve a problem depends on how resource-dependent the problem is. The only information that the beginning student needs to know before writing a program is what was explained before, that is that which can be learned by observation. But if you go on to write programs for difficult problems, be prepared to thoroughly learn the insides of the computer as an auto mechanic has studied what makes a car run.

KNOWING THE ENVIRONMENT

There is more than one definition for the computer environment for a programmer. Some books define the environment as the combination of hardware and software. Here, we will define the environment as consisting of all of the software used to solve a problem. So what belongs to the environment is problem-dependent. That is if you are using the computer to write a paper but happen to also have a spreadsheet program open, the environment for the problem of writing the paper includes the software the runs the computer, the Operating System (OS), and the word processing software you are using. The spreadsheet program is not a part of the environment for writing the paper though it may well be part of the environment for another problem.

In addition, the same relationship that exists between the problem and the extent of knowledge we must have about the hardware also applies to the relationship between the problem and the environment. Some problems require a minimal knowledge of the environment, but the more resource dependent the solution is, the more the programmer must know about the environment.

KNOWING THE APPLICATION

The kind of application required depends on the subject matter of the problem and the user who will use the program. So, there are two parts to the programmer learning the kind of application required. The first part is that the programmer must learn the subject(s) that a programming project draws on. If a programmer is assigned to write a program that solves an accounting problem, then the programmer needs to study accounting. If a programmer is writing a payroll program, then the programmer needs to learn about subjects such as regular pay, overtime pay, tax rates, and so forth. If a programmer is writing a program to simulate a football game, then the programmer must learn about football.

The second part that the programmer must learn is how the user expects to interact with program. Here, the programmer must learn from the user. The programmer can learn from the user by observing the user use the current program that must be rewritten or by interviewing the user. The beginning programmer will not have to worry about learning from the user because assignments for the beginning programmer are too simple to involve a 3rd party user. But a programmer who writes programs for a living will be writing programs that others will use.

HAVING A SYSTEMATIC APPROACH TO PROGRAMMING

There have been a number programming approaches in the history of computer programming. The two most prominent approaches for today are a structured programming approach and an object-oriented programming approach. The key difference between these two approaches lies in the problem solving method. The structured approach solves problems by determining the order of actions needed to solve the problem. Solutions that employ a structured approach to problem solving will express the solution using verb phrases and will place these verb phrases in the correct order. Since it is a simple, straight-forward approach, we will employ this approach in this ebook.

An object-oriented approach to problem solving will depend more on using noun phrases than on verb phrases. The reason for this is because the object-oriented problem solving approach first identifies the players, objects that are labeled using nouns or noun phrases, and then determines how can these objects communicate with each other in ways that will solve the problem. This latter approach is a far more complicated approach but it is the approach that is most used by programmers today.

Regardless of the problem solving approach, a programmer can employ a systematic approach to programming. What is important here is that you develop a methodological approach to problem solving and programming. For the most part, the methods you practice should be independent of the problem you are trying to solve.

Here, though, we want to to give a general description of what this systematic approach means: before even beginning the coding, we have to be prepared. This preparation means knowing the requirements of the problem and the desired solution to the problem, designing the solution to the problem, and THEN writing the code. You will find that the design tools used in a structured programming approach are different from the design tools used in an object-oriented programming approach.

After we have determined the requirements and documented them, and after we have designed the solution and documented it, then we write the code. The next job is to test the code by running it several times against diffferent inputs. The test cases and test results are also documented.

REVIEW EXERCISES

* Why is it important to approach programming exercises in a systematic way. Consulting your own experience, what life problems did you approach without using a systematic problem solving technique, and why did you have to go back and re-think the problem?