Welcome to Edukum.com

Problem Solving with Computer

Elements of C

Input and Output

Operators and Expression

Control Statement

Function

Pointers

Structure and Union

Files and File Handling in C

Introduction to Graphics

Problem Solving with Computer

If you want to get something done by a person, you will tell him what to do in a language that he understands. Similarly, if you want to make the computer do some task for you, you have to tell the computer what to do in a language the computer understands. Hence, the programming language is a set of rules that provides a way of instructing the computer to perform certain operations.
A program is a set of instructions written in a specific programming language that a computer can interpret and execute. A computer requires programs to function. Programming languages are said to be lower to higher, depending on whether they are closer to the language the computer itself uses (lower, which means 0s and 1s) or to the language that people use (higher, which means more English like). Here, we will consider following groups of languages:

  1. Machine Language / First-generation Language
  2. Assembly Language / Second-generation Language
  3. High-level Language
  4. Procedural Language / Third-generation Language
  5. Problem-oriented Language / Fourth-generation Language
  6. Natural language /Fifth-generation Language
  1. Machine Language/First-Gen. Language
    It is the lowest level of programming language. In this language, information is represented as 0s and 1s. This programming is very tedious and time-consuming. A programmer must keep track of a tremendous amount of detail. Programming in machine code has one advantage over programming at other language level – its execution is very fast and efficient because the computer can accept the machine code as it is. Since there is no standard machine language, a program written in machine language for one computer model may not on different model computer.
  2. Assembly Language/Second-Gen. Language
    In the 1950s, to reduce programming complexity and provide some standardization, assembly languages were developed. Assembly languages, also known as symbolic languages use abbreviations or mnemonic code – codes more easily memorized – to replace the 0s and 1s machine language. For example, ADD and SUB for addition and subtraction operations.
    For an assembly language program to be executed, it must be translated to machine code. This translation is done by translators called assemblers. The program before translation, the assembly language program, is called a source program and the program after translation, the machine language program, is an object program.
    1. Assembly language offers several advantages:
      1. Standard and easier to use than machine language.
      2. Operate very efficiently, although not as efficient as the machine languages.
      3. Easier to debug because programs locate and identify syntax errors.
    2. However, there are still some disadvantages:
      1. Programs are usually very long.
      2. Programs are still complex.
      3. Assembly languages are still machine-dependent.

  3. High-level Language
    High-level languages are simple and easy to understand. These languages assist programmers by reducing further the number of computer operation details they had to specify so that they could concentrate more on the logic needed to solve the problem. There are three different types of high level languages.

  4. Procedural language/Third Gen. Language
    General-purpose programming languages are called procedural languages or third generation languages. These languages are designed to express the logic, the procedure, of the problem. Pascal, FORTAN, and C are some examples of procedural language.
    1. Procedural languages have many advantages over machine and assembly languages:
      1. Program statements resemble English and hence are easier to work with. o Less time is required to program a problem.
      2. Programs are easier to understand and modify.
      3. Programming languages are machine-independent.
    2. However, these languages still have some disadvantages compared to machine and assembly languages:
      1. Programs execute more slowly.
      2. These languages use computer resources less efficiently.

  5. Problem-oriented language/Fourth Gen. Language
    These languages are designed to solve specific problems or develop specific applications by enabling you to describe what you want rather than step-by step procedures. These languages are categorized into several kinds of application development tools as given below:
    • Personal computer applications software like word processors, spreadsheets, database managers etc.
    • Query languages and report generators like SQL (Structured Query Language, QBE (Query by Example) etc. to search a database using certain selection commands.
    • Decision support systems that help managers to make decisions and financial planning languages.
    • Application generators to develop a particular application without writing detail logic.

  6. Natural language/Fifth Gen. Language
    Natural languages are used in the areas of artificial intelligence and expert systems. These languages are used to make computers more humanlike and allow the computer to become smarter. That is, simulate the learning process by remembering and improving upon earlier information. Two popular natural languages are LISP and PROLOG.

Compilers and Interpreters
For a high level language to work on the computer it must be translated into machine language. There are two kinds of translators – compilers and interpreters. So, high level languages are called compiled and/or interpreted languages. In a compiled language, a translation program is run to convert the programmer’s entire high-level program, which is called the source code, into a machine language code. This translation process is called compilation. The machine language code is called the object code and can be saved and either run (executed) immediately or later. Some of the most widely used compiled languages are COBOL, C, C++, FORTRAN etc.

In an interpreted language, a translation program converts each program statement into machine code just before the program statement is to be executed. Translation and execution occur immediately, one after another, one statement at a time. Unlike compiled languages, no object code is stored and there is no compilation. The most frequently used interpreted language is BASIC.

Some languages are both compiled and interpreted. One popular such language is JAVA.

Program Design
Before writing computer programs we can use different tools to design it. This design helps the programmer to write computer programs more easily, correctly, and quickly. Some most useful program design tools are: algorithm, flowchart, and pseudocode.

Algorithm
An algorithm is a finite sequence of instructions for solving a stated problem. A stated problem is a well-defined task that has to be performed and must be solvable by an algorithm. The algorithm must eventually terminate, producing the required result. For example, the following algorithm can be used to compute interest given the values of p, n, and r.

1. Enter the values of p, n, and r.
2. Calculate interest i using the formula p × n × r / 100.
3. Display interest i as a result.
 

Properties of a good algorithm:

  • Input
    A number of quantities are provided to an algorithm initially before the algorithm begins. These quantities are inputs which are processed by the algorithm.
  • Definiteness
    The processing rules specified in the algorithm must be unambiguous and lead to a specific action.
  • Effectiveness
    Each instruction must be sufficiently basic such that it can, in principle, be carried out in a finite time by a person with paper and pencil.
  • Finiteness
    The total time to carry out all the steps in the algorithm must be finite.
  • Output
    An algorithm must have output.
  • Correctness
    Correct set of output must be produced from the set of inputs. For the same input, it must always produce the same output.

Flowchart
A flowchart is a common type of chart that represents an algorithm or a computer program showing the steps as boxes of various kinds, and their order by connecting these with arrows. Flowcharts are used in analyzing, designing, documenting or managing a process or program in various fields. A typical flowchart may have the following kinds of symbols.

  • Circles, ovals, or rounded rectangles: Usually containing the word "Start" or "End", or another phrase signaling the start or end of a process.
  • Arrows: Shows flow control. An arrow coming from one symbol and ending at another symbol represents that control passes to the symbol the arrow points to.
  • Rectangle: Represents processing steps.
  • Parallelogram: Represents input and output.
  • Diamond: Represents condition or decision. These typically contain a Yes/No question or True/False test. This symbol is unique in that it has two arrows coming out of it, usually from the bottom point and right point, one corresponding to Yes or True, and one corresponding to No or False. The arrows should always be labeled.
For example, the flowchart to calculate bn can be given as follows:
Flowchart

flow chart

Pseudo code
It is also called program design language (PDL) or structured English. It is a language and syntax for designing a computer program based on the relative strengths of structured programming and Natural English. At first glance, PDL looks like a modern programming language. The difference between PDL and a real programming language lies in the use of narrative text embedded directly within PDL statements. In pseudocode, we can use language constructs like IF, SWITCH, FOR, WHILE, and DO-WHILE along with Natural English.
For example,

GET student’s grade
IF student’s grade is greater than or equal to 50
PRINT “passed”
ELSE
PRINT “failed”

ASCII
It is the acronym for the American Standard Code for Information Interchange. ASCII is a code for representing English characters as numbers, with each letter assigned a number from 0 to 127. For example, the ASCII code for uppercase M is 77. Most computers use ASCII codes to represent text, which makes it possible to transfer data from one computer to another. Text files stored in ASCII format are sometimes called ASCII files. Text editors and word processors are usually capable of storing data in ASCII format, although ASCII format is not always the default storage format. The standard ASCII character set uses just 7 bits for each character, meaning it can have 128 identifiers, two to the seventh power (27). There are several larger character sets that use 8 bits, which gives them 128 additional characters. The extra characters are used to represent non-English characters, graphics symbols, and mathematical symbols.

History:

  1. In 1963 a programming language named CPL (Combined Programming language) was developed at university of Cambridge. The name was chosen because it was combination of all programming language available at that time.
    Problem with CPL was
    • It was too big.
    • And Large number of featured available it made it hard to learn.
  2. Martin Richards of Cambridge try to solve the problem with CPL by introducing a new programming language named BCPL but it becomes less powerful and to much specific.
  3. In 1967, Ken Thompson developed a programming language named B but it has also problems similar to that of BCPL.
  4. By taking all the good points of CPL, BCPL and B Dennis Ritchie developed a programming language named C at AT & T-Lab, USA.
  5. C has achieved lost generality of B and also gained power of CPL.

Structure of C Programs

  • # include //header files
  • #include //contains information about
  • int main()
  • {
  • clrscr(); //Clears the Screen
  • printf(“Wel-Com to C Programming”); // Prints the message in monitor
  • getch(); //Holds the screen until we press some key
  • return 0;
  • }

Dissection of above program

  • Header files stdio.h and conio.h are special files that comes along with C compiler and contains information about library files (Programs).
  • Stdio.h contains information about printf( ) function that we have used in our program. If we do not use printf stdio.h is not needed to be included.
  • Conio.h contains information about clrscr( ) and getch( ) function. If we do not use getch() and clrscr( ) functions in our program conio.h file is not needed to be included.

Another program

  • #include
  • #include
  • int main()
  • {
  • clrscr(); //clears the screen
  • int a,b,r; //declares a, b and r as integer variables
  • a=2; //assigns value 2 to the variable a
  • b=3; //assigns value 3 to the variable b
  • r=a+b; // finds the sum of a and b and keeps the result in variable r
  • printf(“sum=%d”,r); // prints the sum in monitor
  • getch(); //Holds the screen until we press some key
  • return 0;
  • }

 

Reference:

Note by: Bhupi singh Saud
Deitel,c.: How to program, 2/e (with CD), Pearson Education.
Bryons S. Gotterfried: “Programming with C,” TMH

© 2019 EDUKUM.COM ALL RIGHTS RESERVED


#Things To Remember



1042 Views