Computer Programming C: UNIT I: Introduction to C

Building Blocks of an Algorithm

C Programming

An algorithm is a sequence of simple steps that can be followed to solve a problem. These steps must be organized in a logical and clear manner. 1. Statements 2. Sequential control 3. Selection or Condition control 4. Repetition or control flow 5. Functions

BUILDING BLOCKS OF AN ALGORITHM

 

An algorithm is a sequence of simple steps that can be followed to solve a problem. These steps must be organized in a logical and clear manner. Algorithms can be designed using the below basic methods.

• Statements

• Sequence

• Selection or Conditional

• Repetition or Control Flow

• Functions

 

1. STATEMENTS

The algorithm is part of the blueprint or plan for the computer program, an algorithm is An effective procedure for solving a problem in a finite number of steps.

An algorithm is a series of specific steps which solve a particular problem. Each word in the definition above is important:

Series: The steps must be done in a particular order and each of the steps must be used (unless the algorithm says otherwise).

Specific: A step must NOT be replaced by a similar step.

Steps: Like a cooking recipe, an algorithm tells you to do things. Each thing you do is called a step.

Solve: An algorithm produces a final result or output which is the solution to a problem.

Particular problem: The algorithm for one problem will not usually solve a different problem. The details defining the problem must be made available at the start of the algorithm. These details are called givens.

Example: A Simple Example of an Algorithm.

Most algorithms have these basic parts:

• Description of Problem

• Set up

• Parameters

• Execution

• Conclusion

The simple example below is to find the sum of the two numbers.

1. Description of Problem: To find sum of the two numbers.

2. Set up: Two numbers required for addition and one variable for storing the results.

3. Parameters:

1. Read 1st number.

2. Read 2nd Number.

4. Execution: Calculate the sum of the two numbers read (let us say two numbers are 'a' and 'b'.

Result a + b

5. Conclusion: The desired result is sum.

 

2. SEQUENTIAL CONTROL

Sequential Control means that the steps of an algorithm are carried out in a sequential manner, where each step is executed exactly once.

Let's take an example of Temperature conversion, we need to obtain the temperature expressed in Fahrenheit degrees and convert it to degrees Celsius. An algorithm to solve this problem would be:

1. Read temperature in Fahrenheit

2. Apply conversion formula

degrees Celsius = (5/9)* (degrees Farenheit‒32)

3. Display result in degrees Celsius

 

3. SELECTION OR CONDITION CONTROL

In Selection Control, only one of a number of alternative steps is executed based on the condition.

Let's take an example to find the biggest number from the two numbers.

1. Read first number 'a'

2. Read second number 'b'

3. IF (a> b) then print 'A is Big'

This is the simplest case of selection control. In fact, in essence it is a sequential form but the third step is only taken when the condition contained in the brackets is true, if it is true then it p true then it print 'A is Big'.

The above example gives us the result when A is always supplied as biggest number, and it will not give any result when the number B is big.

1. Read first number 'a'

2. Read second number 'b'

3. IF (a> b) then print 'A is Big'

ELSE print 'B is Big'

In this case, there is also a decision to be made but a more complex one. If the condition is True it will print 'A is Big' else (if condition is not true) it will print 'B is big'. That is, the condition helps us make a selection which controls, which of the two steps will be executed.

Let us take one more example to find the student grades based on his average marks. Decide on a student's grade based on their average marks.

1. READ avg

2. CASE (avg>=90)

PRINT 'Grade A'

3. CASE (avg>=80)

PRINT 'Grade B

4. CASE (avg>=70)

PRINT Grade C'

5. CASE (avg>=60)

PRINT Grade D'

6. CASE (avg<60)

PRINT Grade F'

In this case, there are five conditions which are checked, one after the other and takes the decision accordingly.

 

4. REPETITION OR CONTROL FLOW

In Repetition, one or more steps are performed repeatedly. This logic is used for producing loops in a program logic, when one or more instructions may be executed several times or depending on some conditions.

Example 1: Compute and print the average of ten numbers.

1. total 0, average = 0

2. FOR 1 to 10

3. Read number

4. total total + number

5. ENDFOR

6. average = total / 10

7. Print average

Example 2: Read numbers and add them up until their total value reaches or exceeds a set value represented by n.

1. WHILE (total < n)

2. DO

3. Read number

4. total = total + number

5. ENDDO

6. Print total

The difference is that in Example 1, we know beforehand the exact number of repetitions that will be carried out (they are 10), but in Example 2, we do not know beforehand (it depends on the particular numbers that will be fed to our program during its execution).

 

5. FUNCTIONS

For complex problems, our goal is to divide the task into smaller and simpler tasks during algorithm design.

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

Good thing about functions is that they are famous with several names. Different programming languages name them differently, for example, functions, methods, sub‒routines, procedures, etc.

Let's start with a program were read an arrays of numbers and then from

that array, we will find the biggest numbers. Given below are the steps to find out the maximum number from a given set of numbers.

Example: Algorithm to find maximum number of an array.

1. Get a list of numbers L1, L2, L3 .... LN

2. Assume L1 is the largest, Set max = L1

3. Take next number Li from the list and do the following

4. If max is less than Li

5. Set max = Li

6. If Li is last number from the list, then

7. Print value stored in max and come out

8. Else repeat same process starting from step 3

If we write function from step 3 to step 8, the same function can be used for several arrays to find the biggest number.

 

Computer Programming C: UNIT I: Introduction to C : Tag: Computer Science : C Programming - Building Blocks of an Algorithm


Computer Programming C: UNIT I: Introduction to C



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation