1 Introduction To Computer Programming
1 Introduction To Computer Programming
Objectives
In this section, we will be discussing the basic components of a computer, both hardware and software. We will also be giving a brief overview of programming languages and the program development life cycle. Finally, different number systems and conversions from one type to another will be discussed. At the end of the lesson, the student should be able to: Identify the different components of a computer Know about programming languages and their categories Understand the program development life cycle and apply it in problem solving Learn the different number systems and their conversions A computer is a machine that performs a variety of tasks a c c o r d i n g t o s p e c i f i c instructions. It is a data processing machine which accepts data via an input device and its processor manipulates the data according to a program .The computer has two major components. *Hardware which is the tangible part of the computer. It is composed of electronic and mechanical parts. The second major component is the *Software which is the intangible part of a computer. It consists of data and the computer programs.
3.Algorithm design and representation (Pseudocode or flowchart) 4.Coding and debugging In order to understand the basic steps in solving a problem on a computer, let us define a single problem that we will solve step-by-step as we discuss the problem solving methodologies in detail. The problem we will solve will be defined in the next section.
Problem Definition
A programmer is usually given a task in the form of a problem. Before a program can be designed to solve a particular problem, the problem must be well and clearly defined first in terms of its input and output requirements. A clearly defined problem is already half the solution. Computer programming requires us to define the problem first before we even try to create a solution . Let us now define our example problem: Create a program that will determine the number of times a name occurs in a list.
Problem Analysis
After the problem has been adequately defined, the simplest and yet the most efficient and effective approach to solve the problem must be formulated. Usually, this step involves breaking up the problem into smaller and s i m p l e r s u b - problems. Example Problem: Determine the number of times a name occurs in a list Input to the program: list of names, name to look for Output of the program: the number of times the name occurs in a list Algorithm design and representation Once our problem is clearly defined, we can now set to finding a solution. In computer programming, it is normally required to express our solution in a step-by-step manner. An Algorithm is a clear and unambiguous specification of the steps needed to solve a problem. It may be expressed in either Human language (English, Tagalog), through a graphical representation like a flowchart or through a pseudocode, which is a cross between human language and a programming language. Now given the problem defined in the previous sections, how do we express our general solution in such a way that it is simple yet understandable? Expressing our solution through Human language:
1.Get the list of names 2.Get the name to look for, let's call this the key name. 3.Compare the key name to each of the names in the list. 4.If the key name is the same with a name in the list, add 1 to the count. 5.If all the names have been compared, output the result.
Expressing our solution through pseudocode: Let nameList = List of Names Let keyName = the name to be sought Let Count = 0 For each name in NameList do the following if name == key NameCount = Count + 1 Display Count Figure 1.2: Example of a pseudocode
.There are two types of errors that a programmer will encounter along the way. The first one is compile-time error, and the other is runtime error. Compile-Time Errorsoccur if there is a syntax error in the code. The compiler will detect the error and the program won't even compile. At this point, the programmer is unable to form an executable that a user can run until the error is fixed. Forgetting a semi-colon at the end of a statement or misspelling a certain command, for example, is a compile-time error. It's something the compiler can detect as an error. Compilers aren't perfect and so can't catch all errors at compile time. This is especially true for logic errors such as infinite loops. This type of error is called Runtime error. For example, the actual syntax of the code looks okay. But when you follow the code's logic, the same piece of code keeps executing over and over again infinitely so that it loops. In such a case, compilers aren't really smart enough to catch all of these types of errors at compile-time, and therefore, the program compiles fine into an executable file. However, and unfortunately, when the end-user runs the program, the program (or even their whole computer) freezes up due to an infinite loop. Other types of run-time errors are when an incorrect value is computed, the wrong thing happens, etc.
Hexadecimal Numbers in hexadecimal form are in base 16. This means that the only legal digits are 0-9 and the letters A-F (or a-f, lowercase or uppercase does not matter). We need to write the subscript 16 t o i n d i c a t e t h a t t h e n u m b e r i s a h e x a d e c i m a l n u m b e r . H e r e a r e examples of numbers written in hexadecimal form: 7E B