Introduction to Computing

Post on 06-Jan-2016

21 views 0 download

description

Programming is an activity that is carried out through some programming language. - PowerPoint PPT Presentation

Transcript of Introduction to Computing

Introduction to Computing

Programming is an activity that is carried out through some programming language.

Programming is instructing the computer how to solve a computational problem.

In a computational problem, there will be some input, and for each input there is some desired output. A program instructs how to obtain the desiredoutput from the given input.

Notion of a program

A program is a sequence of instructions to the computer.

For example, a program for adding two numbers may be that sequence of instructions which tells the computer how to carry out the algorithm for addition we had learnt in school.

Given any two numbers to be added, the computer executes the program to find the result.

Here the input is the two numbers, and the output is their sum.

Other examples

A more complex example of a computational problem is the monthly pay-bill computation.

Here, the input is various data about the employees of a firm--- their attendance record, overtime record, salary, overtime rate etc.

The output is the amount to be paid to each employee.

The program bodies the method of computing monthly salary.

A program is a series of easy to follow steps: A computer knows how to carry out some simple instructions.Let us explain this idea by an example. Suppose we have a computer that knows how to carry out

the instruction

drawline (x1, y1, x2, y2)

(x1, y1) and (x2, y2) are co-ordinates of any two points on the screen.

This instruction makes the computer draw on the screen a straight line from point (x1, y1) to point (x2, y2).

Complex tasks from simple tasks: The four instructions below

drawline ( 0, 0, 0,10) drawline ( 0,10,10,10) drawline (10,10,10, 0) drawline (10, 0, 0, 0)

when executed, will draw a square on the screen.Thus by performing a series of simple instructions,

each drawing a straight line, we have managed to get a more complex task, drawing a square, done.

Suppose a program has the variables x, y, z, and tp.Let us say that these variables have values 5, 3, -9, and 50.Consider

2 * ( x + y ) + tp This is an expression, and it evaluates to a value, in this case, its value is 66. A fundamental instruction is assignment which assigns the value of an expression to a variable.The general form of assignment instruction is

variable= expressionwhich tells the computer to first evaluate expression to get a value, and then assign this value to the variable.

Expressions, assignment

Sequencing of instructions is important

Remember that executing a program is executing a sequence of instructions, which is what the program is.

A program starts its execution by first executing the first instruction, then the second instruction, and so on.

Execution of the program ends when there are no more instructions left to be executed.

The order of sequencing of instructions is important. For example, the effect of two sequences.

Sequence 1 y = x + y

x = x + 1

Sequencing is important: cont'dand

Sequence 2 x = x + 1 y = x + y are different, even though both have the same two

assignments. To see this, suppose we start with the case: x has 3, y has 5.

Then effect of sequence 1 is:x has 4, y has 8.

But the effect of sequence 2 will result in: x has 4, y has 9.

Other kinds of instructions There are instructions which do something if some condition

holds.

For example, if ( x == y ) z = z + 1 The effect of this instruction is to increment z by 1 only if x and y both have the same value, otherwise no value gets changed. An example of another form of such a conditional instruction is if ( x == y ) z = z + 1 else z = z - 1

This instruction increments z by 1 if x and y have same value, else it decrements z by 1.

Expressions evaluating to true/false

In the above ( x == y )

is also an expression; its value is, however,not a number but true or false.

Such expressions are called boolean expressions

Iterative Instructions

In a program, some action might need to be repeated till some condition is met. Iterative instructions help us in achieving this. One important instruction in this class has the form while( B ) S

where B is a boolean expression, and SS is a sequence of one or more instructions. On reaching such a statement, first B is tested, if false nothing gets done. But if B is true, S is done, and then B is tested again. If false, the work is over, otherwise SS is done again, and so on. SS gets done repeatedly till B becomes false.

Example

Here is a `program', using `instructions‘that we have seen. a = x

b = y while( a != b ) if( a > b ) a = a - b else b = b - a

How do we understand what this program is doing? We can see that there are four variables, x, y, a, and b, and the instructions modify a and b.To understand what the program does, we need to observe how the values of the variables change.

When to observe

To see changes, we would like to observe the values of variables just before and just after each instruction.

But after an instruction, usually, another instruction follows.

Therefore, we observe the variables just before an instruction is about to be executed.

Example: cont'd

Observation time

Where in program

x y a b

1 Before 1st instn. 4 6 * *

2 Before 2nd instn 4 6 4 *

3 About to test a!=b 4 6 4 6

4 About to test a>b 4 6 4 6

5 About to test a!=b 4 6 4 2

6 About to test a > b 4 6 4 2

7 About to test a!=b 4 6 2 2

When both a, b have the same value, (here it was 2), the test in while instruction evaluates to false. That ends the while instruction. After this, our example program had no other instructions to execute, therefore, the execution of the program ends.

What we have done is to hand-execute the program. The final value of a in this case is 2.

Had we started with x as 12 and y as 8, the final value of a would have been 4.

In general, the final value of a will be the gcd of x and y, provided both are greater than 0.

Therefore, the program is doing something useful! To compute gcd of x and y, we can execute the program, and print the final value of a as the result.

Example: Cont’d

Examples of Problems

• Now, we shall discuss some problems which is to be solved using the computers.

• We shall design a method to solve the problem, and then implement the method using high level programming language constructs.

17

Checking caseclass upperCaseCharacter{ public static void main(String arg[]){ char c=‘0’; boolean isuppercase, islowercase; isuppercase = ((c >= ‘A’) && (c <= ‘Z’)); islowercase = ((c >= ‘a’) && (c <= ‘z’)); System.out.ptintln(“The character is ” + c

+ “. Uppercase: ” + isuppercase); }

}

18

Digits in APclass digitsInAP{ public static void main(String arg[]){ int x = 121; int d0, d1, d2; boolean isAP; d0 = x % 10; d1 = (x/10) % 10; d2 = x/100; isAP = ((d0+d2) == (2*d1)); System.out.println(“The number is: ” + x + “.

Digits in AP? ” + isAP); }}

19

Divisibility by 4class divisibleByFour{ public static void main(String arg[]){ int x = 121; int lastTwoDigits; boolean isdivBy4; lastTwoDigits = x%100; isdivBy4 = ((lastTwoDigits % 4) == 0); System.out.println(“The number is: ” + x +

“. Divisible by 4? ” + isdivBy4); }}

20

Exchanging numbersclass swap1{ public static void main(String arg[]){ int x = 121; int y = 200; int temp; System.out.println(“x: ” + x + “, y: ” + y); temp = x; x = y; y = temp; System.out.println(“x: ” + x + “, y: ” + y); }}

21

Exchanging numbersclass swap2{ public static void main(String arg[]){ int x = 121; int y = 200; System.out.println(“x: ” + x + “, y: ” + y); x = x + y; // Could overflow y = x – y; x = x – y; System.out.println(“x: ” + x + “, y: ” + y); }}