Lec 01 ProblemSolving
Lec 01 ProblemSolving
Problem-Solving:
- Q: How do we solve a problem?
- Ans: Follow a structured procedure to find a solution. For example, consider the task of
converting Decimal to Binary; our goal is to find the solution.
2. Steps for Computing Problem-Solving:
a. Analysis:
- Understand the problem, focusing on Decimal to Binary conversion.
1. Define inputs and outputs.
a. Prompt the user for input and display the output.
User input: "Enter the decimal number: 22"
Binary equivalent: "(100110)₂"
2. Determine if the number is positive or negative.
3. Identify if it is a whole or decimal number.
4. Establish the logic for decimal to binary conversion.
b. Design:
- Outline potential solutions using pseudo-code or a flowchart.
a. Take the user's decimal input.
b. Divide by 2; record quotient and remainder.
c. Repeat the process on the quotient until it's zero.
d. Write the remainders in reverse order to form the final
result.
c. Implementation:
- Convert the chosen solution into actual C/C++ code.
#include <iostream>
using namespace std;
int main() {
int num, array[10], k = 0, n;
cout << "Enter any number greater than 0:";
cin >> num;
while (num > 0)
{
array[k] = num % 2;
num = num / 2;
k++;
}
cout << "Binary representation of the entered number is:";
for (n = k - 1; n >= 0; n--)
{
cout << array[n];
}
}
d. Test:
- Evaluate the implemented solution with different inputs/data. If satisfactory, it's okay;
otherwise, go back to steps 1 to 4 for further refinement.
2. Property of problem-solving process/ Key Aspects of Problem-Solving
a. Iterative Approach:
Repeatedly go through analysis, design, implementation, and testing phases.
A repetitive process involving exploration/Search and discovery.
c. Experience Matters:
o Skillful solution to the problem selection based on analysis and experience:
The solution to the problem is also called an Algorithm (as the results of analysis and
design)
3. Algorithm: (Resulting from Analysis and Design)
Steps:
- Break down into smaller steps or subtasks.
- For instance, the detailed steps to move from one point to another.
Sequence
- A precisely ordered set of steps to solve a particular problem.
- For example, consider the algorithm for frying an egg (or converting binary to
decimal)
- Heat a frying pan by pouring oil into it.
- Crack the egg and place it in the frying pan.
- Cook the egg until it is fried.
4. Examples of Algorithm:
o Decimal to binary conversion
o Sorting (Bubble sort)
o Searching (binary search)
Sample execution
Enter your age: 2
Age in days = 730
return 0;
}
Example 2:
Problem Statement: Determine the factorial of the given input number.
Sample execution:
Please enter the number for factorial: 4
Factorial : 24
return 0;
}
Variable: "Balti"
1. The program is currently running within the computer's memory (RAM/cache) under
the control of the processor.
2. The program is comprised of two main components:
a. Data
b. Instructions or code
3. Memory dedicated to instructions and data is divided into two parts:
a. Address
b. Contents
4. For data, such as age data stored in memory:
a. Contents can be accessed using an address or label.
b. It is more convenient for programmers to access memory locations using labels.
c. Labels, in this context, are also referred to as variables.
d. A variable implies that the contents can change.
Example:
Declare an integral variable "a" and assign it the value 3:
Integer a = 3
Home work: write pseudo code:
1) Find sum of 3 numbers
Sample execution:
Please enter first number: 10
Please enter second number: 20
Sum of entered numbers : 30
2) Find Area of circle and Area of rectangle
Sample executiton:
Enter radius: 3
Area of the circle : 28.26