0% found this document useful (0 votes)
14 views5 pages

Problem Solving in Programming Basics

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Problem Solving in Programming Basics

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Problem Solving and Analysis

Problem Solving Problem-solving is the art of breaking down a complex task into manageable,
logical steps. Think of it as a recipe. You can't just throw ingredients in a bowl and expect a
cake; you need a precise sequence of actions. Similarly, in programming, a systematic approach
is crucial.

 Example: Calculating the area of a circle.


o Understand: I need to find the area of a circle. The input is the radius, and the
output is the area.
o Plan: The formula for the area of a circle is $A = \\pi \* r^2$. I will need to get
the radius from the user, apply this formula, and then display the result.
o Execute: Write the code to perform these steps.
o Review: Test with a known radius, like 1, and see if the output is approximately
3.14.

Problem Analysis Chart (PAC) The PAC is a formal method to document your plan. It helps
ensure you have all the necessary information before you start coding.

 Example: Calculating the total cost of an item with sales tax.

Given Required
Information Information Processing (Logic/Formulas) Constraints
(Inputs) (Outputs)
taxAmount = itemPrice * 0.08
itemPrice totalCost <br> totalCost = itemPrice + Sales tax rate is 8%.
taxAmount
itemPrice must be
taxRate (0.08)
a positive number.
Export to Sheets

<br>

2. Developing an Algorithm

An algorithm is the core logic of your solution. It's a precise, step-by-step procedure. It's
language-independent, meaning the same algorithm can be implemented in C++, Python, or
Java.

Flowchart A flowchart uses visual symbols to represent the flow of control. It's particularly
useful for visualizing decisions and loops.
 Example: Finding the largest of two numbers.
1. Start (Oval)
2. Input num1, num2 (Parallelogram)
3. Decision: Is num1 > num2? (Diamond)
 If Yes: Print num1 (Parallelogram)
 If No: Print num2 (Parallelogram)
4. End (Oval)

Pseudocode Pseudocode is a text-based, high-level description of an algorithm. It's less visual


than a flowchart but more detailed than a simple plan.

 Example: Finding the largest of two numbers.


 BEGIN
 READ num1
 READ num2
 IF num1 > num2 THEN
 PRINT "The largest number is " num1
 ELSE
 PRINT "The largest number is " num2
 END IF
 END

<br>

3. Program Structure & Execution

Program Structure A C++ program's structure is like a building with different rooms.

C++
#include <iostream> // The "header" for input/output functions

int main() { // The main entry point


// This is the body of the function where your code goes
std::cout << "Hello, World!" << std::endl;
return 0; // Signals successful completion
}

Compilation & Execution This process is what transforms your human-readable code into
something the computer can run.

1. Source Code (.cpp) -> g++ [Link] -o myprogram -> Object Code (.o) ->
Executable (.exe)
2. The operating system then loads and runs the .exe file.

Interactive vs. Script Mode


 Interactive: In a language like Python, you can type print("Hello") and get an
immediate result.
 Script: In C++, you write a full program (a script), save it, compile it, and then run the
executable.

Comments and Indentation

 Comments are for humans, not the compiler. Use them to explain complex logic.

C++

// This loop calculates the sum of all elements in an array.


/*
This is a block comment
explaining a complex function.
*/

 Indentation makes your code readable. It groups related blocks of code.

C++

if (temperature > 30) {


// This line is inside the if statement
cout << "It's hot!";
}
// This line is outside the if statement

<br>

4. Data Types, Variables, and Operators

Primitive Data Types Think of data types as containers for different kinds of data. You
wouldn't store milk in a cardboard box, and you shouldn't try to store a decimal number in an
integer variable.

 int: For whole numbers (e.g., 10, -5). Occupies 4 bytes.


 float: For single-precision floating-point numbers (e.g., 3.14f). Less precise than
double.
 double: For double-precision floating-point numbers (e.g., 3.1415926). The default for
decimal values.
 char: For a single character (e.g., 'A', '7').
 bool: For a binary state: true or false.

Variables and Constants

 Variable: A named location in memory whose value can change.


C++

int age = 25; // Declares a variable 'age' and initializes it to 25


age = 26; // The value can be changed

 Constant: A named location whose value cannot change after initialization.

C++

const double PI = 3.14159;


// PI = 3.15; // This would cause a compilation error

Operators Operators are symbols that perform operations on variables and values.

 Arithmetic: +, -, *, /, %

C++

int result = 10 % 3; // result is 1 (the remainder of 10/3)

 Relational: ==, !=, >, <, >=, <=

C++

bool is_equal = (5 == 5); // is_equal is true

 Logical: && (AND), || (OR), ! (NOT)

C++

bool condition = (age > 18 && has_license); // Both must be true

 Assignment: =, +=, -=, *=, /=

C++

int num = 5;
num += 3; // Equivalent to num = num + 3; num is now 8

 Ternary (Conditional): A shorthand for if/else.

C++

int max = (num1 > num2) ? num1 : num2; // If num1 > num2, max = num1;
else, max = num2

<br>
5. Functions

Input/Output (I/O) Functions The iostream library provides standard I/O functions.

 std::cout: The Character OUTput stream.


 std::cin: The Character INput stream.

C++
#include <iostream>

int main() {
int user_input;
std::cout << "Enter a number: "; // Prompt the user
std::cin >> user_input; // Read from the keyboard
std::cout << "You entered: " << user_input << std::endl; // Display the
input
return 0;
}

Built-in Functions These are pre-written functions that save you from re-inventing the wheel.
You must include the right header file.

 sqrt() (from <cmath>): Calculates the square root.


 pow() (from <cmath>): Calculates a number raised to a power.

C++
#include <iostream>
#include <cmath> // Required for mathematical functions

int main() {
double base = 2.0;
double exponent = 3.0;
double result = pow(base, exponent); // result will be 8.0
std::cout << "2^3 is " << result << std::endl;

double number = 25.0;


double sq_root = sqrt(number); // sq_root will be 5.0
std::cout << "The square root of 25 is " << sq_root << std::endl;

return 0;
}

You might also like