lect04_CSC101
lect04_CSC101
CSC-101 (Lecture 4)
Dr. R. Balasubramanian
Professor
Department of Computer Science and Engineering
Mehta Family School of Data Science and Artificial Intelligence
Indian Institute of Technology Roorkee
Roorkee 247 667
[email protected]
https://faculty.iitr.ac.in/cs/bala/
Motivation
From Numbers to Code
Trivia:
I Think of your favourite number. Can you represent it in
binary form?
I How would you store the number ⇡ in a computer, given it’s
an irrational number with infinite decimal expansion?
Mathematical Patterns in Programming
I Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, ...
I Prime Numbers: 2, 3, 5, 7, 11, 13, ...
Question: If you were to instruct a computer to generate these
sequences, where would you start?
Puzzles and Logic
Puzzle:
I Consider the equation: 2x + y = 100. Given that x, y 2 N,
how many solutions can you think of?
Question:
I How might a computer systematically find all these
solutions?
I Computers can’t directly understand algebra, but they can
execute commands based on logical and mathematical
operations.
I How do you think a computer might solve the equation
3x 4 = 11, where x 2 Z?
Geometry in Programming
Problem:
I Consider a circle with radius r . Can you write a
mathematical expression for its area and circumference?
Question:
I How would you instruct a computer to calculate these
properties for a given radius? Think of the functions and
constants involved.
Basics of C
Defining a Function
C Programming:
Mathematics:
I Encapsulates a set of
I Defined relation between
instructions.
sets.
I Ex.: int square(int x){
I Ex.: f (x) = x 2
return x * x;}
I Input: x
I Input: x
I Operation: Squaring
I Operation: Multiplication
I Output: x 2
I Output: x * x
Function Characteristics
Mathematics:
C Programming:
I Deterministic: One input
I Deterministic: Functions
has one specific output.
will produce the same
I Predictable: You can result with the same input.
always determine the
I Predictable: If coded
outcome with the given
correctly, no surprises.
input.
I Encapsulates a behaviour
I Represents a rule or a
or task.
transformation.
The Power of this Analogy
Kitchen Recipe:
I Ingredients: Flour, Eggs, C Programming:
Sugar I Variables: ‘int’, ‘char’, ‘float’
I Quantities: 1 cup, 2 eggs, I Values: ‘5’, “a”, ‘3.14’
0.5 cup I Essential to store and
I Essential to achieve the manipulate data.
final dish.
Steps and Instructions
C Programming:
Kitchen Recipe:
I Sequential instructions:
I Sequential steps: Mix,
Initialize, Compute, Print
Bake, Serve
I Each instruction performs
I Each step affects the
a task.
outcome.
I Execution order is
I Order is crucial.
paramount.
Outcomes and Outputs
int main () {
int length = 5;
int width = 7;
int area = length * width ;
printf (" Area = % d \ n " , area ) ;
return 0;
}
Dissecting the Program: Preprocessor Directive
What is this?
# include < stdio .h >
Function:
int main () {
Variable Declaration:
int length = 5;
int width = 7;
Calculation:
int area = length * width ;
Printf:
printf (" Area = % d \ n " , area ) ;
Return Statement:
return 0;