CS-303 Theorical Concepts
CS-303 Theorical Concepts
on
PROGRAMMING IN “C”
By Revised Version
Asst. Professor. Dr. Ghulam Ali
Programming fundamentals
Lecture #1
Introduction, History and overview of Computer Programming
• VERY EXCITING CS APPLICATIONS
• MOBILE PHONES
• SOME FACTS…
• Indian Software, Technology Enabled Services (ITES) and Business Process Outsourcing
(BPO) exports during 2011-12 were estimated at $68.02
• Introduction
• HISTORY 1
• HISTORY 2
• Categories of computers
• Mainframe computers
• Midsize computers
• Micro computers (personal computers)
• Hardware
• CPU
• Main memory
• Secondary storage
• Input/Output devices
• Software
• Hardware
• CPU
• Input/output devices
• Secondary storage
• All programs must be loaded into main memory before they can be executed
• All data must be brought into main memory before it can be manipulated
• When computer power is turned off, everything in main memory is lost
• Main memory is where programs and data are kept during execution that the processor is
actively working with.
• Main memory is also called Random Access Memory (RAM) or primary memory.
• "Random" means that the memory cells can be accessed in any order randomly.
• Each cell has a unique location in main memory, called the address of the cell
• Main Memory
• Volatile
• Non volatile
• Secondary Storage
• Secondary storage: device that stores information permanently
• The contents are easily changed, but this is very slow compared to main memory.
• Hard disks
• Flash drives
• Floppy disks
• Zip disks
• CD-ROMs
• Tapes
Primary memory
1. Fast
2. Expensive
3. Low capacity
Secondary memory
1. Slow
2. Cheap
3. Large capacity
• Input/Output Devices
• Keyboard
• Mouse
• Secondary storage
• Output devices
display results
• Monitor
• Printer
• Secondary storage
• Software
• Software is kept on some hardware device such as a hard disk or floppy disk for further
use.
• character data, numerical data, image data, audio data, and countless other types.
• Types of Software
• Operating system monitors the overall activity of the computer and provides
services such as:
• When you press A on your keyboard, the computer displays A on the screen. But what is
actually stored inside the computer’s main memory?
• Byte:
• Inside the computer, every character is represented as a sequence of eight bits, that is, as a
byte.
• ASCII is a seven-bit code. Therefore, to represent each ASCII character inside the
computer, you must convert the seven-bit binary representation of an ASCII character to
an eight-bit binary representation.
• A is represented as 01000001
• 3 is represented as 00110011
Lecture #3
Evolution of Programming languages
• The Evolution of Programming Languages
• The most basic language of a computer, the machine language, provides program
instructions in bits.
• The machine language of one machine is not necessarily the same as the machine
language of another machine.
• The only uniformity among computers is that in any modern computer, all data is stored
and manipulated as binary codes.
• To represent the data, the programmer had to remember the machine language
codes
• Chance of errors.
• Assembly language uses letters and numbers to represent machine language instructions.
• Machine Language and Assembly Language are both called low-level languages.
• For example, the machine code for adding two integers might be:
010000110011101000111101010000010010101101000010
LOAD A
ADD B
STORE C
• This causes the number in A to be added to the number in B, and the result is
stored for later use in C.
• Using assembly language instructions, wages = rate * hours can be written as:
LOAD rate
MULT hour
STOR wages
• Moving from machine language to assembly language made programming easier, but:
• The next step toward making programming easier was to plan high-level languages that
were closer to natural languages, such as English, French, German, and Spanish.
• High-level languages include Basic, FORTRAN, COBOL, Pascal, C, C++, C#, and Java.
• Interpreter: Translate each instruction from the programming language as needed (line
by line of code).
• Compiler: translates a program written in a high-level language into machine language. A
compiler makes the translation once so that the source code don’t have to be translated
each time the program is run.
• Advantages Of
High Level Languages
• Are more portable (the programs are easier to move among computers with
different microprocessors).
• Sequence.
• Selection: if-then.
• Introduction
• Have you ever given someone directions to go from one location to another?
• E.g.
• If your answer to any of these questions, or to similar questions, yes, then you're already
familiar with programming.
• Pseudocode
• It uses short phrases to write code for programs before you actually create it in a specific
language
• pseudocode
• 3. one of you should sit down; the other should go back to step 2
• This process is described as a sequence of steps, which are followed in order, one
after the other.
• Another Example
• Step-1 If you don't have a new light bulb buy a new one.
• Step-3 Repeat, twisting the light bulb counter clockwise until you remove it.
Here we have a sequence of ordered steps which also include a decision and a repetition.
• Algorithm
• Algorithms describe the procedure for carrying out a task or solving a problem in a step-
by-step fashion.
• Representation of Algorithm
• Very often, there is more than one algorithm that effectively solves a problem.
• Let's have a look at the following map and try to give directions to a person who is
currently at the hotel and wants to get to the train station. Can you come up with two
algorithms for this problem
• Algorithm in English
• Informal definition
• Assumptions
• To solve any problem we must assume something in our mind before actually starting the
solution.
• Examples: Cross the street, bake a cake, pour a glass of juice, sum two numbers of three
digits each?
• For example, what assumption are we going to make for the traffic, when designing an
algorithm for crossing the street?
• Is it left-hand, or right-hand? We need to take into account the direction of the vehicles in
order to check the corresponding side.
• There might be steps which mean something different for different people.
• For instance, if when telling someone how to bake a cake, we instruct them to put it in the
oven for a short time, they, they wouldn't really know at what temperature the oven
should be, or how long a short time really is.
• Properties of Algorithm
• Input and Output - defined the unknowns of the problem is specified and with the
expected outcome.
• Effectiveness - the solution prescribed is guaranteed to give a correct answer and that the
specified process is faithfully carried out.
• Precise instructions
• Draw another diagonal line connected to the top of the first one.
• Draw a vertical line over the point where the diagonal lines meet.
• Draw a diagonal line from the bottom of the first diagonal to the straight line.
• Draw a diagonal line from the bottom of the second diagonal to the straight line.
• Draw a curvy line, starting at the bottom of the straight line. What did you come up with?
Lecture # 5
Algorithm Representation and Examples
• Steps in Program Development
1. State the problem clearly- a problem cannot be solved correctly unless it is being
understood.
2. Plan and Write the Logical Order of Instructions - the computer follows the direction
exactly at the given sequence.
3. Code the Program - write the programming statements in the desired language.
4. Enter the program into the computer - key in or type the statement into the computer.
5. Run and Debug the program - check if you have the desired output; if not, trace the
possible error.
• Algorithm representation
• Flowchart
• Pseudocode
• Flowchart symbols
• Flowcharts
• Lecture # 6
• It is more undersatandable.
• A subalgorithm can be called many times in different parts of the main algorithm
without rewritten.
Lecture # 6
Programming Constructs and Sub-Algorithm
Three constructs
Flowcharts for three constructs
Solution
Average Of Two
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Example 2
Write an algorithm to change a numeric grade to a pass/no pass grade.
Solution
Pass/NoPassGrade
Input: One number
1. if (the number is greater
than or equal to 60)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to
“nopass”
End if
1. Return the grade
End
Example 4
Write an algorithm to find the largest of a set of numbers.
Sub algorithms
• The advantages of subalgorithms:
• It is more undersatandable.
• A subalgorithm can be called many times in different parts of the main algorithm
without rewritten.
FindLargest
Input: A list of positive integers
1.Set Largest to 0
2.while (more integers)
2.1 if (the integer is greater than Largest)
then
2.1.1 Set largest to the value of the
integer
End if
End while
3.Return Largest
End
SUBALGORITHMS
Concept of a subalgorithm
Sub algorithms
• It is more undersatandable.
• A subalgorithm can be called many times in different parts of the main algorithm
without rewritten.
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 FindLargerNumber
End while
3. Return Largest
End
Lecture # 07
Algorithms and Problem Solution
Including this main goal as our main principle there are some more principle that are used
for the structured programming listed as:
• Standardization.
• Abstraction.
• Expressive Power.
• Orthogonality.
• Minimality.
The basic idea behind structured programming is to linearize control flow in computer programs
so that execution sequences follow the order in which code is written. The following qualities are
made obvious by this significant principle:
None of the repetition and sequence selection constructions have more than three boxes. A third
characteristic becomes clear when we see any one of the three structures (described before) in use.
Therefore, this approach makes code more readable, which makes it simpler to comprehend, test,
debug, document, and modify programs.
In addition to this primary objective, which serves as our guiding principle, the following
additional principles are used for structured programming:
• Standardization
• Abstraction
• Expressive Strength
• Orthogonality
• Minimality
(a) Standardization: It is always preferable to construct software from parts that share a common
external interface. The basic colored face has one entering point at the beginning and one departure
point at the conclusion. This possesses the power of something that is consistent with the
fundamentals of sequential programming. Additionally, it complies with the crucial notion of
calling a procedure. We view the concept of calling a procedure as a sequential step that is followed
by a return to the following instruction.
If then else, while do, and similar structures also have the property of having a single entry at the
beginning and a single exit at the conclusion.
(b) Reaction: The most crucial concept in structured programming is definitely abstraction. The
human mind is incapable of creating or comprehending a complicated system in its entirety. It is
limited in its understanding of the system. However, it is necessary and crucial to comprehend the
entire system. As a result, abstraction is utilized to tackle this difficulty. The system must be
abstracted so that subsystems may be represented by a notation that makes it possible to view them
as opaque boxes with well known functions. The procedure has long been a mechanism in
programming that serves this purpose.
(c)Expressive Power: As we saw in section 6.5, 2.2, the go-to statement has relatively little
expressive power because it can have three to four different interpretations. Because of this, we
need a mechanism for recurrence, and both recursion and a while statement are adequate to offer
this facility. Both a while statement and a do while statement are available in many programming
languages. Recursion is a feature that most languages have.
(d) Orthogonality: A smart design approach when creating a group of facilities is to make each
feature as distinct from the others as you can. If this is the case, we may more easily achieve the
goal of having a minimal amount of functions while also making sure that the facilities are
powerful enough to meet all of our needs.
(e) Minimalism: When the minimality principle prevents us from incorporating too many
facilities. Therefore, in order to adhere to the idea of minimalism, the three constructs listed by
Boehm Jacopini are all that are necessary to create a structured program. Other control structures
exist that only use three boxes or less, but of all of them, this collection of three constructs is the
bare minimum.
Lecture #8
Analysis–Coding–Execution Cycle and Processing a C
Programming
Programming with the Problem Analysis–Coding–Execution
Cycle
• Step 3: Maintenance
• After you carefully analyze the problem, the next step is to design an algorithm to solve
the problem.
If you broke the problem into subproblems, you need to design an algorithm for
each subproblem
• After you carefully analyze the problem, the next step is to design an algorithm to solve
the problem.
If you broke the problem into subproblems, you need to design an algorithm for
each subproblem
• Linker links machine code with system resources, and the loader places the program
into main memory for execution.
• Compiler guarantees that the program follows the rules of the language
During execution, the program might terminate abnormally due to logical errors,
such as division by zero.
• Under these circumstances, you may have to reexamine the code, the algorithm, or even
the problem analysis.
Lecture # 9
1.9: Programming Methodologies, Quick Review
Programming Methodologies
Structured
Object-oriented
Structured Programming
• Structured design:
• Structured programming:
– Stepwise refinement
– Modular programming
Object-Oriented Programming
• An object combines data and operations on the data into a single unit
• Must learn how to represent data in computer memory, how to manipulate data, and how
to implement operations
Summary
• Computer: electronic device that can perform arithmetic and logical operations
Operating system monitors overall activity of the computer and provides services
Structured design:
Vocabulary
• Variable- A variable holds data that can change while the program is running
• Constant- A constant is used to store data that remains the same throughout the program’s
execution
• Integers- Integers include the whole numbers and the negative numbers (no decimal
points).
• Floating point numbers- Floating-point numbers are real numbers (have decimal points)
• Data type- Data type is a type of variable that best fits the nature of the data itself