Variables

Filed under , on Sunday, January 31, 2010

0

When an application is to handle data, it needs some place where it can temporarily store this data. This “place” where the application stores data is called the memory.
An application will handle more than one piece of data. Thus, the application has to allocate memory for each piece of data. In allocating memory there are two factors to be considered:
•    How much memory is to be allocated ?
•    Remembering where in memory is each piece of data stored.
Earlier programmers had to write their programs in the language of the machine that is, in 1s and 0s. If the programmer wanted to temporarily store a value, the exact storage locations inside the memory of the computer had to be assigned. This storage location had to be a specific number or memory address.
Modern day languages enable us to use symbolic names known as variables, to refer to the memory location where a particular value is to be stored.
The type of data decides the amount of memory to be allocated. The names that we assign to variables to store a particular data help us in retrieving this data as and when required.
We are familiar with the use of letters, which represent quantities in a formula. For example, the area of a rectangle is given by:
    Area = A = Length x Breadth = L x B
    The simple interest is given by:
    Interest = I = Principal x Time x Rate / 100 = P x T x R / 100
The letters A, L, B, I, P, T and R are all variables and are short notations that represent various values.
To appreciate the significance of variables, consider the following example:
    The sum of the marks obtained by 5 students is to be displayed.
    The sum can be displayed with the help of the following instruction.
    Display the sum of 24, 56, 72, 36 and 82.
Once the sum is displayed, it is lost from the computer’s memory. Suppose we want to calculate the average of the marks, the sum would have to be recalculated.
A better approach will be to store the result in the computer’s memory, and retrieve it as and when required.
    sum = 24 + 56 + 72 + 36 + 82
Here, sum is a variable that is used to store the sum of the five numbers. To calculate average, it can be done as follows:

Pseudo Code

Filed under , on Wednesday, January 27, 2010

2

Pseudo Code is a sequence of lines similar like programming code and doesn’t have exact rules for writing syntax. Pseudo Code is a different way to implements algorithms beside Flowchart. Pseudo Code is more commonly used by experienced programmers. While Flowchart is more understandable for beginners, Pseudo Code supports many programming features and easy to implement into real programming code rather than Flowchart.
There are no exact rules for Pseudo Code. We can write our Pseudo Code freely as long as it is easy to understand for other persons. But it is suggested to use common used keywords in programming tool (i.e. if, then, else, while, do, repeat, for and etc.) and follow certain programming style (i.e. Pascal, C++, etc.). See the code below:
   
    Program Start
    Read a number
    Read a number, store it and write it
    Get the number before and write it
    Repeat
    End Program
Although Pseudo Code above is still understandable but some statements is ambiguous. From second to forth lines, we don’t know where the number currently read store into, and we also don’t know which number is exactly for “the number before”. Is it the first number read or the second one ? And for “Repeat”, we hardly tell which lines are supposed to repeat over. Pseudo Code above is modified to a better one as follows:
   
    Start
    Repeat
       Read Number1
       Read Number2
       Display Number2
       Display Number1
    Until Number1=0
    End
Pseudo Code above is better than previous one. We clearly see where the values are stored into and which values are displayed. Repeat statements will be discussed in Week 3.
   

Connectors (Flowchart Part 4)

Filed under , on Tuesday, January 26, 2010

0

In preparing flowcharts for complex problems:
•    The flowchart may not fit in a single page.
•    It may be difficult to interconnect all boxes directly.
In such cases, flowcharts can be broken into parts and connectors can be used to indicate the location of the joins. If the example in Figure 4 is split it will be as shown in Figure 9.
A unique number is specified within the connector and an arrow is drawn into it at the point where the chart is broken. Another connector with the same number and an arrow pointing away from it is drawn at the point where the broken chart is to be joined again.
Connectors are inserted at the point where the flowchart splits. However, the location of each join is clearly indicated by the corresponding number.
Tips for Flowchart   
The following points are to be remembered while drawing a flowchart:
•    Initially concentrate on the logic of the problem and draw the main path of the flowchart.
•    After the main part is completed, add all the branches and loops.
•    A flowchart can have only one Start point and one Stop point.
•    As far as possible, keep the flowchart machine independent by not using terms associated with the computer.
•    It is not necessary to represent each and every step of a program in the flowchart. Use steps that are meaningful.
•    Use descriptive terms that represent the logic of a problem. Do no use ambiguous terms.
•    Remember that another user or programmer should easily understand the flowchart.

Figure 9


Looping (Flowchart Part 3)

Filed under , on Monday, January 25, 2010

1

Looping refers to using one or more steps repeatedly.
Loops are of two types:
•    Fixed
•    Variable
Consider the following examples to understand the two different types of looping:
Example 4
To calculate the sum of monthly expenditure for an entire year, the flowchart is as follows in Figure 6. Figure 6 illustrated a fixed loop because the total number of months in a year is 12. Thus, this loop cannot be executed for more than 12 times.
We consider the expenditure of one month at a time and add it to the total expenditure. After the expenditure of each month is added to the total expenditure, we examine whether the number of months is equal to 12.

Figure 6


If ‘Yes’ then there are no more months left and we display the total expenditure and stop. If ‘No, we accept the expenditure of the next month.
Since the number of the months in a year is fixed, this loop cannot be executed more than 12 times. Hence, this is an example of a fixed loop.
Example 5    
Another example, a survey is carried out in a town. Information such as the name, sex, age etc. of each person is available. To maintain a list of people aged 50 and above in the town we draw a flowchart as follows:
Figure 7

Figure 7 illustrates a variable loop as we do not know the exact number of people in the town. This loop continues till the name and age of the last person in the town is read.
Given below is the basic flowchart for a loop.
Figure 8

Loops are fixed if operations are repeated a fixed number of times. The values being computed or handled inside the loop have no effect on the number of times the looping operation is done.
Variable loops are the ones where the operations are repeated until a specified condition is met. The number of times that the loop is repeated may vary.

Branching (Flowchart Part 2)

Filed under , on Monday, January 25, 2010

1

Branching is the process of following one of two of more alternate paths of computations.
To understand branching, consider the following examples:
Example 2   
We want to test if a number is odd or even. To accomplish this, we draw a flowchart following the steps given in the algorithm. This flowchart is shown in Figure 3.

Figure 3




Example 3   
To choose the largest of three distinct numbers a, b and c, the flowchart will be drawn as shown in Figure 4.

Figure 4

Thus, branching is used in a flowchart when we want to represent the testing of a condition. A particular path is selected based on the result of the test. The condition could be a comparison of positive or negative values.
The general form of the flowcharts involved in branching are as follows:

Figure 5

In Figure 5(i), one branch consists of one or more computational steps and the other branch is without any computations. Both the branches join the main program at the end.
In Figure 5(ii), after the decision stage, each branch consists of one or more computational steps. The two branches may then join the main program or take a different path only to join the main program at the end.

What is a Flowchart? (Flowchart Part 1)

Filed under , on Monday, January 25, 2010

0

A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to arrive at a solution. In other word, Flowchart helps us to understand and see the form of algorithms by representing algorithms in picture symbols.
The instructions are depicted using the specific symbols. These symbols are connected by arrows to indicate the order of execution.

Figure 1

Flowcharts are tools used to write programs and they serve the following purpose:
•    They are easier to understand, at a glance, than a narrative description.
•    We can review and debug programs easily with the help of flowcharts.
•    They provide effective program documentation.
•    With a flowchart drawn, it is easier to explain a program or discuss a solution.
In drawing flowcharts, certain symbols are used. They are as shown in Figure 1.
The following example will help us to understand the use of the different symbols in a flowchart:
Example 1    To find the sum of the two numbers, the flowchart will be as follows:

Figure 2

The flowchart shown in Figure 2 illustrates the steps in a straight-line logic. It involves no repetitions or alternate sequence or steps. But, in practice:
  • Repeating certain steps of a program is common
    For example, calculating the average marks for 100 students. The step that calculates the average for one student has to be repeated 100 times.
  • We take an alternate sequence of steps for some situations
    For example, if the amount in a bank account is greater than the withdrawal amount then the withdrawal is processed. If the amount is less then the withdrawal is refused.

Algorithms

Filed under , on Sunday, January 24, 2010

0

A logical and concise list of steps required to solve a problem is called an algorithm. An algorithm is the first step in problem solving. Not only in programming world, actually have algorithms also existed in daily life. Given below is the example case in daily life:
A person wishes to book a railway ticket from Oxford to London.
The steps involved or rather the algorithm as follows:
• The passenger enters details like his name, age, the starting point of journey, destination, and the date of journey in the reservation slip.
• On submitting the slip at the reservation counter, the counter assistant checks for the availability of the seats.
• If the required number of seats is available the passenger is given a confirmed ticket.
• Otherwise, a wait-listed ticket is issued.
• A wait-listed ticket is confirmed if another person cancels his ticket.
• The passenger is given a refund if he is not given the confirmation.
Algorithms
How to Think About Algorithms

Problem Solving by Programming

Filed under , on Sunday, January 24, 2010

1

We use computers to solve problems and perform calculations. However, in order to solve a problem using a computer, we must express the solution to the problem in terms of instructions necessary to solve the specific problem.

In other words, we have to provide the computer with a set of instructions to solve the problem at hand. This set of instructions is called a program. Once we write a program to solve a specific type of problem, it can be used again and again to solve the same type of problem.

For example, if we write a program to calculate the average of the marks obtained by 100 students in a class, the same program can be reused whenever we want to calculate the average of any set of 100 students.

Problem solving is an intricate process requiring thought, planning, logical precision, persistence and attention to detail.
The computer cannot be used to solve a problem until the programmer develops a method for the solution. This method or approach used solve a problem is called an algorithm.
The following steps are involved in solving a problem:
* Studying the problem in detail
* Gathering the relevant information
* Processing the information
* Arriving at the results

The above mentioned steps can be understood well with the following example.

For example, to check if a number is even or odd, the following steps are required:
* Read the number
* Divide the number by 2
* If the remainder of the division is zero, then the number is even
* Otherwise the number is odd

With these sequences of steps in hand, we can then proceed with expressing them in the statements of a particular programming language.

Although, there are so many programming languages today with different tools, name, syntax and way to use, they are all based on three primitive fundamental: sequence, branching and looping. Sequence means all program code always run from the first line to the last line of program code. Branching means program flow can jump or skip one or more lines of program code. Looping means program flow can repeat one or more lines of program code. These fundamental will be discussed more in the next week.