Lec 02 Variable Sequence
Lec 02 Variable Sequence
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.
• 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)
• 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.
• 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;
}
Variable: "Balti"
• The program (actual code) is running within the computer's memory
(RAM/cache) under the control of the processor.
Example:
Declare an integral variable "age" and assign it the value 19:
OR
integer age = 19 // variable definition or initialization statement
Problem statement: Create an algorithm that takes the user's age in years as
input and displays it on the next line.
Sample Execution:
Kindly input your age in years: 20
The age you entered is 20.
Pseudo code
Start
integer age
input age
End
Problem statement: Develop an algorithm that converts the input of years into its
equivalent value in days. (use of assignment and arithmetic operators)
Sample execution
Enter your age: 2
Age in days = 730
Pseudo code
Start
integer age, ans
input age
End
Sample execution:
Please enter first quiz: 9
Please enter second number: 8
Please enter second number: 7
Average of three quiz exams : 8
Sample executiton:
Enter radius: 3
Area of the circle : 28.26
Sample executiton:
Enter length of rectangle : 3
Enter the width of circle : 4
Area of the rectangle : 12
Programming Fundamentals – Spring 2024
(BS-IT-F23 Morning)
Assignment # 1 part I (Sequential statements )
Instructions:
• Attempt the following tasks exactly in the given order.
• Indent your pseudo-code properly.
• Use meaningful variable names. Use the camelCase notation to name variables.
• Use meaningful prompt lines/labels for all input/output that is performed by your algorithms.
You are required to write algorithm in pseudo-code form and draw flowchart for each of the following
tasks:
Task # 1
A company has determined that its annual profit is typically 13 percent of total sales. Design an algorithm
that asks the user to enter the amount of total sales, and then determines and displays the annual profit
that will be made from that amount.
Task # 2
Design an algorithm that will ask the user to enter the amount of a purchase. The algorithm should then
compute the federal and provincial sales tax. Assume that the federal sales tax is 6 percent and the
provincial sales tax is 4 percent. The algorithm should display the amount of the purchase, the federal
sales tax, the provincial sales tax, the total sales tax, and the total of the sale (which is the sum of the
amount of purchase plus the total sales tax).
Task # 3
A car’s mileage in kilometers per liter (KMPL) can be calculated with the following formula:
Task # 4
Design an algorithm that asks the user how many eggs he/she has bought and then tells the user how
many dozen eggs he/she has and how many extra eggs are left over. A sample run of your algorithm is
given below. Text shown in red is entered by the user.
Sample run:
Hint: Use the division operator (/) and the modulus (%) operator.