[go: up one dir, main page]

0% found this document useful (0 votes)
15 views71 pages

01intro To Programming

The document outlines the fundamental concepts of computers, including their problem-solving capabilities, hardware components, and the role of operating systems and programming languages. It emphasizes the importance of algorithms in programming, detailing their characteristics and how to design them using pseudocode and flowcharts. Additionally, it covers the significance of state in programming and the process of translating algorithms into code.

Uploaded by

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

01intro To Programming

The document outlines the fundamental concepts of computers, including their problem-solving capabilities, hardware components, and the role of operating systems and programming languages. It emphasizes the importance of algorithms in programming, detailing their characteristics and how to design them using pseudocode and flowcharts. Additionally, it covers the significance of state in programming and the process of translating algorithms into code.

Uploaded by

yawar ayub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 71

Why Computers?

The Problem
Solvers
• Automation of Repetitive Tasks: Freeing up human
time for more creative work.
• Storage & Retrieval of Information: Vast amounts of
data at our fingertips.
• Information Processing & Computation: Performing
complex calculations rapidly.
• Communication & Connectivity: Bridging distances,
enabling global interaction.
• Problem Solving: Solving complex problems (e.g.,
weather prediction, drug discovery).
Conversing with Machines
• Humans: Speak natural languages (English, Hindi,
etc.).
• Computers: Speak machine language (binary - 0s and
1s).
• The Bridge: How do we tell a computer what to do?
(Brief introduction to instructions/commands).
• Examples of Interaction:
• Clicking a mouse
• Typing on a keyboard
• Touching a screen
• Speaking to a virtual assistant
What's Inside a Computer?
• Input Devices: How we feed information (Keyboard,
Mouse, Microphone, Camera).
• Output Devices: How computers show us information
(Monitor, Printer, Speakers).
• Central Processing Unit (CPU): The "brain" - where
all the thinking happens.
• Memory (RAM): Short-term storage for active tasks.
• Storage (Hard Drive/SSD): Long-term storage for
programs and data.
Input Devices: Our Senses for the
Computer
• Keyboard: Text and commands.
• Mouse/Touchpad: Navigation and selection.
• Microphone: Voice input.
• Camera/Webcam: Image and video input.
Output Devices: How Computers
Talk Back
• Monitor/Display: Visual feedback.
• Printer: Hard copies of information.
• Speakers/Headphones: Audio feedback.
• Projector: Large-scale display.
Computer Memory: The Brain's
Scratchpad
• What is RAM? (Random Access Memory) - Volatile
memory.
• Purpose: Stores data and programs currently in use by
the CPU.
• Characteristics:
• Very Fast
• Temporary (data lost on power off)
• Enables multitasking and quick access.
Secondary Storage: The Permanent
Library
• What is it? Non-volatile storage (Hard Disk Drives -
HDDs, Solid State Drives - SSDs, USB drives, SD cards).
• Purpose: Stores operating system, applications, and all
your files permanently.
• Characteristics:
• Slower than RAM
• Permanent (data retained on power off)
• Larger capacity
The CPU: The Brain of the
Computer
• What it does: Executes instructions, performs calculations,
manages other components.
• Key Components:
• Arithmetic Logic Unit (ALU): For math and logical operations.
• Control Unit (CU): Directs and coordinates operations.
• Registers: Small, fast storage areas within the CPU.
• How it works (Simplified): Fetch -> Decode -> Execute ->
Writeback cycle.
• Factors affecting performance: Clock speed, number of
cores, cache size.
Note: 1 GHz = 1 billion cycles per second.
What is an Operating System
(OS)?
• The most important software on a computer.
• It is a Manager and a Translator.
• Manages Resources: CPU time, Memory, Storage, I/O devices.
Makes sure everything plays nicely.
• Provides an Interface: A way for users and applications to talk
to the hardware without needing to know the messy details.
• Windows: Most common on personal computers.
• macOS: On Apple computers.
• Linux: Powers most of the internet, servers, and Android is
based on it.
Why Do We Need Programming
Languages?
• We saw that computers only understand Binary (1s
and 0s). This is called Machine Code.
• It's impossible for humans to write complex programs in
1s and 0s.
• We need a translator between human thought and
machine code.
Low-Level vs. High-Level
Languages
• Low-Level (e.g., Assembly): Close to machine code.
Human-readable mnemonics. Very fast and powerful but
hard to learn and hardware-specific.
• High-Level (e.g., C, Python, Java): Close to human
language (English). Uses words like if, else, print etc.
Much easier to read, write, and maintain. Not directly
understood by the CPU.
The Translator: Compilers and
Interpreters
• Compiler (e.g., C, C++): Takes the entire program and
translates it into an executable file (machine
code) before you run it.
• Interpreter (e.g., Python, JavaScript): Translates and
executes the program line-by-line as it runs.
The Landscape of Programming
Languages
• Many languages exist, each designed for different tasks.
• Python: Data science, AI.
• Java: Used for large enterprise applications, Android
apps.
• JavaScript: The language of the web browser.
• C/C++: Powerful, used for system programming, games,
operating systems, where performance is critical.
Why Are We Learning C?
• Foundation: It's the mother of many languages (C++,
Java, C#). Learning C makes learning others easier.
• Performance: It's very fast and gives you fine-grained
control over the hardware and memory.
• "Closer to the Metal": It allows you to understand the
connection between the hardware and software we just
discussed. You will see how memory works, how the
CPU processes data.
• Everywhere: Operating Systems, Embedded Systems,
Device Drivers are often written in C.
Tools of the Trade
• A Text Editor / IDE: Where we write our code. (e.g., VS
Code, Code::Blocks, or a simple online compiler for day
one)
• A C Compiler: The software that translates our C code
to machine code. (e.g., GCC - GNU Compiler Collection)
Summary & Key Takeaways
• Computers are problem-solving machines.
• Hardware (CPU, Memory, Storage) works together.
• The OS manages the hardware.
• We need programming languages to give instructions.
• Compilers translate high-level code to machine code.
• C is a powerful, foundational language.
UNIT I
• ALGORITHMIC PROBLEM SOLVING :
• Algorithms, building blocks of algorithms (statements,
state, control flow, functions), notation (pseudo code,
flow chart, programming language), strategies for
developing algorithms. Introduction to scratch & raptor.
From Problem to Solution: Algorithms &
Their Building Blocks
• Learning the Language of Logic
What We Already Learnt ?
• Computers need precise instructions called programs.
• We write these in high-level languages (like C) which
are compiled into machine code.
The Bridge Between Problem
and Program
• You have a problem (e.g., "Make me a cup of tea").
• You can't just start typing C code randomly.
• You need a plan. This plan is called an Algorithm.
• Before coding, we must design the algorithm.
What is an Algorithm?
• Definition: A step-by-step procedure to solve a
problem or perform a task.
• It's a recipe: A recipe for baking a cake is an
algorithm.
• It's directions: The steps to get from college to your
home form an algorithm.
Characteristics of Algorithm
• Unambiguous: Each step is clear and precise. (Not
"add some sugar," but "add one teaspoon of sugar").
• Well-Defined Inputs: What do you need to start? (e.g.,
tea leaves, water, cup).
• Well-Defined Outputs: What do you get at the end?
(e.g., a cup of tea).
• Finiteness: It must eventually stop. It can't run forever.
Algorithm in Action: Making a
Cup of Tea
• The Problem Statement:

• Input: Water, kettle, tea bag, cup, milk, sugar.

• Desired Output: A prepared cup of tea.


The Algorithm (First Draft –
Flawed)
• Boil water.
• Put tea bag in cup.
• Pour water into cup.
• Add milk and sugar.
• Serve.
Testing the Algorithm: The
"Unambiguous" Rule
• "Boil water": How much water?
• "Add milk and sugar": How much? One spoon? Ten
spoons? What if the user doesn't want any?
• This algorithm is ambiguous. It relies on the user's
prior knowledge. A computer has no prior knowledge!
The Refined Algorithm
(Unambiguous & Precise):
• Step 1: Ensure the kettle is plugged in and switched on.
Step 2: Pour 200 ml of water into the kettle.
Step 3: Turn on the kettle and boil the water.
Step 4: Place one tea bag into an empty cup.
Step 5: Once the water has boiled, pour 150 ml of the
boiling water into the cup.
Step 6: Let the tea bag steep for 2 minutes.
Step 7: Remove the tea bag from the cup and discard it.
Contd…
• Step 8: Ask the user: "Do you take milk?"
* If the answer is YES, ask "How much?" and add that
many milliliters.
* If the answer is NO, do nothing.
Step 9: Ask the user: "Do you take sugar?"
* If the answer is YES, ask "How many spoons?" and add
that many spoons. Stir.
* If the answer is NO, do nothing.
Step 10: The cup of tea is ready. Serve.
How do we write down our plan?
• We can represent algorithms in different ways:
• Pseudocode: English-like statements that describe the
steps. (Easy for humans to read).
• Flowcharts: A diagram with boxes and arrows to show
the flow of steps. (Easy to visualize logic).
Example:
The Building Blocks of All
Algorithms
• All algorithms are built from three basic building blocks:
• Sequence
• Selection (Decision)
• Iteration (Loop)
Sequence
• A series of steps performed in a specific, linear order.
• One step follows another, from top to bottom.
• This is the default flow of any program.
• Example:
• Step A: Get two numbers, A and B.
• Step B: Add them to find Sum = A + B.
• Step C: Print the value of Sum.
FlowChart to sum two Numbers
SELECTION (The Decision)
• The ability to make a choice or ask a question.
• The program chooses one path out of two or more
based on whether a condition is True or False.
• Also known as branching or if-then-else.
Even-Odd Pseudocode
• Example:
• START
• READ number
• IF (number MOD 2 == 0)
• PRINT "The number is even."
• ELSE
• PRINT "The number is odd."
• END IF
• END
Even-Odd FlowChart
ITERATION (The Loop)
• The ability to repeat a set of steps multiple times.
• Used when you need to do the same thing over and
over without writing the same instructions repeatedly.
• Also known as looping or repetition.
Example
• WHILE (the stack of homework problems is NOT empty)
• DO THIS:
• Take the top problem from the stack.
• Solve the problem.
• Check the answer.
• Place the solved problem in the 'done' pile.
• END WHILE
Iteration Example
From Building Blocks to
Readable Code
• Organizing Your Instructions
• What if your algorithm has a repeated set of steps, or a
specific sub-task?
• Function: A named block of code that performs one
specific, well-defined task.
• It makes algorithms easier to read and manage.
Bringing It All Together: A
Complete Example
• Algorithm: Find the Largest of Three Numbers
• Start.
• Input: three numbers: A, B, C. (SEQUENCE)
• Assume Largest is A. (SEQUENCE)
• If B is greater than Largest, then set Largest =
B. (SELECTION)
• If C is greater than Largest, then set Largest =
C. (SELECTION)
• Print Largest. (SEQUENCE)
• Stop.
What is "State"? The Computer's
Memory
• State is the current snapshot of a program's data. It's
the collective value of all its variables at any given
moment.
• It represents everything the program "knows" or
"remembers" right now.
• When the state changes, the program's behavior might
change in the future.
Example
• Your account's state is your current balance (e.g., $100).
• If you deposit $50, the state changes to a new balance
($150).
• If you try to withdraw $200, the outcome (approval or
denial) depends entirely on the current state ($150).
Why is Understanding "State"
So Important?
• Behavior Depends on State: The same line of code can do different things based
on the current state.
• withdraw(amount); might work if balance >= amount (state allows it) or fail if balance <
amount (state prevents it).
• Debugging: Finding errors (bugs) often means figuring out how the program got into
a bad state.
• "Why is total_value negative?!" You have to trace back through the steps to see which action
corrupted the state.
• It's What Changes: Algorithms are boring if they don't change state. The whole
point of a program is to take an input, change its internal state through calculations,
and produce an output.
Describing Algorithms: From
Idea to Code
• We have a problem... and we know about algorithms... now
what?

• You have a great idea for a program.


• You understand the building blocks (sequence, selection,
iteration).
• But... you can't just start typing C code. It's like building a
house without a blueprint!
• We need a way to write down our algorithm in a structured,
clear way that isn't a specific programming language.
Design of Algorithm
• We have two powerful tools to design our algorithms:
• Pseudocode: A text-based, human-friendly outline.
• Flowchart: A visual diagram of the steps and logic.

• Goal of Both: To focus on logic and process without


getting bogged down by the strict rules of a
programming language (like semicolons, brackets, etc.).
What is Pseudocode?
• Pseudo (false) + Code = "False Code" or "Mock Code"
• It's a high-level description of an algorithm that uses a
mixture of:
• Natural Language (e.g., "Calculate the sum", "Get user
input")
• Common Programming
Concepts (e.g., if, then, else, while, for)
• There are no strict rules! Its purpose is to be read by
humans, not computers.
Check whether Number is Big or Not
?
• BEGIN
• GET user_number
• IF user_number is greater than 100 THEN
• PRINT "That's a big number!"
• ELSE
• PRINT "That's a small number."
• END IF
• END
Why Use Pseudocode?
• Fast and Easy: Quick to write and change. No syntax
errors!
• Focus on Logic: Lets you think about
the solution instead of the syntax.
• Great for Collaboration: Easy to discuss with others
who might not know C.
• The Perfect First Step: It's the easiest way to start
turning an idea into a structured plan.
Example: Adding Two Numbers
• BEGIN
• // Get the first number from the user
• INPUT number1
• // Get the second number from the user
• INPUT number2
• // Calculate the result
• SET sum = number1 + number2
• // Show the result to the user
• OUTPUT sum
• END
What is a Flowchart?
• A visual representation of an algorithm's steps and the
flow of logic between them.
• It uses standardized shapes to represent different types
of actions.
• Perfect for visualizing the three building
blocks, especially selection and iteration.
The Flowchart Dictionary: Basic
Symbols
• Oval (Terminator): Start / End of the program.
• Parallelogram: Input / Output (e.g., getting data,
printing a result).
• Rectangle (Process): A calculation or action (e.g., sum =
a + b).
• Diamond (Decision): A question with a Yes/No
(True/False) answer. This is where selection happens.
• Arrows (Flowlines): Show the direction of the process.
Flowchart in Action: Checking if
a Number is Positive
From Design to C Code
• The recommended steps to solve any programming
problem:
• Understand the Problem.
• Design the Algorithm (using Pseudocode and/or Flowchart).
• Translate the Design into a Programming Language (e.g., C).
• Test and Debug the code.
C Program to Check if number is
positive

#include <stdio.h>
int main() {
int number;
scanf("%d", &number);
if (number >= 0) {
printf("Positive");
} else {
printf("Negative");
}
return 0;
}
Which One Should I Use?
• Use Pseudocode: When you need to be quick, focus
on the structure, or are working with simple logic.
• Use a Flowchart: When the logic is complex, involves
many decisions, or you need to visualize the flow
clearly.
• Use Both: Often, they complement each other
perfectly! You can write pseudocode and then draw a
flowchart for a tricky part.
Strategies for Problem-Solving
• Computers are Dumb: They do exactly what you tell
them, nothing more. Assume they know nothing.
• Be Painfully Precise: Ambiguity is the enemy. Every
step must be clear and unambiguous.
• Start Simple: Don't try to solve the entire complex
problem at once.
The Step-by-Step Development
Process
• Understand the Problem: (What are the inputs &
outputs?)
• Devise a Plan: (Choose a strategy - today's topic!)
• Carry Out the Plan: (Write the pseudocode/flowchart)
• Review and Test: (Look back - does it work for all
cases?)
Step 1: Understand the Problem
• Title: Step 1: Understand the Problem
• Content: Before you solve it, you must define it. Always
ask:
• What are the INPUTS? What data does the algorithm need
to start? (e.g., two numbers, a list of names, a temperature)
• What is the desired OUTPUT? What should the algorithm
produce? (e.g., a sum, a sorted list, "Hot" or "Cold")
• What are the rules? What are the constraints and
requirements? (e.g., "cannot use the / operator")
Strategy 2: Sequential Thinking
(The Straight Line)
• Some problems are just a sequence of steps. The
strategy is to list them in the correct order.
• When to use: For calculations, simple transformations,
and instructions.
Strategy 3: Decomposition
(Break It Down!)
• Decomposition: Breaking a large, complex problem
into smaller, more manageable sub-problems.
• Solve each sub-problem individually. Then, combine the
solutions.
• When to use: For any moderately complex task. This is
the most important strategy!
Strategy 4: Pattern Recognition
(Don't Reinvent the Wheel)
• Pattern Recognition: Looking for similarities or
common structures within a problem or between
problems.
• If you can spot a pattern, you can often use a loop or a
repeated set of instructions.
Learning to Think in Code: An
Introduction to Scratch & RAPTOR
• Visual Programming: Building Logic with Blocks and
Flowcharts
• Introducing Scratch & RAPTOR
Slide 2: Why Are We Doing This
Before C?
• We've learned algorithms, building blocks, and design
tools.
• Now, we need to practice implementing logic without
the frustration of C syntax (semicolons, brackets,
compiler errors).
• These tools let us focus purely on the flow of
control and problem-solving.
What is Scratch?
• A visual programming language developed at MIT.
• You code by snapping together graphical blocks (like
Lego bricks).
• No typing syntax = No syntax errors!
• It's designed to be fun, creative, and intuitive.
Core Scratch Concepts
• Stage: The background where your project runs.
• Sprites: The characters or objects that perform actions.
• Blocks: The commands you snap together to create
scripts.
• Scripts: The stacks of blocks that tell sprites what to
do.
Introduction to RAPTOR
• RAPTOR = Rapid Algorithmic Prototyping Tool
for Ordered Reasoning.
• It allows you to create executable flowcharts.
• You draw the flowchart using the standard symbols
(ovals, rectangles, diamonds), and RAPTOR can run it!
Thank You

You might also like