Module 1.2
Module 1.2
Prof. Sarju S
23 September 2024
Module 1
Page 2
Module 1
Page 3 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
INTRODUCTION
Page 4 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Problem-Solving with the Input/Process/Output Model
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Problem Solving
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Problem Solving - Steps
Page 7 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Understanding the problem
Page 8
Understanding the problem
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Understanding the problem
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Formulating a Model
Page 11
Formulating a Model
Page 12 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Formulating a Model
► Challenge
► Letter grades (e.g., A, B-, C+) cannot be directly added or
divided.
► Solution
► Assign Values: Convert letter grades to numerical
equivalents (e.g., A+ = 12, F = 0).
► Model: 𝐴𝑣𝑒𝑟𝑎𝑔𝑒2 = (𝑦1 + 𝑦2 + ⋯ + 𝑦𝑛)/𝑛.Output: A number
from 0 to 12.Final
► Output: Convert 𝐴𝑣𝑒𝑟𝑎𝑔𝑒2 to a percentage or map it back
to a letter grade using a lookup table.
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Developing an algorithm
Page 14
Developing an algorithm
Page 15 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Do you know
Source: https://en.wikipedia.org/wiki/Algorithm
Page 16 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Different Patterns in Algorithm
Page 17 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Using Algorithms to Solve Problems
Page 18 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Using Algorithms to Solve Problems
► Identify any other data and constants required to solve the problem
► The average of numbers is defined as:
► Sum of all inputs / total number of inputs
► Here the other data to be computed is the sum. The total number of inputs is ‘3’,
which is a constant in this case.
Page 19 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Using Algorithms to Solve Problems
Page 20 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Using Algorithms to Solve Problems
► Write an algorithm
Step 1: Start
Step 2: Read three numbers and store values as Number1, Number2 and Number 3
Step 6: End
Page 21 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Writing the Program
Page 22
Writing the Program
Page 23 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Writing the Program
Page 24 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Good Coding Practices
► Write Clear and Descriptive Comments
► Use comments to explain the purpose of the code,
especially for complex logic.
► Use Meaningful Variable and Function Names
► Choose descriptive names that reflect the role of the
variable or function (e.g., total_sum,
calculate_average).
► Keep Code DRY (Don’t Repeat Yourself)
► Avoid duplicating code by reusing functions and logic
whenever possible.
► Consistent Code Formatting
► Use consistent indentation, spacing, and line breaks for
better readability.
Page 25 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Good Coding Practices
► Write Modular Code
► Break your program into smaller, reusable functions or
modules that perform specific tasks.
► Avoid Magic Numbers
► Replace hard-coded numbers with named constants for
better clarity (e.g., MAX_LIMIT = 100).
► Optimize for Readability Over Cleverness
► Write code that is easy to understand, rather than trying to
be overly clever or complex.
► Follow Coding Standards
► Adhere to industry-specific coding guidelines like PEP 8 for
Python or the coding style guide for your language.
Page 26 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Test the Program
Page 27
Test the Program
► After writing and compiling a program, the next
step is to verify if it solves the intended problem.
► Running a program means instructing the
computer to execute the compiled code.
► Correct results indicate the program is functioning
properly, but issues may arise with certain inputs.
► Incorrect results can stem from:
► Improper conversion of the algorithm into code.
► A flawed algorithm that doesn’t handle all situations.
► Instructions executed in the wrong order.
► These issues are referred to as bugs.
Page 28 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Test the Program
► Bugs: Errors in a program causing it to fail or
produce incorrect/undesirable results.
► Fixing bugs is the programmer’s responsibility.
► Use multiple test cases to find bugs, forming a test
suite.
► Debugging: The process of finding and fixing errors
in code.
► Debugging can be time-consuming but is essential for
ensuring a program works as expected.
► Following careful steps during problem analysis,
algorithm design, and coding reduces bugs.
► This results in an easier and more efficient
debugging process.
Page 29 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Test the Program
Page 30 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Test the Program
► Compare Outputs:
► Compare the program's output to the expected average.
► Identify any discrepancies (e.g., if the output is 10 instead of
20).
Page 31 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Test the Program
► Review the Code:
► Examine the lines where calculations are performed:
► Check how total_sum is computed.
► Ensure that all intended variables (num1, num2, and num3) are
included in the calculation.
► Identify the Bug:
► Notice that the calculation for total_sum is missing num3:
► This indicates that the bug lies in the way the total is calculated.
► Fix the Bug:
► Modify the code to include num3 in the sum:
► Test Again:
► Return the program after the fix. Verify that the output now
matches the expected average of 20.
Page 32 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Evaluating the Solution
Page 33
Evaluating the Solution
Page 34 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
References
► https://peps.python.org/pep-0008/
Page 35 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Thank You
Prof. Sarju S
Department of Computer Science and Engineering
St. Joseph’s College of Engineering and Technology, Palai (Autonomous)
[email protected]
Page 36 Disclaimer - This document contains images/texts from various internet sources. Copyright belongs to the respective content creators.
Document is compiled exclusively for study purpose and shall not be used for commercial purpose.