7
  Lecture No. 1 Introduction What is Programming Why Programming Programming Skills  Design Recipe Good Programming Practices Case Study:Analysis and Design Tips  Introduction In this course we will discus about programming i.e. programming principles, design and structure. In this course, we will use C as a programming language in a C++ development environment. As C++ is an object-oriented language and we are covering procedural programming in our course, we will NOT focus on object-oriented concepts but only on structured programming  concepts .  What is Programming "A program is a precise sequence of steps to solve a particular problem.”  It means that we must know exactly about the order of tasks to be done-- what we will do first and what we will do next. This order of steps should be taken to solve a particular problem so that at the end, we can get the desired result.  Alan Perlis, a professor at Yale University, says: "It goes against the grain of modern education to teach children to program. What fun is there in makin g plans, acquiring disciplin e in organi zing thoughts, devoting attention to detail and learnin g to be self-critical? " It is a sarcastic statement about modern education, which states that the modern education does not provide enough information to children about planning. However, in our daily lives, we plan many activities. For example, for a picnic trip we plan where to go, what to wear, what to take for lunch, how to travel and at what time to go and return. Making these plans is fun. For computer program ming, the same thing is applied but we specify it in compu ter languages. Computer languages are used to write instructions for the computer to do some task. In this course C language is used.

Lecture No

Embed Size (px)

Citation preview

Page 1: Lecture No

8/2/2019 Lecture No

http://slidepdf.com/reader/full/lecture-no 1/7

 •  Lecture No. 1 

Introduction

What is Programming

Why Programming

Programming Skills

  Design Recipe

Good Programming Practices

Case Study:Analysis and Design

Tips

  Introduction

In this course we will discus about programming i.e. programming principles, design and structure.

In this course, we will use C as a programming language in a C++ development environment. As

C++ is an object-oriented language and we are covering procedural programming in our course, we

will NOT focus on object-oriented concepts but only on structured programming concepts.

 What is Programming

"A program is a precise sequence of steps to solve a particular problem.”  

It means that we must know exactly about the order of tasks to be done-- what we will do first and what wewill do next. This order of steps should be taken to solve a particular problem so that at the end, we can getthe desired result.

 Alan Perlis, a professor at Yale University, says:

"It goes against the grain of modern education to teach children to program. What fun is there inmaking plans, acquiring discipline in organizing thoughts, devoting attention to detail and learningto be self-critical? "

It is a sarcastic statement about modern education, which states that the modern education does notprovide enough information to children about planning. However, in our daily lives, we plan many activities.For example, for a picnic trip we plan where to go, what to wear, what to take for lunch, how to travel and atwhat time to go and return. Making these plans is fun.

For computer programming, the same thing is applied but we specify it in computer languages. Computer languages are used to write instructions for the computer to do some task. In this course C language isused.

Page 2: Lecture No

8/2/2019 Lecture No

http://slidepdf.com/reader/full/lecture-no 2/7

 

Why Programming? 

Programming is a creative exercise and is fun. Everyone should do programming. It developsanalytical abilities and self-criticism. Programming skills are very useful in everyday life. For 

instance, if we play a cricket match, we make strategies for bowling, batting and fielding to winthe match. During the match, we may change our strategies by analyzing the situation. If oneparticular bowler is giving a lot of runs, we try another bowler. Similarly on the batting side,when we are chasing a target we change the batting order according to the situation.

By designing programs, we learn many skills like critical reading, analytical thinking, creativesynthesis and attention to detail; that are important in daily life and in all kinds of professions.There are different areas where programming is used e.g. Computer graphics used in webpages, advertisements and movies. You can even do programming to develop games likeStreet Fighter, Motor Race, cricket, etc. Similarly, you can develop programs for differentprofessions like accounting, medicine and business.

In accounting, an accountant uses spreadsheets, he writes formulas for calculation which itself is programming. Similarly, a photographer uses Photo-Editor to make changes andimprovements to the photos. In plane flying, a pilot uses different controls to fly. He feeds hisprogram in the plane's computer and the computer works accordingly.

 

rogramming Skills

 

Programming is not a deeply theoretical subject like Chemistry or Physics; you don't need an advanceddegree to do well at it. (There are important principles of Computer Science, but it's possible to get a degreeafter studying them and to have only vague ideas of how to apply them to practical programming. There areplenty of successful programmers who don't have Computer Science degrees.)

Comparing programming to some physical tasks, programming does not require any inborn talent or skill,like gymnastics or painting or singing. Programming does, however, require care and craftsmanship, likecarpentry or metalworking. You may have seen in a class that some students seem to be able to turn outbeautiful projects effortlessly, while other students are all thumbs and make the exact mistakes that theteacher tells them not to make. What distinguished the successful students is not that they are better or smarter, but just that they pay more attention to what is going on and are more careful and thoughtful about

what they are doing.

While programming, you must take into consideration the following three points

(1) computers are stupid

(2) paying attention to detail

(3) reusability

 

Page 3: Lecture No

8/2/2019 Lecture No

http://slidepdf.com/reader/full/lecture-no 3/7

 1. Computers are stupid

 

Computers are incredibly stupid. They do exactly what you tell them to do: no more, no less-- unlike humanbeings. Computers can't think by themselves. In this sense, they differ from human beings. For example, if someone asks you, “What is the time?”, “Time please?” or just, “Time?” you understand anyway that he isasking the time but computer is different. Instructions to the computer should be explicitly stated. Computer will tell you the time only if you ask it in the way you have programmed it.

When you're programming, it helps to be able to "think'' as stupidly as the computer does, so that you are inthe right frame of mind for specifying everything in minute detail, and not assuming that the right thing willhappen by itself.

This is not to say that you have to specify everything. The whole point of a high-level programminglanguage like C is to take some of the work burden off the busy programmer.

2. Paying attention to detail

 

In programming, the details matter. This is a very important skill. A good programmer always analyzes theproblem statement very carefully and in detail. You should pay attention to all the aspects of the problem.You can't be vague. You can't describe your program 3/4th of the way, then say, "You know what I mean?'',and have the compiler figure out the rest. From the coding point of view, if the language says you have todeclare variables before using them, you have to. If the language says you have to use parentheses hereand square brackets there and squiggly braces some third place, you have to.

From logical point of view you should pay attention to the calculations involved in the program, its flow, andmost importantly, the logic of the program. Sometimes, a grammatically correct sentence does not have asense or understanding. For example, here is a verse from poem "Through the Looking Glass" written byLewis Carol:

Twas brillig, and the slithy toves

Did gyre and gimble in the wabe

The grammar is correct but there is no meaning. Similarly, the sentence, "Mr. ABC sleeps thirty hours everyday", is correct grammatically but has a nonsense meaning.

So it may happen that a program is grammatically correct. It compiles and runs but produces incorrect or senseless results and does not solve the problem. It is very important to pay attention to the logic of theprogram.

In the Falkland war that was fought between England and Argentina, both countries had weapons made bythe West. The Argentinean naval force launched a missile towards a British ship. The anti missile systemon the British ship detected it and also recognized its type and manufacturer and the system then looked

into its database and recognized it as a friendly missile (as Argentina was also using weapons made by theWest and the West was a friend of the British) and did not activate its defense system. The missile hit itstarget and there was a loss of many lives. The system did not consider the trajectory of missile (where it isgoing to hit). That was a programming fault. The system had not been told to detect the target of the missileat the time of programming. Thus a little oversight in programming resulted in loss of many lives.

Page 4: Lecture No

8/2/2019 Lecture No

http://slidepdf.com/reader/full/lecture-no 4/7

3: Reusability 

When ever you are writing a program, always keep in mind that it can be reused at some other time. Also,try to write in a way that it can be used to solve some other related problem. A classic example of this is:

Suppose we have to calculate the area of a given circle. We know the area of a circle is (Pi * r 2

). Now wehave written a program which calculates the area of a circle with given radius. At some later time we aregiven a problem to find out the area of a ring. As we know the formula of calculating the area of a ring(shaded area) is (Area of outer Circle - Area of Inner Circle), we can use the same program whichcalculates the area of a circle.

Design Recipe 

To design a program properly, we must:

   Analyze a problem statement, typically expressed as a word problem.  Express its essence, abstractly and with examples.  Formulate statements and comments in a precise language.  Evaluate and revise the activities in light of checks and tests

PAY ATTENTION TO DETAIL.

Let’s say we have to develop a payroll system of a company. The company has permanent staff,contractual staff, hourly based employees and per unit making employees. Moreover, there are differentdeductions and benefits for permanent employees and there is a bonus for per unit making employees andovertime for contractual employees.

We need to analyze the above problem statement. The company has four categories of employees; i.e.;Permanent staff, Contractual staff, hourly based employees and per unit making employees. Further,

permanent staff has benefits and deductions depending upon their designation. Bonus will be given to per 

Page 5: Lecture No

8/2/2019 Lecture No

http://slidepdf.com/reader/full/lecture-no 5/7

unit making employees if they make more than 10 pieces a day. Contractual employee will get overtime if they stay after office hours.

Now divide the problem into small segments and calculations. Also include examples in all segments. Inthis problem, we should take an employee with his details from each category. Let’s say, Mr. Ahmad is apermanent employee working as Finance Manager. His salary is Rs.20000 and benefits of medical, car allowance and house rent are Rs.4000 and there is a deduction of Rs.1200. Similarly, we should consider employees from other categories. This will help us in checking and testing the program later on.

The next step is to formulate these statements in a precise language, i.e.; we can use the pseudo code and

flowcharting. Then this will be used to develop the program using computer language.

Then the program should be evaluated by testing and checking. If there are some changes identified, werevise the activities and repeat the process. Thus repeating the cycle, we achieve a refined solution.

The most important thing in this process is to pay attention to details. Here is a small exercise for your brainstorming.

In an office, after having a very busy day, the secretary is just leaving the seat when the boss calls her andhands over the drafts for four letters with addresses. The secretary types the letters and addresses onenvelopes. But when she is going to put letters in envelopes, she puts letters in envelopes randomly withoutseeing the corresponding addresses. What is the probability (what are the chances) that exactly threeletters will be dispatched to correct addresses?

 Good Programming Practices 

Here are some good practices to be undertaken while programming and we will learn more as we go along.

Liberally comment your codes Always comment the code liberally. The comment statements do

not affect the performance of the program as these are ignored by the compiler and do not take anymemory in the computer. Comments are used to explain the functioning of the programs. It helps the other programmers to understand the program.

Good user interface

 As programmers, we assume that computer users know a lot of things which is a big mistake. So never assume that the user of your program is computer literate. Always provide an easy to understand and easy

to use interface that should be self explanatory. 

Case Study: Analysis and Design 

Problem Statement:

 A friend of yours has just started making his own window units and is looking for a program which will do thecosting of the materials for him. He wants to just enter the dimensions of the window (Height and Width)and then get the cost to make the window, in terms of the amount of wood and glass required.

Specifying the Problem:

When considering how to write the specification of a system there are three important things :

• What information flows into the system?

• What the system does with the information?

• What flows out of the system?

Page 6: Lecture No

8/2/2019 Lecture No

http://slidepdf.com/reader/full/lecture-no 6/7

 Information going in

In the case of our problem we can describe the information as:

• The width of a window.

• The height of the window.

Information coming out

The information that our customer wants to see is:

• the area of glass required for the window

• the length of wood required to build a frame.

What the program actually does

The program can derive the two values according to the following equations:

glass area = width of window * height of window

wood length = (width of window + height of window) * 2

 Putting in more detail

We now have a fairly good understanding of what our program is going to do for us. Being sensible and far thinking people, we do not stop here. We now have to worry about how our program will decide when theinformation coming in, is actually valid.

This must be done in conjunction with the customer. He or she must understand that if information is givenwhich fits within the range specified, your program will regard the data as valid and act accordingly.

In the case of the above, we could expand the definition of data coming in as :

• The width of the window, in meters and being a value between 0.5 meters and 3.5 meters inclusive.

• The height of the window, in meters and being a value between 0.5 meters and 2.0 metersinclusive.

Note that we have also added units to our description. This is very important - perhaps our customer buyswood from a supplier who sells by the foot, in which case our output description should read :

• The area of glass required for the window, in square meters.

• The length of wood required for the frame, given in feet using the conversion factor of 3.25 feet per meter.

If in the above program i give the input: 2 meters for height and 1 meter for the width, the program should 

display:

2 square meters of glass and 9.75 feet of wood. 

The programming portion of the job is now simply converting the above description into a language whichcan be used in a computer.......

 

Page 7: Lecture No

8/2/2019 Lecture No

http://slidepdf.com/reader/full/lecture-no 7/7

Tips 

Don’t assume on part of the users

  User Interface should be friendly

  Don’t forget to comment the code

  PAY ATTENTION TO DETAIL

  Program, program and program, not just writing code, but the whole process of design and

development