Monday, December 16, 2013

Chapter 1: (C Programming) Problem Solving With Computer



Problem Solving With Computer

1.1 Problem Analysis

It is the most important stage of program or software development process. There are different techniques for analyzing problem depending upon the problem and situation. Each technique has its own merits and demerits. Methods used to develop a solution or a program are called programming techniques. The programmers use different techniques to develop the logical sequence for the solution of the problem. Some of the most commonly used techniques in procedure oriented programming are: algorithm and flowchart.

1.1.1 Algorithm

The term algorithm may be formally defined as a sequence of instructions designed in such a way that if the instructions are executed in the specified sequence, the desired result will be obtained. The instructions however, should be precise and unambiguous and the result should be obtained after a finite number of executional steps. The later condition actually states that an algorithm must terminate and should not repeat one or more instructions infinitely.
Basic guidelines for writing algorithms
·        Use plain language
·        Do not use any language specific syntax. Same algorithm should hold true for any programming language.
·        Do not make any assumptions. Describe everything clearly and explicitly.
·        Ensure that the algorithm has single entry and exit point.
Important features of an Algorithm:
1.      Finiteness / Termination: Every algorithm should lead to a session of task that terminates after a finite number of steps. (Each instruction should be such that it can be performed in a finite time)
2.      Definiteness: Each step must be precisely defined. Actions should be unambiguously specified for each case.
The definiteness and effectiveness of an instruction implies the successful termination of that instruction.
3.      Inputs: Any algorithm can have zero or more inputs. Inputs may be given initially or as the algorithm runs.
4.      Outputs: Any algorithm may result in one or more outputs (quantities that have specified relation to inputs). After algorithm terminates the desired result should be obtained.
5.      Effectiveness: All operations must be sufficiently basis so as to be implemented with even paper and pencil. This ensures that the algorithm will ultimately terminate.
Therefore, an algorithm can be defined as a sequence of definite and effective instructions, which terminates with the production of correct output from the given input.
Example1: A man brushing his own teeth
Step 1: Take the brush
Step 2: Apply the paste
Step 3: Start brushing
Step 4: Rinse
Step 5: Wash
Step 6: Stop
If one goes through these six steps without being aware of the statements of the problem, he could possibly feel that this is the algorithm for cleaning a toilet. This is because of several ambiguities while comprehending every step. The step 1 may imply toothbrush, paintbrush, toilet brush etc. Thus every step should be made unambiguous. An unambiguous step is called definite instruction. Even if step 2 is rewritten as apply the toothpaste, to eliminate ambiguities yet the conflicts such as, where to apply the toothpaste and where the source of toothpaste is, need to be resolved. Hence the act of applying the toothpaste is not mentioned. Although unambiguous, such unreliable steps can’t be included as algorithmic instruction as they are not effective.
Example2: Algorithm for preparing tea:
1.      Start
2.      Fetch water and tea leaves along with sugar and milk
3.      Boil the water
4.      Put tea leaves and sugar in boiled water
5.      Mix with milk
6.      Serve the tea
7.      Stop
Example3: Algorithm to calculate the sum of two numbers:
1.      Input two numbers A and B
2.      Sum=A+B
3.      Print sum
Example4: Algorithms to determine the student’s final grade and indicate whether he is pass or fail
1.      Input M1, M2, M3, M4
2.      Grade=(M1+M2+M3+M4)/4
3.      If (Grade<50) then
Print “Fail”
Else
Print Pass
4.      Stop

1.1.2 Flowchart

A flowchart can be defined as a pictorial representation of a sequence of steps required solving a problem. A flowchart is simply a graphical method of indicating a proposed or an actual solution of a problem. It is very helpful for debugging or tracing the flow of the diagram. Flowchart is drawn by using basic block each having special meaning. Blocks used for drawing flowchart are given below:

 Figure: Basic blocks used in flowchart 


For easy visual recognition a standard convention is used in drawing flow charts. This convention has been standardized by the international standards organization, which is shown in figure.
1.      Flat oval symbols are used to indicate the beginning and ending of the operations.
2.      Rectangles are used to indicate any processing operation such as storage and arithmetic.
3.      Parallelograms are used to represent input and output operations.
4.      Diamond shaped boxes are used to indicate questions asked or conditions tested based on whose answers appropriate exists are taken by a procedure.
5.      A circle is used to join different parts of a flow chart. This is called a connector. The use of connector gives a neat appearance to a flow chart. Further, they are necessary if a flow chart extends over more than one page and the different parts are to be joined together. The fact that two points are to be joined is indicated by placing connectors at these points and by writing the same identifying letter or digit inside both connectors.
6.      Arrows indicate the direction to be followed in a flow chart.
1.2 Coding, Compilation, and Execution
Coding
o       It is the process of writing executable program statements using language specific syntax.
o       It is the process of converting algorithm into executable program.
o       It is the process of converting programming logic into executable program.
Compilation and Execution
Compilation refers to the process of converting source program into object program. After coding the program using syntax of programming language the next step is to convert the source program into object program. During this process compiler checks for syntax error i.e. if the program contains syntax error compiler will display the list of errors in a separate window. Each error message indicates the line number where the error was detected as well as the type of error. If the program does compile successfully, it will immediately begin to execute, prompting for input, displaying output.
1.3 Debugging, Testing, and Documentation
Debugging
It is the process of identifying and removing bugs from a program or system. Bugs may be syntax error, logical error or runtime errors.

Testing
Testing is a process used to identify the correctness, completeness and quality of developed computer program or software by providing various sets of input values.

Documentation
Documentation means recording all events, activities that are carried out during program or system development. There are various tools that will aid documentation like system specifications algorithm, flowchart, coding sheets, pseudocodes, test data, reports etc. Program documentation varies according to its intended use. Three main uses are:
            - For programmer's own present or future use, as an aid to all stages in programming
            - For the presnt or future use of other programmers
            - For the user of the program, who may themselves vary in expertise
Program documentation is vital for the successful implementation of a system.

1.4 Characteristics of Procedural Programming Language

·        Emphasis is on doing things
·        Large programs are divided into smaller functions
·        Most functions share global data
·        Data move openly around the system from function to function
·        Functions transform data from one form to another
·        Employs top down approach

1.5 What is C language?

·C is a general-purpose computer programming language.
·This language is machine independent language.
·The C language is often described as a middle level programming language. It permits programs to be written in much the same style as that of most modern high level languages and interact with the inner workings of the computer.
·This language is attracting considerable attention worldwide because the software industry is adopting it to great advantage. The reason for this popularity are:

Ø      Versatility
Ø      Portability
Ø      Powerful
Ø      The programs written in C are fast and efficient
Because of above all, it is now the preferred language among computer professionals for writing efficient programs.

1.6 The History of C Language

·A systems programmer, named, Dennis Ritchie, developed the C language at Bell laboratories in the early 1972.
·It was written originally for programming under an operating system called UNIX.
·This language is not tied to anyone operating system or machine.
·This language has been called a system programming language due to its capability for writing compilers and operating systems.
·Many of the important ideas of C stem from the language BCPL (Basic Combined Programming Language), developed by Martin Richards.
·The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken Thompson, another Bell Lab systems engineer in 1970 for the first UNIX system on the DEC PDP-7 computer system Ken dubbed his language to B, the first initials of BCPL.
·The letter C (Second initials of BCPL) was chosen to represent the new version of BCPL by its developer.

1.7 Characteristics of C Language

·C is a middle level language capable of writing high level code and accessing machine internal parts
·C is a structured programming language.
Ø      Grouping of statements
Ø      Decision making (if-else)
Ø      Looping with termination conditions (while, for, do, break)
Ø      Selecting possible cases (switch)
·It is function oriented
·It can access the computer memory where the variable has been stored.

1.8 Structure of C Program

·Every C program consists one or more modules called functions. One of the functions is called a main.
·Program execution always starts from main function
·Each function must contain:
Ø      Function heading - consisting function name and list of arguments in a parenthesis.
Ø      Declaration of the arguments.
Ø      Compound statements enclosed within a pair of braces.
·Each statement must terminate with a semicolon.
·Comments may appear anywhere as long as they are within the delimiters /*  and */


End of Chapter One 

If you have any problem regarding this chapter then post your problem in comment box. :)

[ Note: If you have any problems regarding this then you can post the problem in comment box. we will try to solve this here. Or if you know any solution, you can also help :) ]


No comments:

Post a Comment