You are on page 1of 17

Unit 1

Introduction to Problem Solving and Basics of Programming in C


Language

Unit 1: Introduction to Problem Solving and Basics of Programming in C Language:


Evolution of Programming & Languages - Problem Solving through Programming - Writing
Algorithms/ Pseudocodes - Drawing Flowcharts - Evolution of C language, Program Structure,
Need of header files, linkers, loader- Input and output functions: printf and scanf - Variables and
identifiers – Expressions - Single line and multiline comments - Constants, Keywords - Values,
Names, Scope and life time of variables - Storage Classes - Data type classification: Basic, derived,
user defined- Numeric data types: integer,floating point, long, double- Non-Numeric Data types:
char and string - Arithmetic operator - Increment and decrement operator -Bitwise operator and
Size of operator-Boolean- comma, arrow-Assignment operator.

Week 1
Learning Objectives:

1. Understand what a problem is and why there is a need to solve a problem.


2. Steps that are undertaken by a programmer to solve a problem.
3. Explain what an algorithm is and how it plays a major role in solving problems.
4. Understand the use of flowcharts and different symbols that are used in flowchart.

Key Topics
1.1 Introduction
1.2 Evolution of Programming and Languages
1.2.1 History of Programming Language
1.3 Problem Solving through Programming
1.3.1 Creative thinking, Critical thinking
1.3.2 Steps involved in solving a problem
1.3.3 Software Development Life Cycle
1.4 Writing Algorithm / Pseudocode
1.4.1 Properties and Rules of writing Algorithm
1.4.2 Sample Algorithms
1.5 Flowchart
1.5.1 Flowchart Symbols and guidelines for preparing flowcharts
1.5.2 Sample Flowcharts
1.6 Pseudocode
1.6.1 Guidelines for Writing Pseudocode
1.6.2 Advantages of Pseudocode

1
1.7 Evolution of C language
1.7.1 History of C Programming Language
1.7.2 Characteristics of C
1.8 Summary

1.1 Introduction

Problems are at the center of what many people do at work every day. Whether you're solving a
problem for a client (internal or external), supporting those who are solving problems, or discovering
new problems to solve, the problems you face can be large or small, simple or complex, and easy or
difficult.

A fundamental part of every manager's role is finding ways to solve them. So, being a confident
problem solver is really important to your success. Much of that confidence comes from having a
good process to use when approaching a problem. With one, you can solve problems quickly and
effectively. Without one, your solutions may be ineffective, or you'll get stuck and do nothing, with
sometimes painful consequences.

Solving problems is the core of computer science. Programmers must first understand how a human
solves a problem, then understand how to translate this "algorithm" into something a computer can
do, and finally how to "write" the specific syntax (required by a computer) to get the job done. It is
sometimes the case that a machine will solve a problem in a completely different way than a human.

Computer Programmers are problem solvers. In order to solve a problem on a computer you must:

1. Know how to represent the information (data) describing the problem.

2. Determine the steps to transform the information from one representation into another.

1.2 Evolution of Programming and Languages

Problem solving includes techniques of algorithms, heuristics and root cause analysis. The amount of


resources (e.g. time, memory, energy) required to solve problems is described by computational
complexity theory. In more general terms, problem solving is part of a larger process that
encompasses problem determination, de-duplication, analysis, diagnosis, repair, and other steps.

A Computer needs to be given instructions in a programming language that it understands.


A programming language is an artificial language that can be used to control the behaviour of a
machine, particularly a computer. Programming languages, like human languages, are defined through
the use of syntactic and semantic rules, to determine structure and meaning respectively.
Programming languages are used to facilitate communication about the task of organizing and
manipulating information, and to express algorithms precisely. 
Programming Language a computer understands are as follows:
1) Artificial language that controls the behaviour of computer
2) Language which is defined using syntactic and semantic rules

2
3) Language used to facilitate communication about the task of organizing and
manipulating information
4) Language used to express algorithms precisely

A programming language can be of two types: Functional programming language or Imperative


Programming language.

The functional programming paradigm was explicitly created to support a pure functional approach to


problem solving. Functional programming is a form of declarative programming. In contrast, most
mainstream languages, including object-oriented programming (OOP) languages such as C#, Visual
Basic, C++, and Java, were designed to primarily support imperative (procedural) programming.
With an imperative approach, a developer writes code that specifies the steps that the computer must
take to accomplish the goal. This is sometimes referred to as algorithmic programming. In contrast, a
functional approach involves composing the problem as a set of functions to be executed. You define
carefully the input to each function, and what each function returns. 

In the 1940s, the first recognizably modern electrically powered computers were created. The limited
speed and memory capacity forced programmers to write hand-tuned assembly language programs. It
was eventually realized that programming in assembly language required a great deal of intellectual
effort.

An early proposal for a high-level programming language was Plankalkül, developed by Konrad


Zuse for his Z1 computer between 1943 and 1945 but not implemented at the time.[4]
The first functioning programming languages designed to communicate instructions to a computer
were written in the early 1950s. John Mauchly's Short Code, proposed in 1949, was one of the first
high-level languages ever developed for an electronic computer.[5] Unlike machine code, Short Code
statements represented mathematical expressions in understandable form. However, the program had
to be translated into machine code every time it ran, making the process much slower than running the
equivalent machine code.

In the early 1950s, Alick Glennie developed Autocode, possibly the first compiled programming


language, at the University of Manchester.
The image below shows us the evolution of different languages (functional and imperative) developed
in different years.

1.2.1 History of Programming Languages

1883:

 In the early days, Charles Babbage had made the device, but he was confused about how to
give instructions to the machine, and then Ada Lovelace wrote the instructions for the
analytical engine.
 The device was made by Charles Babbage and the code was written by Ada Lovelace for
computing Bernoulli’s number.
 First time in history that the capability of computer devices was judged.

3
1949: Assembly Language

 It is a type of low-level language.


 It mainly consists of instructions (kind of symbols) that only machines could understand.
 In today’s time also assembly language is used in real-time programs such as simulation
flight navigation systems and medical equipment eg – Fly-by-wire (FBW) systems.
 It is also used to create computer viruses.

1952: Autocode

 Developed by Alick Glennie.


 The first compiled computer programming language.
 COBOL and FORTRAN are the languages referred to as Autocode.

1957: FORTRAN

 Developers are John Backus and IBM.


 It was designed for numeric computation and scientific computing.
 Software for NASA probes voyager-1 (space probe) and voyager-2 (space probe) was
originally written in FORTRAN 5.

1958: ALGOL

 ALGOL stands for ALGOrithmic Language.


 The initial phase of the most popular programming languages of C, C++, and JAVA.
 It was also the first language implementing the nested function and has a simple syntax than
FORTRAN.
 The first programming language to have a code block like “begin” that indicates that your
program has started and “end” means you have ended your code.

1959: COBOL

 It stands for COmmon Business-Oriented Language.
 In 1997, 80% of the world’s business ran on Cobol.
 The US internal revenue service scrambled its path to COBOL-based IMF (individual
master file) in order to pay the tens of millions of payments mandated by the coronavirus
aid, relief, and economic security.

1964: BASIC

 It stands for beginner’s All-purpose symbolic instruction code.


 In 1991 Microsoft released Visual Basic, an updated version of Basic
 The first microcomputer version of Basic was co-written by Bill Gates, Paul Allen, and
Monte Davidoff for their newly-formed company, Microsoft.

1972: C

 It is a general-purpose, procedural programming language and the most popular


programming language till now.
 All the code that was previously written in assembly language gets replaced by the C
language like operating system, kernel, and many other applications.
 It can be used in implementing an operating system, embedded system, and also on the
website using the Common Gateway Interface (CGI).

4
 C is the mother of almost all higher-level programming languages like C#, D, Go, Java,
JavaScript, Limbo, LPC, Perl, PHP, Python, and Unix’s C shell.

Some of the other popular programming languages are:

Period Evolution of some other popular Programming Languages

1972 SQL
1978 MATLAB
1983 Objective- C, C++
1990 Haskell
1991 Python
2000 C#
2009 Go
2011 Kotlin
2014 Swift

1.3 Problem Solving through Programming

Let’s understand what a problem is and how a computer helps us in solving a problem.

q Problem - Defined as any question, something involving doubt, uncertainty, difficulty,


situation whose solution is not immediately obvious

The problem is any question that involves doubt, uncertainty, difficulty, a situation whose
solution cannot be immediately found. Problems cannot be solved straightforwardly, and
they need thought processing.

Consider traveling from one city to another city is a problem, and it can be solved with a
series of actions.

q Types of solution for a Problem?


 Algorithm: A solution algorithm assurances an accurate solution
 Heuristic: A heuristic algorithm does not assure an accurate solution.

5
1.3.1 Creative Thinking and Critical Thinking

Creative Thinking

q Proven method for approaching a challenge or opportunity in an imaginative way


q Process for innovation that helps explore and reframe the problems faced, come up
with new, innovative responses and solutions and then take action
q It is generative, nonjudgmental, and expansive
q Thinking creatively, a list of new ideas is generated

Critical Thinking

q Engages a diverse range of intellectual skills and activities that are concerned with
evaluating information, our assumptions and our thinking processes in a disciplined
way so that we can think and assess information more comprehensively
q It is Analytical, Judgmental and Selective
q Thinking critically allows a programmer in making choices

6
1.3.2 Steps Involved in Solving a Problem

The various steps that are involved in solving a problem are:

1) Defining the Problem: State the problem as clearly as possible. For example: “I don’t
have enough money to pay the bills.”Be specific about the behaviour, situation, timing,
and circumstances that make it a problem. For example: “I need to pay the phone and
gas bills, and I don’t have enough money to cover both this month.”
2) Planning the Solution:List all the possible solutions; don’t worry about the quality of
the solutions at this stage.
If you allow yourself to be creative, you may come up with some solutions that you
would not otherwise have thought about. Then go through and eliminate less desirable
or unreasonable solutions.
3) Coding the Program: Choose a suitable programming language that is suitable for you
and code the logic of each solution that you were able to find.
4) Testing the Program: Compile and run each program successfully and check the
answer received. Evaluate how effective the solution was and then decide whether the
existing solution needs to be revised or not.
5) Documenting the Program: Documentation is about lying about all the steps that you
performed to solve a problem. It is necessary so that other programmers or users can
understand what exactly is done to reach a solution.

1.3.3 The Software Development Life Cycle

1) Planning: In the Planning phase, project leaders evaluate the terms of the project. This includes
calculating labor and material costs, creating a timetable with target goals, and creating the
project’s teams and leadership structure.Planning can also include feedback from stakeholders.
Stakeholders are anyone who stands to benefit from the application. Try to get feedback from
potential customers, developers, subject matter experts, and sales reps.Planning should clearly
define the scope and purpose of the application. It plots the course and provisions the team to
effectively create the software. It also sets boundaries to help keep the project from expanding or

7
shifting from its original purpose.

2) Analysis: In analysis Defining requirements is considered part of planning to determine what the
application is supposed to do and its requirements. For example, a social media application would
require the ability to connect with a friend. An inventory program might require a search
feature.Requirements also include defining the resources needed to build the project. For
example, a team might develop software to control a custom manufacturing machine. The
machine is a requirement in the process. After defining requirements analyse the problem and
understand what the problem wants to convey.

3) Design: The Design phase models the way a software application will work. Some aspects of the
design include:
Architecture – Specifies programming language, industry practices, overall design, and use
of any templates or boilerplate
User Interface – Defines the ways customers interact with the software, and how the
software responds to input
Platforms – Defines the platforms on which the software will run, such as Apple, Android,
Windows version, Linux, or even gaming consoles
Programming – Not just the programming language, but including methods of solving
problems and performing tasks in the application
Communications – Defines the methods that the application can communicate with other
assets, such as a central server or other instances of the application
Security – Defines the measures taken to secure the application, and may include SSL traffic
encryption, password protection, and secure storage of user credentials.

4) Implementation: This is the actual writing of the program. A small project might be written by a
single developer, while a large project might be broken up and worked by several teams. Use an
Access Control or Source Code Management application in this phase. These systems help
developers track changes to the code. They also help ensure compatibility between different team
projects and to make sure target goals are being met. The coding process includes many other
tasks. Many developers need to brush up on skills or work as a team. Finding and fixing errors
and glitches is critical. Tasks often hold up the development process, such as waiting for test
results or compiling code so an application can run. SDLC can anticipate these delays so that
developers can be tasked with other duties.

5) Testing and Integration: It’s critical to test an application before making it available to users.
Much of the testing can be automated, like security testing. Other testing can only be done in a
specific environment – consider creating a simulated production environment for complex
deployments. Testing should ensure that each function works correctly. Different parts of the
application should also be tested to work seamlessly together—performance test, to reduce any
hangs or lags in processing. The testing phase helps reduce the number of bugs and glitches that
users encounter. This leads to a higher user satisfaction and a better usage rate.

6) Maintenance: At this point, the development cycle is almost finished. The application is done
and being used in the field. The Operation and Maintenance phase is still important, though. In
this phase, users discover bugs that weren’t found during testing. These errors need to be

8
resolved, which can spawn new development cycles.
In addition to bug fixes, models like Iterative development plan additional features in future
releases. For each new release, a new Development Cycle can be launched

1.4 Writing Algorithms / Pseudocode

An informal definition of an algorithm is:

1.4.1 Properties and Rules of writing Algorithm

Notations
1) Starting point
2) Step Numbers – Positions in Algorithm
3) Incoming Information – Input
4) Control Flow – Order of evaluating Instructions
5) Statements
6) Outgoing Information - Output
7) Ending Point

q Properties of an algorithm
q Finite: The algorithm must eventually terminate
q Complete: Always give a solution when one exists
q Correct (sound): Always give a correct solution

9
q Rules of Writing an Algorithm
q Be consistent
q Have well Defined input and output
q Do not use any syntax of any specific programming language
q Algorithm development process consists of five major steps
q Step 1: Obtain a description of the problem
q Step 2: Analyze the problem
q Step 3: Develop a high-level algorithm
q Step 4: Refine the algorithm by adding more detail
q Step 5: Review the algorithm

1.4.2 Sample Algorithms


q Problem
Develop an algorithm for finding the largest integer among a list of positive integers. The algorithm
should find the largest integer among a list of any values. The algorithm should be general and not
depend on the number of integers
q Solution
a) To solve this problem, we need an intuitive approach
b) First use a small number of integers (for example, five), then extend the solution to
any number of integers
c) The algorithm receives a list of five integers as input and gives the largest integer as
output

10
1.5 Flowcharts

1.5.1 Flowchart Symbols and guidelines for preparing flowcharts


1) Diagrammatic representation
2) Illustrates sequence of operations to be performed
3) Each step represented by a different symbol
4) Each Symbol contains short description of the Process
5) Symbols linked together by arrows
6) Easy to understand diagrams
7) Clear Documentation
8) Helps clarify the understanding of the process

Flowchart Symbols
Terminal:
The oval symbol represents Start, Stop, and Halt in a program. The terminal is used as the
start and stop symbols in the flowchart. The Halt symbol is used to represent an error
condition in the program.

11
Input / Output: A parallelogram represents the Input and Output functions. The
instructions used in the program related to input-output are written in a parallelogram.

Processing: The rectangular box represents the processes. They consist of operations such as
add, subtract, multiply, divide, or any other formulas required as per the program.

Decision: The diamond shape represents the decisions or conditions which include two-way
output, such as if the condition satisfies then "True" or "False. "True / False can also be
represented with “yes”/"no."

Connectors: A small circle represents a connector. It is used when the flowchart needs to be
continued on the nextpage.

Flow lines: Flow lines connect the symbols and direct the reader from the initial stage to the
final stage of the program.

q Guidelines for Preparing Flowchart

q Logical order of requirements


q Ensure that Flowchart has logical Start and Stop
q Direction is from Top to bottom
q Only one flow line is used with Terminal Symbol

12
q Only one flow line should come out of a Process symbol
q Only one flow line should enter a Decision symbol, but multiple lines may leave the
Decision symbol
q Write briefly within Symbols
q Use connectors to reduce number of flow lines
q Avoid intersection of flow lines
q Test Flowchart through simple test data
q Clear, Neat and easy to follow

1.5.2 Sample Flowcharts

Flowchart 1: Sum of two Numbers

Flowchart 2: Finding Factorial of a given number

13
1.6 Pseudo code

Pseudo code is a high level representation of computer program or an algorithm.It is intended


for human reading rather than machine reading.

Pseudo – Imitation / False


Code – Instructions
 Goal: To provide a high level description of the Algorithm
Similar to programming code
Description of the Algorithm
No specific Programming language notations
Pseudo Code transformed into actual program code

1.6.1 Guidelines for Writing Pseudo Code

 Write only one Statement per line


 Capitalize the Keywords
 Indent to show the hierarchy
 End Multiline structure ( IF – ENDIF)
 Keep statements language independent
Example – Pseudo Code for calculating Salary
1. READ name, hourly rate, hours worked, deduction rate
2. Gross pay = hourly rate * hours worked
3. deduction = gross pay * deduction rate
4. net pay = gross pay – deduction

14
5. WRITE name, gross, deduction, net pay

1.6.2 Advantages of Pseudo code

 Easily typed in a Word document


 Easily modified
 Simple to Use and understand
 Implements Structured Concepts
 No special symbols are used
 No specific syntax is used
 Easy to translate into Program

Example: Sum of three numbers

ALGORITHM Flo PSEUDOCO


wch DE:
Step 1: Start
the program art
READ Input
Step 2: A
Accept three READ Input
inputs A,B,C B
READ Input
Step 3: C
Calcula SUM =
te the
1.7 Evolution of C cycle A+B+C
sum by
1.7.1 History of C adding
programming Language
WRITE SUM
q C– General Purpose Programming Language
three
q Developed by Dennis Ritchie in 1972
numbe
q Developed at Bell Laboratories
q Principles takenrsfrom BCPL and CPL
q Structured Programming Language
q C Program
Step 4: Print
q Collection of Functions
the
SUM 15

Step 5: End
q Supported by C library

 Father of C Programming: Dennis Ritchie

Born September 9, 1941


Profession Computer Scientist
Place Bell Labs, US
Known As Father of C & UNIX
Awards q Turing Award(1983)
q National Medal of
Technology(1998)
q IEEE Medal(1990)
q Computer Pioneer
Award(1994)
q Computer History Museum
Fellow(1997)
q Harold Pender Award(2003)

Died October 12, 2011

q Languages developed before C

q CPL – Common Programming Language invented by Martin Richards in 1960’s


q BCPL – Basic Combined Programming Language by Martin Richards in 1966
q B Language – by Ken Thompson & Dennis Ritchie in 1969
q Traditional C – by Dennis Ritchie in 1972
q K&R C – by Kernighan & Dennis Ritchie in 1978
q ANSI C – by ANSI Committee in 1989
q ANSI/ISO C – by ISO Committee in 1990
q C99 – by Standardization Committee in 1999

q Why the Name “C” was given ?

q Many of C’s principles and ideas were derived from the earlier language B
q BCPL and CPL are the earlier ancestors of B Language (CPL is common
Programming Language)
q In 1967, BCPL Language ( Basic CPL ) was created as a scaled down version of
CPL
q As many of the features were derived from “B” Language the new language was
named as “C”.

 Characteristics of C
q Low Level Language Support
q Structured Programming
q Extensive use of Functions
q Efficient use of Pointers

16
q Compactness
q Program Portability
q Loose Typing

q Advantages of C
q Compiler based Language
q Programming – Easy & Fast
q Powerful and Efficient
q Portable
q Supports Graphics
q Supports large number of Operators
q Used to Implement Datastructures

q Disadvantages of C
q Not a strongly typed Language
q Use of Same operator for multiple purposes
q Not Object Oriented

1.8 Summary

This unit summarizes about basics of problem-solving techniques, which could be helpful for
the beginners to learn all the technologies involved in coding programs independent of any
programming language. The content helps the reader to improve his/her skills in creative and
critical thinking, Writing Algorithms, Pseudo code and drawing flowcharts.

17

You might also like