24
Introduction to Com puters and Program ming O bjectives Com puters Being able to : D istinguish hardw are from softw are Identify m ain com ponentsofa com puter D efine the generalfunction ofeach part Program m ing Being able to : Recognize program m ing asonly a partofthe processofdeveloping a solution to a problem . Analysis . D esign . Implementation . Testing U nderstand the stepsin com piling and executing aprogram Relate the processofcom piling and executing to the hardware and softw are com ponentsused in each step.

Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Embed Size (px)

Citation preview

Page 1: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Introduction toComputers and Programming

ObjectivesComputers

Being able to : Distinguish hardware from software Identify main components of a computer Define the general function of each part

ProgrammingBeing able to : Recognize programming as only a part of the process of developing a

solution to a problem. Analysis. Design. Implementation. Testing

Understand the steps in compiling and executing a program Relate the process of compiling and executing to the hardware and

software components used in each step.

Page 2: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

How much intelligence does a computer have?

LIKE A CALCULATOR!

If you were to solve the problem using a calculator, howmuch of the work would the calculator do for you?

NOT MUCH

Have you ever tried to explain a complicated process to aperson who you perceived to be slow to comprehend?

COMPUTERS ARE WORSE

Conclusion:As a programmer your job is to use the SIMPLE capabilities of a computer tosolve complicated problems.

It is not an easy task!

Page 3: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

First Step:

Understand the Problem

How do you know if you understand?

Can you solve it by hand (with enough time)? 1. Create an example for which you know the answer. 2. Apply your approach 3. See if the answer looks right

It is like the scientific method 1. Propose a theory 2. Test the theory 3. If the example appears to disprove the theorem (shows your

approach is not correct), then modify your theory(approach) andtry again!

Remember!You have to describe EVERY step in your approach.

Page 4: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Second Step:Formulate (state) your solution

Third Step:Express the solution in a language

the computer can understand(c++ for us)

Fourth Step:Test your approach

As if it’s not hard enough, remember you are also using aNEW LANGUAGE to describe what to do!

Steps 1,3 and 4 are the easiest.Step 2 is very difficult!

Page 5: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Common Errors

1. Misunderstanding the problem

2. Having an error in your approach

3. Stating your approach incorrectly in c++

4. Typing wrong

Finding Errors

1. Look for the obvious first

2. Consider the kinds of mistakes you’ve made before(coaches and their players)

3. Isolate the parts of a system ( what if your stereo system fails)

Page 6: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Problem Solving

1. Look for solutions to similar problems and try to adapt thesolutions to your problem

2. A problem requiring a solution twice as long is four times ascomplicated

3. Use techniques we will show in class to manage the complexityof the problems

structured programming top-down design object-oriented design

These skills will take YEARS to develop!

Page 7: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

You are learning a new language!

1. Be CAREFUL to avoid being ambiguous(saying something with multiple interpretations)

Examples:A person sitting at the desk is instructed to stand(we assume to turnfirst).

2. Be specific

Example:A parent tells the child, “You can’t have that candy”

Try to avoid being ambiguous and try to be specific.

3. Remember that ENGLISH is inherently ambiguous. You have been trainedto interpret in the proper context to eliminate ambiguity. Don’t ASSUME ina programming language. Programming languages are precise to avoid theseambiguities.

Page 8: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Language Evol uti on

Machine Language and Assembly Language

Machine-Language Version Assembly-Language Version013737 000016 000022 GO: MOV NMBR1, SUM063737 000020 000022 ADD NMBR2, SUM000000 HALT000100 NMBR1: .WORD 100000150 NMBR2: .WORD 150000000 SUM: .WORD 0

.END GO Machine- and assembly language versions of PDP-11 addition program

Too error-prone. Takes too long to understand it.

Higher Level Language (c++)

Much More Natural !

#include <iostream.h>

void main( ){

int NMBR1, NMBR2, SUM;NMBR1 = 100;NMBR2 = 150;SUM = NMBR1 + NMBR2Cout << SUM;

}

Page 9: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

HOW I S A COMPUTERORGANI ZED?

Basic Structure has remained the same over 40+ years

INPUT -> PROCESS -> OUTPUT

temperature -> decide whether -> signal to control to turn a/c on/off

class requested -> select and print -> a report (listing)course enrollments students in class

MainMemory

CentralProcessorUnit

Keyboard

InputDevice

SecondaryMemory

Monitor

Printer

Output Devices

Page 10: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

MEMORY - (data and instructions)

1. Each cell has a VALUE and ADDRESS

2. One value at a time

3. Move a COPY to a new location

4. Size of memory varies

byte = 8 bits

word = multiple of bytes

usually transparent to you (not important here)

Page 11: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

MEMORY - (data and instructions cont.)

Computer Memory with 1000 cells

Words have multiple BYTESBYTES have multiple (8) BITS

Address Content0 -27.21 3542 0.0053 -264 H. .. .. .

998 X999 75.62

Memory 4 BYTES0 1 2 3

8 BITS0 1 2 3 4 5 6 7

Page 12: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Central Processing Unit

I/O Devices

Secondary MemoryDisk or Tape

Slower

Larger -> Reasons for different types

Cheaper

Page 13: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

What do Computers Do?(example like p.8 of notes)

1. Fetch instruction (add)

2. Decode instruction

3. Fetch operands (NMBR1, NMBR2)

4. Perform operation (NMBR1 + NMBR2)

5. Store results (put sum in location for Sum)

REPEAT

In other words - they always(only) run programsWhat happens when you turn it on?

Runs bootstrap program

Bootstrap program loads OS (unix)

Unix loads other programs (including “shell”)

“shell” will

read commands

load program

run program

wait for the next command

Page 14: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

What does “unix” do for you?

1. Manage resources

Who runs and how long

Who uses devices such as disk

2. Provide Protection

Who uses machine

Users only have access to what they need

3. Provide programs you need

Compilers

Building programs

File utilities (create, delete, rename, etc)

Printing utilities

Your lab instructor will show you how to use unix

Page 15: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Entering, Compiling and Running a Program

1. Use an editor (like “emacs”) to

enter the program and save it on

the disk.

2. Use a compiler (like “g++”) to

translate the program into machine

language

3. Link libraries with your program

(auto)

4. Execute the machine language

version (like “emacs”, “g++” or

“a.out”)

You enter the program andsave it as a source file

Source fileon disk

The compiler attemptsto translate the program

Revised

source

file

You correct syntax errors

List of Errors

Otherobject files

Newobject

file

The linker links the new objectfile with other object files

Loadfile

Loader places the loadfile into memory

Executableprogram in memory

Page 16: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

MODELS for learning

The process of learning about computers is one of building a MENTAL MODEL of what happens.

As you become more knowledgeable about computers your model will become more sophisticated and more accurate.

Accept the limitations of your models, but question your instructor if you have any suspicion that your model of what is happening is wrong.

Language Problems

Realize that English itself is very INaccurate. YOU make it reasonable by associating the MOST REASONABLE interpretation to the statements.

Learning to be precise with c++ will take practice!

Page 17: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Overview of c++

Objectives

1. Recognizing the non-linear nature of program complexity the need for simple interfaces that OOP/OOD is a long-term goal

2. Understanding that obtaining a program that “works” is insufficient. Style is an important part of programming.

Page 18: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Complexity of Software

Size of Program

Complexity

LINEAR NON-LINEAR

1. Stay in this area2. Break large systems into small pieces3. Interfaces of the pieces become a problem

Page 19: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Tools

Building your program from good tools (software) makes your job less complicated

REUSABILITY of code has been historically LOW

OOP/OOD increases REUSABILITY!

First you need to learn to use existing toolsto build your solutions.

Later you will learn to build your own tools.

Page 20: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

OOD (Object-Oriented Design)OOP (Object-Oriented

Programming)

The DESIGN process is fairly independent of the

language as long as the language is object-oriented.

C++ is used in our course because of its

widespread acceptance

support

not necessarily because it is the best OOP language.YOU WILL

FIRST learn structured programming techniquesSECOND use existing objects to gain familiarityTHIRD learn to build your own objects

Page 21: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Sample c++ program. Version 1.

Convert Square Meters to Square Yards.

(INPUT ->) PROCESS -> OUTPUT Calculate the answer Output

the answer#include <iostream.h>

void main ()

{

cout << 20 * 1.196; //20 SqMeters to SqYards

}

Problems

1. Always uses the same numbers (solves the same problem). NOT GENERAL!

2. Only numbers on output! NEEDS LABELLING!

3. Observer does not understand what we're doing!

NEEDS DOCUMENTATION!

drake> g++ version1.C

drake> a.out

23.92

drake>

Page 22: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Same Program. Version 2.#include <iostream.h>

void main()

{ // declare variables and constants

const float meters_to_yards = 1.196;

float size_in_sqmeters;

size_in_sqmeters = 20;

float size_in_sqyards;

// calculate square yards

size_in_sqyards = size_in_sqmeters * meters_to_yards;

// output square yards

cout << "The size in square yards is ";

cout << size_in_sqyards << "\n";

}

drake> g++ version2.C

drake> a.out

The size in square yards is 23.92

drake>

GOOD -> Provided DOCUMENTATION and LABELLING

BAD -> Still NOT GENERAL

Page 23: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Version 3.#include <iostream.h>

void main()

{ // declare variables and constants

const float meters_to_yards = 1.196;

float size_in_sqmeters, size_in_sqyards;

// read size in square meters

cin >> size_in_sqmeters;

// calculate square yards

size_in_sqyards = size_in_sqmeters * meters_to_yards;

// output square yards

cout << "The size in square yards is ";

cout << size_in_sqyards << "\n";

}

drake> g++ version3.C

drake> a.out

20

The size in square yards is 23.92

drake> a.out

10

The size in square yards is 11.96

drake>

STILL NEED to PROMPT USER!

Page 24: Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices

Version 4.#include <iostream.h>

void main()

{ // declare variables and constants

const float meters_to_yards = 1.196;

float size_in_sqmeters, size_in_sq_yards;

// read size in square meters

cout << "Enter the number of square meters ->";

cin >> size_in_sqmeters;

// calculate square yards

size_in_sqyards = size_in_sqmeters * meters_to_yards;

// output square yards

cout << "The size in square yards is ";

cout << size_in_sqyards << "\n";

}

drake> g++ version4.C

drake> a.out

Enter the number of square meters -> 20

The size in square yards is 23.92

drake> a.out

Enter the number of square meters -> 10

The size in square yards is 11.96

drake>

DONE!