0% found this document useful (0 votes)
45 views

UNIT-1 C Programming

Uploaded by

dhanushadas2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

UNIT-1 C Programming

Uploaded by

dhanushadas2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

What is an Algorithm?

An algorithm is a step-by-step and logical approach that defines a systematic


process for computers to solve a specific problem. It consists of a set of rules
defining how a task will be executed to get the expected results. Algorithms
are conceptual and can be described using language or flowcharts. We can
implement them in different programming languages. When we use a computer
to solve a specific problem, we need to tell these steps to the solution clearly to
the computer.

For example, here’s an algorithm to add two numbers:

Step 1: Start

Step 2: Take two number inputs

Step 3: Add both the numbers using the + operator

Step 4: Display the result

Step 5: End

What is a Program?
A program is a set of instructions a computer follows to perform a specified
task. Many programming languages can be used to write computer programs.
Some popular programming languages include Python, Java, C+
+, JavaScript, PHP, and Ruby. These high-level programming languages are
human-readable and writable. These languages are converted into low-level
machine languages by compilers, interpreters, and assemblers within the
computer system.

A program tells the computer how to accept input, manipulate that input, and
display the output in some form that humans find useful.

For Example, in a word processor:

 The input will be the characters you type from the keyboard
 The program will format the text and correct the spelling
 Well-organized will be displayed on the screen as output
Algorithm vs Program: Difference Between Program and
Algorithm
 Computer algorithms solve the problem while computer programs
implement them in a form that a computer can execute. Here are the
main differences between algorithms and programs :

Algorithm Program

It refers to a set of instructions for a computer to


It is a well-defined, step-by-step, logical follow. A program can implement many
procedure for solving a given problem. algorithms, or a program can even contain no
algorithms.

An algorithm provides abstract steps for


processing one sequence of related The constituents of a program may not be
information into a different sequence of conceptually related.
derived information.

It could be written in any programming language,


It is written using plain English language and
such as Python, Java, C++, JavaScript, or any other
can be understood by those from a non-
language, depending on the particular task the
programming background.
program is designed for.

We write computer programs in a computer


It can be expressed in natural language, flow
language. Then a compiler or interpreter translates
charts, pseudocode, and various
it into a language that any computer system
programming languages.
understands.

An algorithm can be executed by a person. A program is always executed by a computer.

Problem Solving using Computer


A computer is a tool to solve a problem.

Problem solving is the process of transforming the description of a problem into


a solution by using our knowledge of the problem domain and by relying on our
ability to select and use appropriate problem-solving strategies, techniques and
tools.

Programming is a problem solving activity. When we write a program, we are


actually writing an instruction for the computer to solve something for us. Using
a computer as a problem solving tool following steps are involved
1. Problem Definition

A clearly defined problem is already half the solution. Computer programming


requires us to define the problem first before we even try to create a solution.

2. Problem Analysis:

Analyzing the problem require us to identify the following:

 Input(s) to the problem, their form and the input media to be used
 Output(s) expected from the problem, their form and the output media to
be used
 Special constraints or conditions (if any)
 Any formulas or equations to be used

For example, Compute the average marks obtained by students in "C-


Programming".
Inputs: Marks of individual students
Outputs: The average mark of students
Formula: Average = Total Marks / No of students
i.e. A =T/N
4. Coding: Coding is the real job of programmer. The algorithm to solve a
problem which is described by pseudo-code or flow chart is converted into
actual programming language code. The code written by programmer by using
any programming language like C is called the source code or source program.

5. Compilation and execution: The source code written in any programming


language is not directly executed by the computer. It should be translated into
to the machine readable format i.e. actual machine language. The process of
translation of source code into the target code is called the compilation. Each
programming language has its own compiler program that translates the
source code into its target code. The converted program in actual machine
language is then executed by the computer which is known as program
execution.

6. Debugging and Testing: A written program may have errors, some errors
can be detected by the language compilers and some errors can not be
identified by the compiler and occured during the program run. Common types
of errors are

 Syntax Errors: Identified by compiler at the program compilation time.


 Logical Errors: Not identified by the compiler at compile time and
identified at the execution time. e.g. misuse of operators

So testing is the process of checking the program for its correct functionality by
executing the program with some input data set and observing the output of
the program.
7. Documentation: From the start of the problem solving to the end of the
implementation of the program, all the tasks should be documented i.e. kept
for future reference. It is also the important part of the problem solving or
program development. Documentation may be of two types

 Technical Documentation known as programmer's documentations


which includes the problem analysis to implementation details for that
program. It is needed for future reference for any modification, update of
the program.

 User manual is the documentation prepared for the end-user of the


program that guides the user how to operate the program.

Phases of Problem Solving Process


Step 1: Define the Problem

 What is the problem?


 How did you discover the problem?
 When did the problem start and how long has this problem been going on?
 Is there enough data available to contain the problem and prevent it from
getting passed to the next process step? If yes, contain the problem.

Step 2: Clarify the Problem

 What data is available or needed to help clarify, or fully understand the problem?
 Is it a top priority to resolve the problem at this point in time?
 Are additional resources required to clarify the problem? If yes, elevate the problem
to your leader to help locate the right resources and form a team.
 Consider a Lean Event (Do-it, Burst, RPI, Project).
 ∙Ensure the problem is contained and does not get passed to the next process step.

Step 3: Define the Goals

 What is your end goal or desired future state?


 What will you accomplish if you fix this problem?
 What is the desired timeline for solving this problem?

Step 4: Identify Root Cause of the Problem

 Identify possible causes of the problem.


 Prioritize possible root causes of the problem.
 What information or data is there to validate the root cause?

Step 5: Develop Action Plan

 Generate a list of actions required to address the root cause and prevent
problem from getting to others.
 Assign an owner and timeline to each action.
 Status actions to ensure completion.
Step 6: Execute Action Plan

 Implement action plan to address the root cause.


 Verify actions are completed.

Step 7: Evaluate the Results

 Monitor and Collect Data.


 Did you meet your goals defined in step 3? If not, repeat the 8-Step Process.
 Were there any unforeseen consequences?
 If problem is resolved, remove activities that were added previously to contain the
problem.

Step 8: Continuously Improve

 Look for additional opportunities to implement solution.


 Ensure problem will not come back and communicate lessons learned.
 If needed, repeat the 8-Step Problem Solving Process to drive further
improvements.

Why c is called as the top down approach?


C programming uses top down approach to solve a problem.
Top down approach starts with high-level design and ends with the low-level
implementation.
In top down approach, we use following approach to solve any problem.
1.First we will make a high level design of a problem statement.
2.After that we will write the main function.
3.From the main function we will call the sub functions.
Generally, the large program is divided into small small sub functions(each function will do
a specific task) which improves the modularity of a program.
Using that we can easily debug the program.
4.Later we will implement(code) all the sub functions based on requirements. Thats why c
is called the top down approach.
Example

Write a c program to implement a simple calculator.

High-Level Design
declare two integers
scan the values from the user
call addition
call subtraction
call multiplication
call division
addition, subtraction, multiplication, division are sub functions.

Write main function and call sub functions

int main()
{
int a,b;

scanf("%d%d", &a, &b);

add(a,b);
sub(a,b);
mul(a,b);
div(a,b);

return 0;
}

# Implement the sub functions


void add(int a, int b)
{
printf("%d + %d = %d", a+b);
}

void sub(int a, int b)


{
printf("%d - %d = %d", a-b);
}

void mul(int a, int b)


{
printf("%d * %d = %d", a*b);
}

void div(int a, int b)


{
if(b != 0) //division by zero is undefined.
printf("%d / %d = %d", a/b);
}
Algorithm Design
Algorithm design refers to a method or process of solving a problem. It is the design of
algorithms that is part of many solution theories. In short, your design is what you use
to solve the problem. Algorithms get you to the solution you desire.
There are many different types of algorithms. But at their core, they are all the same.
Even in computer programming and science.
The algorithm definition computer science states that an algorithm is a list or set of
rules used to perform tasks or solve problems. It has the same meaning in CS that it
does in the kitchen while baking a cake. You’re given a set of variables and a list of
steps. It’s up to you to follow them.
Types of Algorithms
Now that we’ve answered the question: What is an algorithm in computer science? Let’s look
at the different algorithm types.
Brute force algorithms
These are computer algorithms that try all possible solutions until you find the right one.
Algorithms work by computing and problem solving. Because of the way they work, this
algorithm is widely used.
Divide and conquer algorithms
This algorithm divides the problem into smaller problems (subproblems) of the same type. You
then solve the smaller problems and combine the solutions to solve the original problem.
Dynamic programming algorithms
This is a lot like the algorithm that divides and conquers. It breaks a complex problem into a
series of smaller subproblems. Then it solves each of the subproblems once. It stores the
solutions for future use.
Greedy algorithms
Greedy algorithms are used to find the best solution at the local level, but with the intent on
locating a solution for the entire problem. Of the many algorithms out there, this one is good for
solving optimization issues.
Randomized algorithms
A good example of an algorithm that uses random numbers for computation problems is
randomized algorithms. They use random numbers once to find a viable solution.
Recursive algorithms
You use a recursive algorithm to solve the simplest version of a problem to then solve more
complicated versions of the problem. You do this until you find a solution for the original
problem.
Search algorithms
A search algorithm solves a search problem. It retrieves stored information within a particular
data structure. It locates where it is stored.
Sorting algorithms
You use sorting algorithms to rearrange a well defined list of elements into an order. Quicksort
is one of the most efficient algorithms of this kind.
Program Verification
Reviewing any software to find faults is known as Software Verification. Verification is
the process of checking that software achieves its goal without any bugs. It is the
process to ensure whether the product that is developed is right or not. The reviewing of
a document can be done from the first phase of software development i.e. software
requirement and analysis phase where the end product is the SRS document. There are
many methods for practicing the verification of the software like peer-
reviews, walkthroughs, inspections, etc. that can help us in the prevention of
potential faults otherwise, it may lead to the failure of the software.
Methods of Verification
Here are some of the common methods that are required for Verification are listed
below.
 Peer Reviews
 Walk Through
 Inspections
Peer Reviews
The easiest method and most informal way of reviewing the documents or the
programs/software to find out the faults during the verification process is the Peer-
Review method. In this method, we give the document or software programs to others
and ask them to review those documents or software programs where we expect their
views about the quality of our product and also expect them to find the faults in the
program/document. The activities that are involved in this method may include SRS
document verification, SDD verification, and program verification. In this method, the
reviewers may also prepare a short report on their observations or findings, etc.
Walk-Through
Walk-throughs are a formal and very systematic type of verification method as
compared to peer review. In a walkthrough, the author of the software document
presents the document to other persons which can range from 2 to 7. Participants are
not expected to prepare anything. The presenter is responsible for preparing the
meeting. The document(s) is/are distributed to all participants. At the time of the meeting
of the walk-through, the author introduces the content in order to make them familiar
with it and all the participants are free to ask their doubts.
Inspections
Inspections are the most structured and formal type of verification method and are
commonly known as inspections. A team of three to six participants is constituted which
is led by an impartial moderator. Every person in the group participates openly, and
actively, and follows the rules about how such a review is to be conducted. Everyone
may get time to express their views, potential faults, and critical areas. After the
meeting, a final report is prepared after incorporating necessary suggestions from the
moderator.

Algorithm Analysis and Notations


Worst, Average and Best Case Analysis of Algorithms
In the previous post, we discussed how Asymptotic analysis overcomes the problems of
the naive way of analyzing algorithms. But let’s take an overview of the asymptotic
notation and learn about What is Worst, Average, and Best cases of an algorithm:
Popular Notations in Complexity Analysis of Algorithms
1. Big-O Notation
We define an algorithm’s worst-case time complexity by using the Big-O notation, which
determines the set of functions grows slower than or at the same rate as the expression.
Furthermore, it explains the maximum amount of time an algorithm requires to consider
all input values.
2. Omega Notation
It defines the best case of an algorithm’s time complexity, the Omega notation defines
whether the set of functions will grow faster or at the same rate as the expression.
Furthermore, it explains the minimum amount of time an algorithm requires to consider
all input values.
3. Theta Notation
It defines the average case of an algorithm’s time complexity, the Theta notation
defines when the set of functions lies in both O(expression) and Omega(expression),
then Theta notation is used. This is how we define a time complexity average case for
an algorithm.
Measurement of Complexity of an Algorithm
Based on the above three notations of Time Complexity there are three cases to
analyze an algorithm:
1. Worst Case Analysis (Mostly used)
In the worst-case analysis, we calculate the upper bound on the running time of an
algorithm. We must know the case that causes a maximum number of operations to be
executed. For Linear Search, the worst case happens when the element to be searched
(x) is not present in the array. When x is not present, the search() function compares it
with all the elements of arr[] one by one. Therefore, the worst-case time complexity of
the linear search would be O(n).
2. Best Case Analysis (Very Rarely used)
In the best-case analysis, we calculate the lower bound on the running time of an
algorithm. We must know the case that causes a minimum number of operations to be
executed. In the linear search problem, the best case occurs when x is present at the
first location. The number of operations in the best case is constant (not dependent on
n). So time complexity in the best case would be Ω(1)
3. Average Case Analysis (Rarely used)
In average case analysis, we take all possible inputs and calculate the computing time
for all of the inputs. Sum all the calculated values and divide the sum by the total
number of inputs. We must know (or predict) the distribution of cases. For the linear
search problem, let us assume that all cases are uniformly distributed (including the
case of x not being present in the array). So we sum all the cases and divide the sum by
(n+1). Following is the value of average-case time complexity.

Average Case Time = \sum_{i=1}^{n}\frac{\theta (i)}{(n+1)} = \frac{\theta (\


frac{(n+1)*(n+2)}{2})}{(n+1)} = \theta (n)
Which Complexity analysis is generally used?
Below is the ranked mention of complexity analysis notation based on popularity:
1. Worst Case Analysis:
Most of the time, we do worst-case analyses to analyze algorithms. In the worst
analysis, we guarantee an upper bound on the running time of an algorithm which is
good information.
2. Average Case Analysis
The average case analysis is not easy to do in most practical cases and it is rarely
done. In the average case analysis, we must know (or predict) the mathematical
distribution of all possible inputs.
3. Best Case Analysis
The Best Case analysis is bogus. Guaranteeing a lower bound on an algorithm doesn’t
provide any information as in the worst case, an algorithm may take years to run.

Interesting information about asymptotic notations:

A) For some algorithms, all the cases (worst, best, average) are asymptotically the
same. i.e., there are no worst and best cases.
 Example: Merge Sort does Θ(n log(n)) operations in all cases.
B) Where as most of the other sorting algorithms have worst and best cases.
 Example 1: In the typical implementation of Quick Sort (where pivot is chosen as a
corner element), the worst occurs when the input array is already sorted and the
best occurs when the pivot elements always divide the array into two halves.
 Example 2: For insertion sort, the worst case occurs when the array is reverse
sorted and the best case occurs when the array is sorted in the same order as
output.
Examples with their complexity analysis:
1. Linear search algorithm:

 C
 C++
 Java
 Python3
 C#
 PHP
 Javascript

// C implementation of the approach


#include <stdio.h>

// Linearly search x in arr[].

// If x is present then return the index,

// otherwise return -1

int search(int arr[], int n, int x)

int i;

for (i = 0; i < n; i++) {

if (arr[i] == x)

return i;

return -1;

/* Driver's code*/

int main()

{
int arr[] = { 1, 10, 30, 15 };

int x = 30;

int n = sizeof(arr) / sizeof(arr[0]);

// Function call

printf("%d is present at index %d", x,

search(arr, n, x));

getchar();

return 0;

Output
30 is present at index 2

Time Complexity Analysis: (In Big-O notation)


 Best Case: O(1), This will take place if the element to be searched is on the first
index of the given list. So, the number of comparisons, in this case, is 1.
 Average Case: O(n), This will take place if the element to be searched is on the
middle index of the given list.
 Worst Case: O(n), This will take place if:
 The element to be searched is on the last index
 The element to be searched is not present on the list

You might also like