4 CHAPTER 1/Introduction to Computers and C++Programming In order to store input and to have the equivalent of scratch paper for performing calculations,computers are provided with memory.The program that the computer executes is also stored in this memory.A computer has two forms of memory,called main memory and secondary memory.The program that is being executed is kept in main memory,and main memory is,as the name implies,the most important memory.Main memory consists of a long list of numbered locations called memory locations;the number of memory locations varies from one computer to another,ranging from a few thousand to many millions,and sometimes even into the billions.Each memory location contains a string of Os and 1s.The contents of these locations can change. Hence,you can think of each memory location as a tiny blackboard on which the computer can write and erase.In most computers,all memory locations contain the same number of zero/one digits.A digit that can assume only the values 0 or 1 is called a binary digit or a bit.The memory locations in most computers contain eight bits (or some multiple of eight bits).An eight- bit portion of memory is called a byte,so we can refer to these numbered memory locations as bytes.To rephrase the situation,you can think of the computer's main memory as a long list of numbered memory locations called bytes.The number that identifies a byte is called its address.A data item,such as a number or a letter,can be stored in one of these bytes,and the address of the byte is then used to find the data item when it is needed. If the computer needs to deal with a data item(such as a large number) that is too large to fit in a single byte,it will use several adjacent bytes to hold the data item.In this case,the entire chunk of memory that holds the data item is still called a memory location.The address of the first of the bytes that make up this memory location is used as the address for this larger memory location.Thus,as a practical matter,you can think of the computer's main memory as a long list of memory locations of varying sizes.The size of each of these locations is expressed in bytes and the address of the first byte is used as the address (name)of that memory location.Display 1.2 shows a picture of a hypothetical computer's main memory.The sizes of the memory locations are not fixed,but can change when a new program is run on the computer. Bytes and Addresses Main memory is divided into numbered locations called bytes.The number associated with a byte is called its address.A group of consecutive bytes is used as the location for a data item,such as a number or letter.The address of the first byte in the group is used as the address of this larger memory location. The fact that the information in a computer's memory is represented as Os and 1s need not be of great concern to you when programming in C++
4 Chapter 1 / Introduction to Computers and C++ Programming In order to store input and to have the equivalent of scratch paper for performing calculations, computers are provided with memory. The program that the computer executes is also stored in this memory. A computer has two forms of memory, called main memory and secondary memory. The program that is being executed is kept in main memory, and main memory is, as the name implies, the most important memory. Main memory consists of a long list of numbered locations called memory locations; the number of memory locations varies from one computer to another, ranging from a few thousand to many millions, and sometimes even into the billions. Each memory location contains a string of 0s and 1s. The contents of these locations can change. Hence, you can think of each memory location as a tiny blackboard on which the computer can write and erase. In most computers, all memory locations contain the same number of zero/one digits. A digit that can assume only the values 0 or 1 is called a binary digit or a bit. The memory locations in most computers contain eight bits (or some multiple of eight bits). An eightbit portion of memory is called a byte, so we can refer to these numbered memory locations as bytes. To rephrase the situation, you can think of the computer’s main memory as a long list of numbered memory locations called bytes. The number that identifies a byte is called its address. A data item, such as a number or a letter, can be stored in one of these bytes, and the address of the byte is then used to find the data item when it is needed. If the computer needs to deal with a data item (such as a large number) that is too large to fit in a single byte, it will use several adjacent bytes to hold the data item. In this case, the entire chunk of memory that holds the data item is still called a memory location. The address of the first of the bytes that make up this memory location is used as the address for this larger memory location. Thus, as a practical matter, you can think of the computer’s main memory as a long list of memory locations of varying sizes. The size of each of these locations is expressed in bytes and the address of the first byte is used as the address (name) of that memory location. Display 1.2 shows a picture of a hypothetical computer’s main memory. The sizes of the memory locations are not fixed, but can change when a new program is run on the computer. Bytes and Addresses The fact that the information in a computer’s memory is represented as 0s and 1s need not be of great concern to you when programming in C++ Main memory is divided into numbered locations called bytes. The number associated with a byte is called its address. A group of consecutive bytes is used as the location for a data item, such as a number or letter. The address of the first byte in the group is used as the address of this larger memory location
1.1 Computer Systems 5 DISPLAY 1.2 Memory Locations and Bytes byte 1 byte 2 3 byte location with address 1 byte 3 byte 4 byte 5 2 byte location with address 4 byte 6 1 byte location with address 6 byte 7 byte 8 3 byte location with address 7 byte 9 (or in most other programming languages).There is,however,one point about this use of 0s and Is that will concern us as soon as we start to write programs.The computer needs to interpret these strings of Os and 1s as numbers,letters,instructions,or other types of information.The computer performs these interpretations automatically according to certain coding schemes.A different code is used for each different type of item that is stored in the computer's memory:one code for letters,another for whole numbers, another for fractions,another for instructions,and so on.For example,in one commonly used set of codes,01000001 is the code for the letter A and also for the number 65.In order to know what the string 01000001 in a particular location stands for,the computer must keep track of which code is currently being used for that location.Fortunately,the programmer seldom needs to be concerned with such codes and can safely reason as though the locations actually contained letters,numbers,or whatever is desired. Why Eight? A byte is a memory location that can hold eight bits.What is so special about eight?Why not ten bits?There are two reasons why eight is special.First,eight is a power of 2.(8 is 23.)Since computers use bits,which have only two possible values,powers of 2 are more convenient than powers of 10.Second,it turns out that eight bits (one byte)are required to code a single character(such as a letter or other keyboard symbol)
1.1 Computer Systems 5 (or in most other programming languages). There is, however, one point about this use of 0s and 1s that will concern us as soon as we start to write programs. The computer needs to interpret these strings of 0s and 1s as numbers, letters, instructions, or other types of information. The computer performs these interpretations automatically according to certain coding schemes. A different code is used for each different type of item that is stored in the computer’s memory: one code for letters, another for whole numbers, another for fractions, another for instructions, and so on. For example, in one commonly used set of codes, 01000001 is the code for the letter A and also for the number 65. In order to know what the string 01000001 in a particular location stands for, the computer must keep track of which code is currently being used for that location. Fortunately, the programmer seldom needs to be concerned with such codes and can safely reason as though the locations actually contained letters, numbers, or whatever is desired. Display 1.2 Memory Locations and Bytes byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7 byte 8 byte 9 3 byte location with address 1 2 byte location with address 4 1 byte location with address 6 3 byte location with address 7 Why Eight? A byte is a memory location that can hold eight bits. What is so special about eight? Why not ten bits? There are two reasons why eight is special. First, eight is a power of 2. (8 is 23.) Since computers use bits, which have only two possible values, powers of 2 are more convenient than powers of 10. Second, it turns out that eight bits (one byte) are required to code a single character (such as a letter or other keyboard symbol)
6 CHAPTER 1/Introduction to Computers and C++Programming The memory we have been discussing up until now is the main memory. Without its main memory,a computer can do nothing.However,main memory is only used while the computer is actually following the instructions in a program.The computer also has another form of memory called secondary memory or secondary storage.(The words memory and storage are exact synonyms in this context.)Secondary memory is the memory that is used for keeping a permanent record of information after (and before)the computer is used. Some alternative terms that are commonly used to refer to secondary memory are auxiliary memory,auxiliary storage,external memory,and external storage. Information in secondary storage is kept in units called files,which can be as large or as small as you like.A program,for example,is stored in a file in secondary storage and copied into main memory when the program is run.You can store a program,a letter,an inventory list,or any other unit of information in a file. Several different kinds of secondary memory can be attached to a single computer.The most common forms of secondary memory are hard disks, diskettes,CDs,DVDs,and removable flash memory drives.(Diskettes are also sometimes referred to as floppy disks.CDs(compact discs)used on computers are basically the same as those used to record and play music,while DVDs (digital video discs)are the same as those used to play videos.CDs and DVDs for computers can be read-only so that your computer can read,but cannot change,the data on the disc;CDs and DVDs for computers can also be read/ write,which can have their data changed by the computer.Hard disks are fixed in place and are normally not removed from the disk drive.Diskettes and CDs can be easily removed from the disk drive and carried to another computer.Diskettes and CDs have the advantages of being inexpensive and portable,but hard disks hold more data and operate faster.Flash drives have largely replaced diskettes today and store data using a type of memory called flash memory.Unlike main memory,flash memory does not require power to maintain the information stored on the device.Other forms of secondary memory are also available,but this list covers most forms that you are likely to encounter. Main memory is often referred to as RAM or random access memory.It is called random access because the computer can immediately access the data in any memory location.Secondary memory often requires sequential access, which means that the computer must look through all (or at least very many) memory locations until it finds the item it needs. The processor (also known as the central processing unit,or CPU)is the "brain"of the computer.When a computer is advertised,the computer company tells you what chip it contains.The chip is the processor.The processor follows the instructions in a program and performs the calculations specified by the program.The processor is,however,a very simple brain.All it can do is follow a set of simple instructions provided by the programmer.Typical processor instructions say things like "Interpret the 0s and 1s as numbers,and then add the number in memory location 37 to the number in memory location 59,and
6 Chapter 1 / Introduction to Computers and C++ Programming The memory we have been discussing up until now is the main memory. Without its main memory, a computer can do nothing. However, main memory is only used while the computer is actually following the instructions in a program. The computer also has another form of memory called secondary memory or secondary storage. (The words memory and storage are exact synonyms in this context.) Secondary memory is the memory that is used for keeping a permanent record of information after (and before) the computer is used. Some alternative terms that are commonly used to refer to secondary memory are auxiliary memory, auxiliary storage, external memory, and external storage. Information in secondary storage is kept in units called files, which can be as large or as small as you like. A program, for example, is stored in a file in secondary storage and copied into main memory when the program is run. You can store a program, a letter, an inventory list, or any other unit of information in a file. Several different kinds of secondary memory can be attached to a single computer. The most common forms of secondary memory are hard disks, diskettes, CDs, DVDs, and removable flash memory drives. (Diskettes are also sometimes referred to as floppy disks.) CDs (compact discs) used on computers are basically the same as those used to record and play music, while DVDs (digital video discs) are the same as those used to play videos. CDs and DVDs for computers can be read-only so that your computer can read, but cannot change, the data on the disc; CDs and DVDs for computers can also be read/ write, which can have their data changed by the computer. Hard disks are fixed in place and are normally not removed from the disk drive. Diskettes and CDs can be easily removed from the disk drive and carried to another computer. Diskettes and CDs have the advantages of being inexpensive and portable, but hard disks hold more data and operate faster. Flash drives have largely replaced diskettes today and store data using a type of memory called flash memory. Unlike main memory, flash memory does not require power to maintain the information stored on the device. Other forms of secondary memory are also available, but this list covers most forms that you are likely to encounter. Main memory is often referred to as RAM or random access memory. It is called random access because the computer can immediately access the data in any memory location. Secondary memory often requires sequential access, which means that the computer must look through all (or at least very many) memory locations until it finds the item it needs. The processor (also known as the central processing unit, or CPU) is the “brain” of the computer. When a computer is advertised, the computer company tells you what chip it contains. The chip is the processor. The processor follows the instructions in a program and performs the calculations specified by the program. The processor is, however, a very simple brain. All it can do is follow a set of simple instructions provided by the programmer. Typical processor instructions say things like “Interpret the 0s and 1s as numbers, and then add the number in memory location 37 to the number in memory location 59, and
1.1 Computer Systems put the answer in location 43,"or "Read a letter of input,convert it to its code as a string of 0s and 1s,and place it in memory location 1298."The processor can add,subtract,multiply,and divide and can move things from one memory location to another.It can interpret strings of 0s and Is as letters and send the letters to an output device.The processor also has some primitive ability to rearrange the order of instructions.Processor instructions vary somewhat from one computer to another.The processor of a modern computer can have as many as several hundred available instructions.However,these instructions are typically all about as simple as those we have just described. Software You do not normally talk directly to the computer,but communicate with it through an operating system.The operating system allocates the computer's resources to the different tasks that the computer must accomplish.The operating system is actually a program,but it is perhaps better to think of it as your chief servant.It is in charge of all your other servant programs, and it delivers your requests to them.If you want to run a program,you tell the operating system the name of the file that contains it,and the operating system runs the program.If you want to edit a file,you tell the operating system the name of the file and it starts up the editor to work on that file. To most users,the operating system is the computer.Most users never see the computer without its operating system.The names of some common operating systems are UNIX,DOS,Linux,Windows,Mac OS,iOS,and Android. A program is a set of instructions for a computer to follow.As shown in Display 1.3,the input to a computer can be thought of as consisting of two parts,a program and some data.The computer follows the instructions in the program and in that way performs some process.The data is what we conceptualize as the input to the program.For example,if the program adds two numbers,then the two numbers are the data.In other words,the data is the input to the program,and both the program and the data are input to the computer (usually via the operating system).Whenever we give a computer DISPLAY 1.3 Simple View of Running a Program Program Data Computer Output
1.1 Computer Systems 7 put the answer in location 43,” or “Read a letter of input, convert it to its code as a string of 0s and 1s, and place it in memory location 1298.” The processor can add, subtract, multiply, and divide and can move things from one memory location to another. It can interpret strings of 0s and 1s as letters and send the letters to an output device. The processor also has some primitive ability to rearrange the order of instructions. Processor instructions vary somewhat from one computer to another. The processor of a modern computer can have as many as several hundred available instructions. However, these instructions are typically all about as simple as those we have just described. Software You do not normally talk directly to the computer, but communicate with it through an operating system. The operating system allocates the computer’s resources to the different tasks that the computer must accomplish. The operating system is actually a program, but it is perhaps better to think of it as your chief servant. It is in charge of all your other servant programs, and it delivers your requests to them. If you want to run a program, you tell the operating system the name of the file that contains it, and the operating system runs the program. If you want to edit a file, you tell the operating system the name of the file and it starts up the editor to work on that file. To most users, the operating system is the computer. Most users never see the computer without its operating system. The names of some common operating systems are UNIX, DOS, Linux, Windows, Mac OS, iOS, and Android. A program is a set of instructions for a computer to follow. As shown in Display 1.3, the input to a computer can be thought of as consisting of two parts, a program and some data. The computer follows the instructions in the program and in that way performs some process. The data is what we conceptualize as the input to the program. For example, if the program adds two numbers, then the two numbers are the data. In other words, the data is the input to the program, and both the program and the data are input to the computer (usually via the operating system). Whenever we give a computer Display 1.3 Simple View of Running a Program Program Computer Data Output
8 CHAPTER 1/Introduction to Computers and C++Programming both a program to follow and some data for the program,we are said to be running the program on the data,and the computer is said to execute the program on the data.The word data also has a much more general meaning than the one we have just given it.In its most general sense,it means any information available to the computer.The word is commonly used in both the narrow sense and the more general sense. High-Level Languages There are many languages for writing programs.In this text we will discuss the C++programming language and use it to write our programs.C++is a high-level language,as are most of the other programming languages you are likely to have heard of,such as C,C#,Java,Python,PHP,Pascal,Visual Basic, FORTRAN,COBOL,Lisp,Scheme,and Ada.High-level languages resemble human languages in many ways.They are designed to be easy for human beings to write programs in and to be easy for human beings to read.A high-level language,such as C++,contains instructions that are much more complicated than the simple instructions a computer's processor (CPU)is capable of following. The kind of language a computer can understand is called a low- level language.The exact details of low-level languages differ from one kind of computer to another.A typical low-level instruction might be the following: ADD X Y Z This instruction might mean "Add the number in the memory location called X to the number in the memory location called Y,and place the result in the memory location called Z."The above sample instruction is written in what is called assembly language.Although assembly language is almost the same as the language understood by the computer,it must undergo one simple translation before the computer can understand it. In order to get a computer to follow an assembly language instruction, the words need to be translated into strings of Os and 1s.For example, the word ADD might translate to 0110,the X might translate to 1001,the Y to 1010,and the Z to 1011.The version of the instruction above that the computer ultimately follows would then be: 0110100110101011 Assembly language instructions and their translation into Os and 1s differ from machine to machine. Programs written in the form of Os and Is are said to be written in machine language,because that is the version of the program that the computer (the machine)actually reads and follows.Assembly language and machine language are almost the same thing,and the distinction between them will not be important to us.The important distinction is that between
8 Chapter 1 / Introduction to Computers and C++ Programming both a program to follow and some data for the program, we are said to be running the program on the data, and the computer is said to execute the program on the data. The word data also has a much more general meaning than the one we have just given it. In its most general sense, it means any information available to the computer. The word is commonly used in both the narrow sense and the more general sense. High-Level Languages There are many languages for writing programs. In this text we will discuss the C++ programming language and use it to write our programs. C++ is a high-level language, as are most of the other programming languages you are likely to have heard of, such as C, C#, Java, Python, PHP, Pascal, Visual Basic, FORTRAN, COBOL, Lisp, Scheme, and Ada. High-level languages resemble human languages in many ways. They are designed to be easy for human beings to write programs in and to be easy for human beings to read. A high-level language, such as C++, contains instructions that are much more complicated than the simple instructions a computer’s processor (CPU) is capable of following. The kind of language a computer can understand is called a lowlevel language. The exact details of low-level languages differ from one kind of computer to another. A typical low-level instruction might be the following: ADD X Y Z This instruction might mean “Add the number in the memory location called X to the number in the memory location called Y, and place the result in the memory location called Z.” The above sample instruction is written in what is called assembly language. Although assembly language is almost the same as the language understood by the computer, it must undergo one simple translation before the computer can understand it. In order to get a computer to follow an assembly language instruction, the words need to be translated into strings of 0s and 1s. For example, the word ADD might translate to 0110, the X might translate to 1001, the Y to 1010, and the Z to 1011. The version of the instruction above that the computer ultimately follows would then be: 0110 1001 1010 1011 Assembly language instructions and their translation into 0s and 1s differ from machine to machine. Programs written in the form of 0s and 1s are said to be written in machine language, because that is the version of the program that the computer (the machine) actually reads and follows. Assembly language and machine language are almost the same thing, and the distinction between them will not be important to us. The important distinction is that between