Nothing Special   »   [go: up one dir, main page]

Module II Problem Solving Techniques: CDS101 Introduction To Computers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Module II Problem Solving Techniques

CDS101 Introduction To Computers


5 Basic Elements of C Programming
5 Basic Elements Of Programming

Programming is a process for creating different sets of instructions. There are various factors that
must be present in order to build any instruction. So any programming language is made up of
the five basic elements of the instructions.
Variables: variables in programming tells how the data is represented which can be range from
very simple value to complex one. The value they contain can be change depending on condition.
As we know program consist of instructions that tell the computer to do things and data the
program use when it is running. Data is constant with the fixed values or variable. They can hold a
very simplex value like an age of the person to something very complex like a student track
record of his performance of whole year.
Loops: we can define loop as a sequence of instructions that are repeated continuously till a
certain condition is not satisfied. How a loop start understand this first a certain process is done,
to get any data and changing it after that applied condition on the loop is checked whether
counter reached to prescribed number or not. Basically a loop carry out execution of a group of
instruction of commands a certain number of times. There is also a concept of infinite loop which
is also termed as endless loop is a piece of code that lack from functional exit and goes to repeat
indefinitely.
Conditionals: conditionals specify the execution of the statements depending on the
whether condition is satisfied or not. Basically it refers to an action that only fulfil when
the applied condition on instructions satisfied. They are one of the most important
components of the programming language because they give freedom to program to act
differently every time when it execute that depends on input to the instructions.
Input/output: the element of computer programming allow interaction of the program
with the external entities. Example of input/output element are printing something out to
the terminal screen, capturing some text that user input on the keyboard and can be
include reading and writing files.
Subroutines and functions: the element of the programming allow a programmer to use
snippet of code into one location which can be used over and over again. The primary purpose
of the functions is to take arguments in numbers of values and do some calculation on them
after that return a single result. Functions are required where you need to do complicated
calculations and the result of that may or may not be used subsequently used in an expression.
If we talk about subroutines that return several results. Where calls to subroutines cannot be
placed in an expression whether it is in the main program where subroutine is activated by
using CALL statement which include the list of inputs and outputs that enclosed in the open and
closed parenthesis and they are called the arguments of the subroutines. There are some of the
rules follow by both to define name like start with the letters. The name should be different
that used for variables and functions.
Problem Solving
https://ccmit.mit.edu/problem-solving/
The following are six steps that must be followed to solve a
problem using computer

1. Problem Analysis
2. Program Design - Algorithm, Flowchart and Pseudocode
3. Coding
4. Compilation and Execution
5. Debugging and Testing
6. Program Documentation
Introduction To Problem Solving:
• Steps For Problem Solving( analyzing the Problem, Developing the
algorithm,coding,testing and debugging).
Representation of algorithms using
• Flow chart and
• Pseudo code
Decomposition
Introduction

Computers is machine not only use to develop the software. It is also used for solving various
day-to-day problems. Computer cannot solve problem by themselves. It solve the problem on
basic of the step by step instructions given by us. Thus the success of a computer in solving
problem depends on how correctly and precisely we-
• Identify the problem
• Designing and Developing an algorithm and
• Implement ting the algorithm do develop a program using any programming language.
• Problem solving is an essential skill that everyone should know.
1.Analysing

The following are steps involves in problem analysis

a. Understand your problem


b. Break the problem
c. Define problem goals
d. Decide how to measure progress towards goals.
1. Understand Your Problem:
You must understand the issue, problem or symptom you are experiencing before you can
realistically try to figure out what to do about it. As a first step towards self – help, take steps
to understand the nature of your problem.
2. Break the Problem Down Into Small Parts:
Even when you understand what your problem is, it may be too big and too well established
for you to figure out how to fix all at once. Instead of trying to tackle the entire problem all at
once, break it down into manageable parts. Then, make a plan for how you will fix or address
each part separate
3. Define Problem Goals:
For each of your small manageable problem parts, figure out what your goals are; where you
want to end up at the end of the self-help process for each part of your problem. If you don't
know what you are working towards, you will never know when you've arrived there.
4. Decide How To Measure Progress Towards Goals: Find ways to measure progress you make
towards accomplishing each of your problem goals, so that you will always know:
a. What your problem starting point looked like,
b. How far you've come towards meeting your goals at any given moment, and
c. How you'll know when you've met your goals and are done.
2.Algorithm

In computer systems, an algorithm is basically an instance of logic written in software by software


developers, to be effective for the intended "target" computer(s) to produce output from given
(perhaps null) input.
An optimal algorithm, even running in old hardware, would produce faster results than a non-optimal
(higher time complexity) algorithm for the same purpose, running in more efficient hardware; that is
why algorithms, like computer hardware, are considered technology
Characteristics of a good algorithm:
• Precision
• Uniqueness
• Finiteness
• Input
• Output
Representation of Algorithm

There are two common methods of representing an algorithm-


• Flowchart
• Pseudocode
Flowchart-A visual representation of an algorithm.
There are standardized symbols to draw flowcharts.
Start/End-Also called “Terminator symbol. It indicates
Where the flow starts and ends.
Process-Also called “Action Symbol", It represents a process,action,or a single step.
Decision-A decision or branching point, usually a yes/no or True/False question is asked,
and based on the answer the path gets split into two branches.
Input/Output-Also called data symbol, this parallelogram shape is used to input or output
data.
Arrow-Connector to show order of flow between shapes.
Question: Write an algorithm to find the square of a number.
Algorithm to find square of a number.
Step 1: Input a number and store it to num.
Step 2:Compute num * num and store it in square
Step 3:Print square.
Pseudocode

In computer science, pseudocode is a plain language description of the steps in an algorithm or another
system. Pseudocode often uses structural conventions of a normal programming language, but is
intended for human reading rather than machine reading. Pseudocode for calculating area and
perimeter of a rectangle.
Keywords are used in pseudocode
Input
Compute
Print
Increment
If/Else
While
True/False
Flow of Control:
The process can flow in flowchart
• Sequence
• Selection
• Repetition
Basic Shapes and Symbols of Flowchart
3.Coding

Once an algorithm is finalized, it should be coded in high-level programming language as


selected by the programmer. The ordered set of instructions are written in that
programming language by the following its syntax.
The Syntax is the set of rules or grammar that governs the formulation of the statements
in the language, such as spelling ,order of words,punctuation,etc.
Source code: A program written in high-level language is called source code.
Compilation and Execution
We need to translate the source code into machine language using a compiler or an
interpreter so that it can be understood by the computer.
4.Testing and Debugging

Testing
The Program created should be tested on various parameters.
The program should meet the requirements of the user
It must respond within the expected time.
It should generate correct output for all possible inputs
In the presence of syntactical errors, no output will be obtained.
In case the output generated is incorrect, then the program should be checked for logical errors, if
any.
Debugging
• The errors or defects found in the testing phases are debugged or rectified and the program is
again tested. This continues till all the errors are removed from the program.
Decomposition

Decomposition is a process to ‘decompose’ or breakdown a complex problem into smaller


subproblems.It is useful when we have to solve a complex or big problem.
Once the individual sub-problems are solved, it is necessary to test them for their correctness
and integrate them to get the complete solution
Workout

Write a program to compute the radius of a circle. Derive your formula from the given
equation: A=πr², then display the output.
Swapping of 2 variables without using temporary (or 3rd variable).
Design a program to find the circumference of a circle. Use the formula: C=2πr, where π
is approximately equivalent 3.1416.
Write a program that exchanges the value of two variables: x and y. The output must
be: the value of variable y will become the value of variable x, and vice versa.
Write a program that converts an input inch(es) into its equivalent centimeters. Take
note that one inch is equivalent to 2.54cms.
Write a program the converts the input Celsius degree into its equivalent Fahrenheit
degree. Use the formula: F = (9/5) *C+32.
Assignments
Write an algorithm to display the sum of two numbers entered by user, using both
pseudocode and flowchart.
Write an algorithm to calculate area and perimeter of a rectangle, using both
pseudocode and flowchart. Pseudocode for calculating area and perimeter of a
rectangle.

You might also like