Computer Science Unit 1 Notes

12
ONE ON ONE PROFESSIONAL TUTORIAL SERVICE Computer Science Unit1 a concise course Chavez N. Allen 1/1/2014 This book serves as a simplified approach to learning Cape Computer Science Unit 1

Transcript of Computer Science Unit 1 Notes

Page 1: Computer Science Unit 1 Notes

One on one professional tutorial service

Computer Science Unit1

a concise course

Chavez N. Allen

1/1/2014

This book serves as a simplified approach to learning Cape Computer Science Unit 1

Page 2: Computer Science Unit 1 Notes

ContentsSteps in compiling a code (compilation process)........................................................2

Editing..................................................................................................................... 2

Compiling................................................................................................................2

Linking..................................................................................................................... 2

Executing................................................................................................................ 2

Steps in problem solving............................................................................................4

Defining the Problem...............................................................................................4

IPO Chart..............................................................................................................4

Input Statements.................................................................................................5

Output Statement................................................................................................5

Processing Statement..........................................................................................6

Designing the Program............................................................................................6

Pseudo code.........................................................................................................6

Implementation.......................................................................................................7

Testing.................................................................................................................... 7

Control Structures......................................................................................................9

Selection................................................................................................................. 9

Iteration................................................................................................................... 9

Sequence................................................................................................................9

Page 3: Computer Science Unit 1 Notes

Steps in compiling a code (compilation process)

There are four fundamental stages, or processes, in the creation of any program:

Editing Compiling Linking Executing

EditingEditing is the process of creating and modifying C source code.

CompilingCompiling is the process where the compiler converts your source code1 into machine language and detects and reports errors in the compilation process.

Compilation is a two-stage process. The first stage is called the preprocessing phase, during which your code may be modified or added to, and the second stage is the actual compilation that generates the object code2.

LinkingThe linker combines the object modules generated by the compiler from source code files, adds required code modules from the standard library supplied as part of C, and welds everything into an executable whole. The linker also detects and reports errors; for example, if part of your program is missing or a nonexistent library component is referenced.

ExecutingThe execution stage is where you run your program, having completed all the previous processes successfully.

1 Any input file into a compiler you produce during editing is known as source code.2 Any output from a compiler is known as object code and is stored in files called object files.

Page 4: Computer Science Unit 1 Notes

Figure 1 Creating and executing a program.

Page 5: Computer Science Unit 1 Notes

Steps in problem solving

The program development process is a series of activities that are necessary for the successful development of a computer program, however simple of complex. The program development process can be divided into six stages:

Defining the problem Designing the program Coding or Implementing the program Testing the program Installing and maintaining the program Documenting the program

Defining the ProblemBefore a program can be correctly solved, it must be clearly defined and understood by the programmer. Once the problem is clearly defined and understood the programmer can use the program development tools available to effectively plan a solution.

The problems should be carefully analyzed to extract the following information:

a) The input3 required by the program.b) The output4 or end result required.c) The process5 – a list of actions to be performed to achieve the desired

output.

IPO ChartProgrammers use IPO (Input, Processing, and Output) charts to organize and summarize the results of a problem definition. The Output is the first data added to the IPO chart, you can then ask what inputs the program need 3 Input is any information/data that is put into the program. E.g. it is like putting food into a refrigerator for storage.4 Output is any information/data that the program prints out or shows the user. E.g. it is like opening the refrigerator and seeing the food that you put in from input.5 Process is a list of actions to be performed to achieve the desired output.

Page 6: Computer Science Unit 1 Notes

to produce the required output. The next set of data to be added to the IPO chart is the input. Once you have determined the output you need and the input required to produce the output, the processing portion of the chart can be determined. Processing is simply the series of actions you must perform on the input to get the required output.

Suppose your problem is to build a house.

Our output is house Our input therefore are the materials needed for the house The processing is the series of activities that must be performed on the

inputs (materials) to build the house

The above steps clearly define the problem of building a house.

Input StatementsAn input statement will use the following rule:

Read VAR_NAME

This means to accept and input and store it in the variable VAR_NAME. For example:

Read Numb1

This means to accept an input and store it in the variable Numb1.

Output StatementAn output statement will use the following rule:

Print (“Phase”) or Print VAR_NAME

This means to print the phrase enclosed between the quotes or print the content of VAR_NAME. For example:

Print (“Hello World”) or Print Numb1

Where our phrase is “Hello World” and Numb1 is a variable.

Page 7: Computer Science Unit 1 Notes

Processing StatementFor processing, Our IPO chart will use the following convention:

VAR_NAME = Expression

Where expression is a formula or a value; For example:

Numb1 = 20

Sum = Numb1 + 30

are valid processing statements. Numb1 = 20, means store the value 20 in the variable Numb1. Sum = Numb1 + 30 means add 30 to the value in variable Numb1 and store the result in the variable Sum.

Designing the ProgramOnce we have clearly defined the problem, several programming tools are now available to assist the programmer in designing a solution for the problem. One should note that for a given problem, several solutions may exist. The programmer must examine the alternative solutions and choose the best one.

Some of the tools available to the programmer during the design phase are:

Algorithm Flowcharts Pseudo codes Decision tables Structure charts

The programmer can use some or all of the above tools in the design phase depending on the complexity of the problem to be solved. For the purpose of this text our main design tool will be pseudo code.

Pseudo codeAfter creation of an IPO chart, the next step in creating a solution is to develop a set of instructions for the computer, called pseudo code. A pseudo code is similar to an algorithm and sometimes the words are used interchangeably. A pseudo code is an informal programming language that is similar to the High level Programing Language. We use the term informal

Page 8: Computer Science Unit 1 Notes

because the rules and phrases used for pseudo code statements are subjective and depend on the style and preference of the programmer. The input, output and processing statements for pseudo code will follow the convention used by the IPO chart in the previous section.

ImplementationFor example; given the detailed design of a house, the work can begin. Each group of construction workers will need to complete its part of the project at the right time. Each stage will need to be inspected to check that it’s been done properly before the next stage begins. Omitting these checks could easily result in the whole house collapsing.

Of course, if a program is large and you are writing it all yourself, you’ll write the source code one unit at a time. As one part is completed, you can write the code for the next. Each part will be based on the detailed design specifications, and you’ll verify that each piece works, as much as you can, before proceeding. In this way, you’ll gradually progress to a fully working program that does everything you originally intended.

A large programming project usually involves a team of programmers. The project is divided into relatively self-contained units that can be allocated among the members of the team. This allows several units of code to be developed concurrently. The interface between one unit of code and the rest of the program must be precisely defined if the units are going to connect together as a whole.

TestingThe house is complete, but there are a lot of things that need to be tested: the drainage, the water and electricity supplies, the heating, and so on. Any one of these areas can have problems that the contractors need to go back and fix. This is sometimes an iterative process, in which problems with one aspect of the house can be the cause of things going wrong somewhere else.

The mechanism with a program is similar. Each of your program modules—the pieces that make up your program—will need to be tested individually. When they don’t work properly, you need to debug them. Debugging is the process of finding and correcting errors in your program. This term is said to have originated in the days when finding the errors in a program involved

Page 9: Computer Science Unit 1 Notes

tracing where the information went and how it was processed inside the computer by using the circuit diagram for the machine. The story goes that in one instance it was discovered that a computer program error was caused by an insect shorting part of a circuit in the computer. The problem was caused by a bug. Subsequently, the term bug was used to refer to any error in a program.

With a simple program, you can often find an error simply by inspecting the code. In general, though, the process of debugging usually involves using a debugger that inserts code temporarily for working out what happened when things go wrong. This includes breakpoints where execution pauses to allow you to inspect values in your code.

You can also step through a program a statement at a time. If you don’t have a debugger, adding extra program code to produce output that will enable you to check what the sequence of events is and what intermediate values are produced when a program executes. With a large program, you’ll also need to test the program modules in combination because, although the individual modules may work, there’s no guarantee that they’ll work together! The jargon for this phase of program development is integration testing.

Page 10: Computer Science Unit 1 Notes

Control Structures

Figure 2 Control Structures.

SelectionThe selection structure tests a condition, and then executes one sequence of statements instead of another, depending on whether the condition is true or false. A condition is any variable or expression that returns a Boolean value (TRUE or FALSE). E.g. IF-Else-Statements

IterationThe iteration structure executes a sequence of statements repeatedly as long as a condition holds true. E.g. Loop such as for, while and repeat

SequenceThe sequence structure simply executes a sequence of statements in the order in which they occur. E.g. GOTO Statements and Exit Statements