Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer...

21
Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in a computer. We provide an introduction to programming languages and discuss some of the features of the C programming language. We also outline the steps involved in creating a C program. 1.1 Computer Hardware A typical computer system consists of two parts: 1. Hardware

Transcript of Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer...

Page 1: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

Chapter 1

Introduction to Computers

In this chapter we describe the major components of a computer system and explain how information is represented in a computer. We provide an introduction to programming languages and discuss some of the features of the C programming language. We also outline the steps involved in creating a C program.

1.1 Computer Hardware

A typical computer system consists of two parts:

1. Hardware

2. Software

Page 2: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

All computers are similar in that they have four components.

1. Central processing unit (CPU)

2. Primary storage

3. Secondary storage

4. Input and output (I/O) devices

CPUInputdevice

Outputdevice

Secondarystorage

Page 3: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

1.1.1 Central Processing Unit

The central processing unit is the heart of the computer system. It monitors and controls the operation of the other devices and the flow of information to and from these devices, and it performs all the necessary manipulations of the data. The central processing unit can be subdivided into three submodules:

1. Control unit

2. Arithmetic logic unit (ALU)

3. Primary storage (main memory)

1.1.2 Primary Storage

Primary storage, also called “main memory”, is used to store information for immediate access by CPU.

Page 4: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

1.1.3 Secondary Storage

Most computer systems have secondary storage devices which are used to provide additional data storage capability. Secondary storage is necessary because of the limitations of primary storage.

The data in secondary memory is not directly accessible by the CPU but must be first transferred to main memory before processing. Secondary memory storage is considerably less expensive than main memory but requires significantly longer access times.

1. Floppy disks

2. Hard disks

3. Zip disks

Page 5: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

1.1.4 Input and Output Devices

Input and output devices allow us to communicate with the computer system. They include devices that enter information into the computer and devices that display information stored in the computer’s memory.

1. Keyboard

2. Monitor

3. Printer

1.2 Programming Languages:….Low level and High level

1.2.1 Machine and Assembly Languages

For a computer to perform any task it has to given a series of specific instructions in a language that it can understand.

Page 6: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

1. Machine language

2. Assembly language

3. Mnemonic code

1.2.2 High Level Languages

High level languages are geared more toward the people writing the programs rather than the computer. These languages provide the interface between the user and the machine. They are closer to English but are sufficiently rigorous to permit the computer to translate the programs written in the high level language into machine language.

CompilerSource Code Object Code

Page 7: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

1.3 The C Programming Language

The C programming language was developed in the early 1970s by Dennis Ritchie, a system software engineer at AT&T Bell Laboratories. C evolved from a language named B that was developed by Ken Thomson.

1.4 Creating a C Program

The translation of programs written in a high level language such as C into machine code is accomplished by means of a special computer program called a “compiler”. The compiler analyzes a program written in a language such as c and translates it into a form that is suitable for execution on the particular computer system. There are several steps that you have to perform to create a C program.

Page 8: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

Text editor

C source file

C compiler

Object code

Linker

Executed file

Syntax errors

Header file

Library functions Other object

Page 9: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

1.5 Structure of a C Program

Most C programs contain the following basic elements The structure of a typical C program is shown as follows.

1. Preprocessor statements

2. Global declarations

3. Function prototypes

4. Functions

Preprocessor statement represent instructions to the C preprocessor. The C preprocessor is a program that analyzes source code before passing it on to the compiler. All preprocessor statements begin with the # symbol.

Page 10: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

Preprocessor statements

Global declarations

Function prototypes

manin ()

{

}

function1()

{

}

function2()

{

}

Page 11: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

1.6 A Simple C Program

r

h

Volume = (pi) r2h

Surface area =

2(pi)rh + 2(pi)r2

The first few lines of the program which begin with /* and end with */ are comments. Comments are remarks which help clarify a program. Comments are ignored bye the compiler.

Page 12: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

/********************************************************************************************/

/* cylinder.c */

/* computes the volume and surface area fo acylinder */

/********************************************************************************************/

#include <stdio.h>

#define PI 3.14159 preprocessor statements

void main (void); function protype

void main(void) function header

{

float radius, height, volume, surface_area; variable declarations

printf(“\n Cylinder.c”);

printf(“\n Computes volume and surface area of a cylinder.”);

printf(“\n\n Enter radius of cylinder: “);

scanf(“%f”, &radius);

printf(“ Enter height of cylinder: “);

scanf(“%f”, &height);

volume = PI * radius * radius * height;

surface_area = 2.0 * PI * radius * (radius + height);

printf(“\n Volume of cylinder is: %10.4f”, volume);

printf(“\n Surface area of cylinder is: %10.4f”, surface_area);

} end body of function main()

Page 13: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

Simple input and output

Cylinder.c

Computes volume and surface area of a cylinder.

Enter radius of cylinder: 10

Enter height of cylinder: 20

Volume of cylinder is : 6283.1860

Surface area of cylinder is: 1884.9558

Page 14: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

1.7 Functions

int square (int a)

type of value function name parameter list

returned

type name (parameter list) function header

{

… function body

}

Page 15: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

1.8 The printf( ) Function

printf(“\n Computes volume and surface area of a cylinder.”);

The printf ( ) function is a powerful output function that allows us to display information on the standard output screen. The general format of the printf( ) function is:

printf(“control string”, argument list);

Example

printf(“\n Volume of cylinder is: %10.4f”,volume);

format specifier

Page 16: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

Format Specifier Data Type

%c character

%d decimal integer

%f floating point

%s string of characters

1.9 The scanf ( ) Function

The scanf ( ) function is a general input function that is provided as part of the standard C library. The format of the scanf( ) function in similar to the printf( ) function:

scanf(“ control string”, argument list);

Page 17: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

Example: Linear Interpolation

problem statement: The equation of a straight line is

y = mx + c

Where m is the slope and c the y-intercept. If the coordinate of two points P1 and P2.

y

x

Page 18: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

(x1,y1) and (x2,y2) are coordinates of p1 and p2, respective. And the slope of the line is

m = (y2-y1)/(x2-x1)

c = (y1x2 - y2x1)/(x2-x1)

Write a program to compute the slope and intercept of straight line given the coordinates of two points on the straight line. The program should also compute the y coordinate of a point on the straight line, given the x coordinate of the point.

Page 19: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

#include <stdio.h>

main()

{

float x1, y1,x2, y2; /* coordinate of two points on line */

float m, c; /* slope and intercept of line */

float x, y; /* coordinates of point to be interpolated */

printf(“\n\nEnter x and y coordinates of first point “);

scanf(“%f %f”, &x1, &y1);

printf(“\nEnter x coordinates of pointf to be interpolated”);

scanf(“%f”, &x);

m = (y2 - y1)/(x2-x1);

c = (y1*x2 - y2*x1)/(x2 - x1);

printf(“\n\nSlope of line = %f”, m);

printf(“\nIntercept of line = %f”, c);

printf(“\nY coordinate of point = %f”, y);

}

Page 20: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

Enter x and y coordinates of first point 10.0 10.0

Enter x and y coordinates of second point 20.0 20.0

Enter x coordinate of point to be interpolated 15.

Slope of line = 1.000000

Intercept of line = 0.000000

Y coordinate of point = 15.000000

Page 21: Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.

Any Questions