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

Lec 01 ProblemSolving

The document discusses problem solving and algorithms. It defines problem solving as a structured process to find solutions. It then describes the typical steps of problem solving like analysis, design, implementation, and testing. It also discusses key aspects like the iterative nature and moving from specific to general solutions. The document defines an algorithm as the precise sequence of steps to solve a problem and provides examples. It discusses different ways of representing algorithms like pseudo-code, flowcharts, and actual code. Finally, it covers common algorithm building blocks like sequences, conditionals, and loops.

Uploaded by

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

Lec 01 ProblemSolving

The document discusses problem solving and algorithms. It defines problem solving as a structured process to find solutions. It then describes the typical steps of problem solving like analysis, design, implementation, and testing. It also discusses key aspects like the iterative nature and moving from specific to general solutions. The document defines an algorithm as the precise sequence of steps to solve a problem and provides examples. It discusses different ways of representing algorithms like pseudo-code, flowcharts, and actual code. Finally, it covers common algorithm building blocks like sequences, conditionals, and loops.

Uploaded by

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

1.

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.

b. From Specific to General Solutions:


 Start by solving for positive whole numbers in the decimal to binary conversion.
 If successful, extend the solution to cover both positive and negative real
numbers (handle negative numbers by finding the binary of the corresponding
positive real number and taking its two's complement).

c. Experience Matters:
o Skillful solution to the problem selection based on analysis and experience:

o Selection of the most suitable algorithm by considering runtime analysis.


(Sorting)
o Reuse existing solutions:
o Solutions developed for one problem (e.g., calculating the area of a
circle) can be applied to solve related problems (e.g., finding the area
of a ring).

The solution to the problem is also called an Algorithm (as the results of analysis and
design)
3. Algorithm: (Resulting from Analysis and Design)

Sequence of steps that can be taken to solve a given problem”


“[precise] Sequence of [finite] steps that can be taken to solve a given problem

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)

5. Algorithm Representation: Software developers convey algorithms through


three distinct forms:

a. Pseudo Code - Visualizing Actual Code:


 It closely resembles actual code.
 Advantages: Nearly a programming language, making the transition from
natural language to actual code easier.

 Disadvantages: Lack of standardization; developers must establish their


conventions for elements like input, output, and variable names to ensure code
comprehension.

b. Flow Charts - Graphical Solutions Representation:


 Visual diagrams depicting the algorithm's flow.
 Advantages: More accessible, particularly for those without programming
expertise.
 Disadvantages: Can become unwieldy as the algorithm grows in complexity.

c. Actual Code: - Implementation using a programming language like C.


- Each representation has its own merits and drawbacks.
6. Algorithm building blocks: All categories of programming problems can be
addressed by employing any one of the following elements or their combinations.

1. Sequence: A direct execution of steps in a specified order.


a. For instance, consider the task of taking your age in years as input and
displaying it in days. In this scenario, we rely solely on a sequence of
steps to accomplish the task.
2. Conditionals
a. Example: Toss a coin; if it lands heads, proceed with the task;
otherwise, do not.
b. Example: [If a number is negative, convert it to its binary
representation and then take its 2’s complement.]
3. Loops

a. Example: [Continue filling the tank until it reaches full capacity.]


b. Example: [Divide the number by 2 repeatedly until it becomes zero.
Problem statement: Develop an algorithm that converts the input of years into its
equivalent value in days.

Sample execution
Enter your age: 2
Age in days = 730

Pseudo code Flow chart Actual code


#include <iostream>

using namespace std;

Start int main()


integer age {
int age;
print "Enter your age:" cout << "Enter your age: ";

input age // get age from user


cin>> age;
age = 365 * age
age = 365 * age;
print "Age in days is: ", age
// print age
End cout << "Age in days = "<< age;

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

Pseudo code Flow chart Actual code


#include <iostream>
Start #include <iostream>
integral factorial, number; using namespace std;
integers
Print "Please enter the int main()
number for factorial " ; {
input number; //declaration of variables
while(number > 1 ) int factorial, number;
{ // initializaation of the variable
factorial = factorial * factorial = 1;
number; number = 1;
number =
number - 1; //prompt the user to enter limit of
} integers
cout << "Please enter the number
Print "The factorial is " << for factorial " ;
factorial; cin >> number;
End while(number > 1 )
{
factorial = factorial * number;
number = number - 1;
}

cout << "The factorial is " << factorial;

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.

7. The assignment operator is utilized to assign a value to a variable.


8. We also have arithmatic operators:
a. +, -, *, /, %

A variable comprises a datatype, a name, and a value or data.

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

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu


// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:


// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to the source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing
Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the
.sln file

You might also like