File - EXCEPTIONAL NEWS

advertisement
PROGRAMMING LANGUAGE 1
LECTURE 1
OBJECTIVES:
To gain experience with –
LECTURE:




The purpose of programming.
Declarative & Imperative knowledge.
Basic components to construct a programming language.
Data representation in computers using Binary number system.
LABORATORY:


Binary conversions.
Writing algorithm or steps of process in plain English or pseudo code or flow chart.
INTRODUCTION TO PROGRAMMING LANGUAGE:
Computer Science is not about computers or the tools only. It is more about the kind of
knowledge that Computer Science makes available to us.
In this course we will learn how Computer Science deals with a different kind of knowledge –
Imperative or “how to” knowledge. We will try to capture the notion of a process that causes
information to evolve from one state to another, and how different methods are used to capture
that knowledge.
Let us understand this notion through an example. To do that let us first understand about two
type of knowledge related to Science –
Declarative or “what is true” Knowledge: Talks about what is true. It makes statements of fact
that one can use to try to reason about things. For example, here is a statement of truth about
square roots.
√x = y such that y2 = x and y ≥ 0.
It provides a definition of a square root. As a consequence if someone were to hand us a possible
value for the square root of some x, we could check it by using this definition. For example, if
we are given, x = 4, then using this definition we have, 22 = 4, so y = √x = 2. But it doesn't tell us
anything about how to find the value of square root of x.
Imperative or “how to” knowledge: It tends to describe a specific sequence of steps that
characterize the evolution of a process by which one can deduce information. For example, here
is a set of steps to find an approximation to the square root of some number, x.
Step 1: Make a guess for the answer G (x/2 might be a good guess to start with).
Step 2: Improve the guess by averaging the G and x/G. Let’s call the improved guess, IG.
Step 3: If the improved guess IG is approximately equal to G or IG2 is approximately very close
to x, we have the answer IG.
Otherwise, we set G = IG and go to Step 2.
Let's test it out. Suppose we want to find the square root of 2. We will see how this sequence of
steps describes a process for finding the square root of 2. So here we go. We want to find the
square root of 2, so x = 2. And we start with some guess, say G = 1. Step 2 tells us how to
improve this guess. So we compute x/G and average G and x/G. And get a new guess, IG = 1.5.
Page 1 of 4
PROGRAMMING LANGUAGE 1
LECTURE 1
Step 3 tells us, the current guess IG = 1.5 is not close enough to the previous guess G = 1 or
square of our current guess IG2 = 2.25 is too far away from x = 2. So we decide we are not close
enough and continue at Step 2 by G = IG = 1.5. By continuing the steps we get the following –
√x for x = 2
x=2
x/G = 2/1 = 2
G=1
IG= ½ (G+(x/G))
IG2= (3/2)2 = 2.25 ≠ x = 2;
= ½ (1+2)=3/2
So, G=IG=3/2.
x/G= 2/(3/2)=4/3
IG= ½ (G+(x/G))
IG2= (17/12)2 ≈ 2.00692 ≠ x = 2;
= ½ (3/2+4/3)=17/12
So, G=IG=17/12.
x/G=2/(17/12)=24/17 IG= ½ (G+(x/G))
IG2= (577/408)2 ≈ 2.00001≈ x = 2;
= ½ (17/12+24/17)=577/408 So, our answer is 577/408= 1.41421.
Eventually we get a value for the guess that is close enough, and we stop.
Notice how this sequence of steps deduces/transforms some new information from a set of facts.
These steps of the process is known as algorithm. It tells us "how to" do something. Compare
this with the case of the declarative or "what is" version. It simply told us how to recognize a
square root if we saw one, but nothing about how to find one.
So what we have seen is that imperative and declarative knowledge are very different. One
captures statements of fact; the other captures methods for deducing information. It is easy to see
why the latter is more interesting.
Thus, we are primarily interested in "how to" knowledge -- we want to be able to give the
computer instructions to compute a value, and this means we need a way of capturing the "how
to" knowledge. In particular, we want to describe a series of specific, mechanical steps, known as
algorithm, to be followed in order to deduce a particular value associated with some problem,
using a predefined set of operations. This algorithm for describing "how to" knowledge we call a
procedure. The actual sequence of steps within the computer that cause the "how to" knowledge
to evolve is called a process. Much of our focus during the term will be in understanding how to
control different kinds of processes by describing them with procedures using algorithm.
COMPONENTS:
Now we want to create tools that make it easy for us to capture procedures and describe
processes, and for that we will need a language. Whatever language we choose to use to describe
computational processes, it must have several components.
First, it will have a vocabulary -- a set of words on which we build our description. These will
be the basic elements of computation, the fundamental representations of information and the
fundamental procedures that we use to describe all other procedures.
Second, it will have a set of rules for legally connecting elements together -- that is, how to build
more complex parts of a procedure out of more basic ones. This will be very similar to the
syntax of a natural language.
Third, it will have a set of rules for deducing the meaning associated with elements of the
description. This will be very similar to the semantics of a natural language.
And finally, we will need procedures, that is, standard ways of combining expressions in our
language together into a sequence of steps that actually describe the process of computation.
Page 2 of 4
PROGRAMMING LANGUAGE 1
LECTURE 1
We will see is that our language for describing procedures will have many of the same features
as natural languages, and that we will build methods for constructing more complex procedures
out simpler pieces in natural ways.
REPRESENTING INFORMATION:
We need to understand how we are going to representation information, which we will use as the
basis for our computational processes. To do this, we need to decide on representations for
numeric values and for symbolic ones. Let’s start with numbers. To represent a number, we start
with the most atomic element. Because ultimately we will represent information internally inside
the computer, we will use electronic signals to do so. These are most conveniently represented
by using a high voltage or current or a low voltage or current to represent fundamental values.
Thus, the most primitive element in representing a value is a binary variable, which takes on one
of two values: a zero or a one. This variable represents one bit, or binary digit, or information.
Of course, we need to group these bits together to represent other numbers, which we do
typically in groupings of 8 bits (a byte) or in groupings of 16, 32, or 48 bits (a word). Once we
have sequences of bits, we can use them not only to represent other numbers, but we can
envision encodings, in which numbers or bit sequences are used to represent characters. And
characters can further be group together to represent symbolic words. There are standard
encoding schemes for using bit sequences to represent characters as well as numbers (ASCII).
Explanation of binary representations for numbers and operations to manipulate them. Binary to
Decimal and Decimal to Binary conversion.
Relation of ASCII character Set with binary representation. Explanation of UNICODE.
LABORATORY EXERCISES:
1. Students will be given problems to solve by writing down the operations in steps-pseudo
code. Teacher may also choose Flow Chart technique. Using Pseudo code or Flow chart to
describe the concept of a program – INPUT, PROCESS, and OUTPUT. Describe the concept
of variable in very brief just to understand the variables for writing the steps or flow chart.
May use the problem Square Root done in the lecture to describe.
Write down steps to find 2. The largest number between two given numbers.
Step 1:
Start;
Step 2:
Input two numbers- n1 and n2;
Step 3:
If n1 > n2 then Output n1; go-to Step 5;
Step 4:
(Otherwise) Output n2;
Step 5:
Exit.
3. The largest number among three given numbers.
4. If a given year is Leap-year.
5. If a given number is even or odd.
Step 1:
Start;
Step 2:
Input a number- n;
Step 3:
If (n modulo 2) = 0 Output “EVEN”; go-to Step 5;
Step 4:
(otherwise) Output “ODD”;
Page 3 of 4
PROGRAMMING LANGUAGE 1
Step 5:
LECTURE 1
Exit;
6. Sum of all the even numbers from 1 to a given number.
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Step 7:
Step 8:
Start;
Input a number- n;
Take two numbers- s and k. s 0; k  1;
If k > 100 then go-to Step 7;
(otherwise) If (k modulo 2) = 0 then s  s + k;
k  k + 1; go-to Step 4;
Output s;
Exit.
7. If a given number is Prime.
Step 1:
Start;
Step 2:
Input number- n;
Step 3:
Take a number- p; p  2;
Step 4:
If p ≥ n then Output “PRIME”; go-to Step 7;
Step 5:
(otherwise) If (n modulo p) = 0 ; Output “NOT PRIME”; go-to Step 7;
Step 6:
p  p + 1; go-to Step 5;
Step 7:
Exit.
8. Sum of all the Prime numbers from 1 to a given number.
9. Sum of all the digits of a given number.
10. Reverse a given integer. [Example: Input: 123; Output: 321]
Step 1:
Start;
Step 2:
Input an integer- n.
Step 3:
Take three numbers- s, m, and p; s 0; p 1;
Step 4:
If n = 0 then go-to Step 9;
Step 5:
m  (n modulo 10);
Step 6:
s  (s × p) + m;
Step 7:
p  p × 10;
Step 8:
n  n / 10; go-to Step 4;
Step 9:
Output s;
Step 10: Exit;
11. If a given number is palindrome. [Example: 123321 is a palindrome as it is same after
reversing].
Page 4 of 4
Download