1.1 Computer Systems 9 machine language and high-level languages like C++:Any high-level language program must be translated into machine language before the computer can understand and follow the program. Compilers A program that translates a high-level language like C++to a machine language is called a compiler.A compiler is thus a somewhat peculiar sort of program, in that its input or data is some other program,and its output is yet another program.To avoid confusion,the input program is usually called the source program or source code,and the translated version produced by the compiler is called the object program or object code.The word code is frequently used to mean a program or a part of a program,and this usage is particularly common when referring to object programs.Now,suppose you want to run a C++program that you have written.In order to get the computer to follow your C++instructions,proceed as follows.First,run the compiler using your C++program as data.Notice that in this case,your C++program is not being treated as a set of instructions.To the compiler,your C++program is just a long string of characters.The output will be another long string of characters, which is the machine-language equivalent of your C++program.Next,run this machine-language program on what we normally think of as the data for the C++program.The output will be what we normally conceptualize as the output of the C++program.The basic process is easier to visualize if you have two computers available,as diagrammed in Display 1.4.In reality,the entire process is accomplished by using one computer two times. Compiler A compiler is a program that translates a high-level language program, such as a C++program,into a machine-language program that the computer can directly understand and execute. The complete process of translating and running a C++program is a bit more complicated than what we show in Display 1.4.Any C++program you write will use some operations (such as input and output routines) that have already been programmed for you.These items that are already programmed for you(like input and output routines)are already compiled and have their object code waiting to be combined with your program's object code to produce a complete machine-language program that can be run on the computer.Another program,called a linker,combines the object code for these program pieces with the object code that the compiler produced
1.1 Computer Systems 9 The complete process of translating and running a C++ program is a bit more complicated than what we show in Display 1.4. Any C++ program you write will use some operations (such as input and output routines) that have already been programmed for you. These items that are already programmed for you (like input and output routines) are already compiled and have their object code waiting to be combined with your program’s object code to produce a complete machine-language program that can be run on the computer. Another program, called a linker, combines the object code for these program pieces with the object code that the compiler produced machine language and high-level languages like C++: Any high-level language program must be translated into machine language before the computer can understand and follow the program. Compilers A program that translates a high-level language like C++ to a machine language is called a compiler. A compiler is thus a somewhat peculiar sort of program, in that its input or data is some other program, and its output is yet another program. To avoid confusion, the input program is usually called the source program or source code, and the translated version produced by the compiler is called the object program or object code. The word code is frequently used to mean a program or a part of a program, and this usage is particularly common when referring to object programs. Now, suppose you want to run a C++ program that you have written. In order to get the computer to follow your C++ instructions, proceed as follows. First, run the compiler using your C++ program as data. Notice that in this case, your C++ program is not being treated as a set of instructions. To the compiler, your C++ program is just a long string of characters. The output will be another long string of characters, which is the machine-language equivalent of your C++ program. Next, run this machine-language program on what we normally think of as the data for the C++ program. The output will be what we normally conceptualize as the output of the C++ program. The basic process is easier to visualize if you have two computers available, as diagrammed in Display 1.4. In reality, the entire process is accomplished by using one computer two times. Compiler A compiler is a program that translates a high-level language program, such as a C++ program, into a machine-language program that the computer can directly understand and execute
10 CHAPTER 1/Introduction to Computers and C++Programming from your C++program.The interaction of the compiler and the linker are diagrammed in Display 1.5.In routine cases,many systems will do this linking for you automatically.Thus,you may not need to worry about linking in many cases. Linking The object code for your C++program must be combined with the object code for routines(such as input and output routines)that your program uses.This process of combining object code is called linking and is done by a program called a 1inker.For simple programs,linking may be done for you automatically. DISPLAY 1.4 Compiling and Running a C++Program(Basic Outline) C++program Data for C++program Compiler Computer Machine- language Computer Output of C++program
10 Chapter 1 / Introduction to Computers and C++ Programming from your C++ program. The interaction of the compiler and the linker are diagrammed in Display 1.5. In routine cases, many systems will do this linking for you automatically. Thus, you may not need to worry about linking in many cases. Linking Display 1.4 Compiling and Running a C++ Program (Basic Outline) C++ program Data for C++ program Compiler Computer Machinelanguage Computer Output of C++ program The object code for your C++ program must be combined with the object code for routines (such as input and output routines) that your program uses. This process of combining object code is called linking and is done by a program called a linker. For simple programs, linking may be done for you automatically
1.1 Computer Systems 11 DISPLAY 1.5 Preparing a C++Program for Running C++program Compiler Object code for Object code for C++program other routines Linker Complete machine- language code ready to run SELF-TEST EXERCISES 1.What are the five main components of a computer? 2.What would be the data for a program to add two numbers? 3.What would be the data for a program that assigns letter grades to students in a class? 4.What is the difference between a machine-language program and a high- level language program? 5.What is the role of a compiler? 6.What is a source program?What is an object program? 7.What is an operating system? 8.What purpose does the operating system serve?
1.1 Computer Systems 11 Self-Test Exercises 1. What are the five main components of a computer? 2. What would be the data for a program to add two numbers? 3. What would be the data for a program that assigns letter grades to students in a class? 4. What is the difference between a machine-language program and a highlevel language program? 5. What is the role of a compiler? 6. What is a source program? What is an object program? 7. What is an operating system? 8. What purpose does the operating system serve? Display 1.5 Preparing a C++ Program for Running Complete machinelanguage code ready to run Object code for C++ program Object code for other routines C++ program Compiler Linker
12 CHAPTER 1/Introduction to Computers and C++Programming 9.Name the operating system that runs on the computer you use to prepare programs for this course. 10.What is linking? 11.Find out whether linking is done automatically by the compiler you use for this course. 1.2 PROGRAMMING AND PROBLEM-SOLVING The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform.It can follow analysis, but it has no power of anticipating any analytical relations or truths.Its prov- ince is to assist us in making available what we are already acquainted with. ADA AUGUSTA,Countess of Lovelace(1815-1852) HISTORY NOTE Charles Babbage, In this section we describe some general principles Ada Augusta that you can use to design and write programs. These principles are not particular to C++.They The first truly programmable computer was designed by Charles Babbage,an apply no matter what programming language you English mathematician and physical scientist. are using. Babbage began the project sometime before 1822 and worked on it for the rest Algorithms of his life.Although he never completed the construction of his machine,the design When learning your first programming language, was a conceptual milestone in the history of it is easy to get the impression that the hard part computing.Much of what we know about of solving a problem on a computer is translating Charles Babbage and his computer design your ideas into the specific language that will be comes from the writings of his colleague fed into the computer.This definitely is not the Ada Augusta,the Countess of Lovelace case.The most difficult part of solving a problem and the daughter of the poet Byron.Ada on a computer is discovering the method of Augusta is frequently given the title of the solution.After you come up with a method of first computer programmer.Her comments, solution,it is routine to translate your method quoted in the opening of the this section, still apply to the process of solving problems into the required language,be it C++or some on a computer.Computers are not magic other programming language.It is therefore and do not,at least as yet,have the ability to helpful to temporarily ignore the programming formulate sophisticated solutions to all the language and to concentrate instead on for- problems we encounter.Computers simply mulating the steps of the solution and writing do what the programmer orders them to them down in plain English,as if the instructions do.The solutions to problems are carried were to be given to a human being rather than a out by the computer,but the solutions computer.A sequence of instructions expressed in are formulated by the programmer.Our this way is frequently referred to as an algorithm. discussion of computer programming begins A sequence of precise instructions which with a discussion of how a programmer leads to a solution is called an algorithm.Some formulates these solutions. approximately equivalent words are recipe,method
12 Chapter 1 / Introduction to Computers and C++ Programming 9. Name the operating system that runs on the computer you use to prepare programs for this course. 10. What is linking? 11. Find out whether linking is done automatically by the compiler you use for this course. 1.2 Programming and Problem-Solving The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform. It can follow analysis; but it has no power of anticipating any analytical relations or truths. Its province is to assist us in making available what we are already acquainted with. ADA AUGUSTA, Countess of Lovelace (1815–1852) In this section we describe some general principles that you can use to design and write programs. These principles are not particular to C++. They apply no matter what programming language you are using. Algorithms When learning your first programming language, it is easy to get the impression that the hard part of solving a problem on a computer is translating your ideas into the specific language that will be fed into the computer. This definitely is not the case. The most difficult part of solving a problem on a computer is discovering the method of solution. After you come up with a method of solution, it is routine to translate your method into the required language, be it C++ or some other programming language. It is therefore helpful to temporarily ignore the programming language and to concentrate instead on formulating the steps of the solution and writing them down in plain English, as if the instructions were to be given to a human being rather than a computer. A sequence of instructions expressed in this way is frequently referred to as an algorithm. A sequence of precise instructions which leads to a solution is called an algorithm. Some approximately equivalent words are recipe, method, History Note Charles Babbage, Ada Augusta The first truly programmable computer was designed by Charles Babbage, an English mathematician and physical scientist. Babbage began the project sometime before 1822 and worked on it for the rest of his life. Although he never completed the construction of his machine, the design was a conceptual milestone in the history of computing. Much of what we know about Charles Babbage and his computer design comes from the writings of his colleague Ada Augusta, the Countess of Lovelace and the daughter of the poet Byron. Ada Augusta is frequently given the title of the first computer programmer. Her comments, quoted in the opening of the this section, still apply to the process of solving problems on a computer. Computers are not magic and do not, at least as yet, have the ability to formulate sophisticated solutions to all the problems we encounter. Computers simply do what the programmer orders them to do. The solutions to problems are carried out by the computer, but the solutions are formulated by the programmer. Our discussion of computer programming begins with a discussion of how a programmer formulates these solutions
1.2 Programming and Problem-Solving 13 Charles Babbage A model of Babbage's computer Ada Augusta, Countess of Lovelace and the first computer programmer directions,procedure,and routine.The instructions may be expressed in a programming language or a human language.Our algorithms will be expressed in English and in the programming language C++.A computer program is simply an algorithm expressed in a language that a computer can understand.Thus,the term algorithm is more general than the term program. However,when we say that a sequence of instructions is an algorithm,we usually mean that the instructions are expressed in English,since if they were expressed in a programming language we would use the more specific term program.An example may help to clarify the concept. Display 1.6 contains an algorithm expressed in English.The algorithm determines the number of times a specified name occurs on a list of names. If the list contains the winners of each of last season's football games and the name is that of your favorite team,then the algorithm determines how many games your team won.The algorithm is short and simple but is otherwise very typical of the algorithms with which we will be dealing
1.2 Programming and Problem-Solving 13 directions, procedure, and routine. The instructions may be expressed in a programming language or a human language. Our algorithms will be expressed in English and in the programming language C++. A computer program is simply an algorithm expressed in a language that a computer can understand. Thus, the term algorithm is more general than the term program. However, when we say that a sequence of instructions is an algorithm, we usually mean that the instructions are expressed in English, since if they were expressed in a programming language we would use the more specific term program. An example may help to clarify the concept. Display 1.6 contains an algorithm expressed in English. The algorithm determines the number of times a specified name occurs on a list of names. If the list contains the winners of each of last season’s football games and the name is that of your favorite team, then the algorithm determines how many games your team won. The algorithm is short and simple but is otherwise very typical of the algorithms with which we will be dealing. Ada Augusta, Countess of Lovelace and the first computer programmer A model of Babbage’s computer Charles Babbage