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

lect04_CSC101

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

lect04_CSC101

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

Programming with C and C++

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

I Both paradigms focus on the transformation: Converting


inputs into desired outputs.
I By understanding one, we have a head start on
understanding the other.
I Just as we manipulate mathematical functions to solve
problems, we can manipulate C functions to program
solutions.

Mathematics is the gate, and programming is the key.


Another Analogy: Ingredients and Variables

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

Kitchen Recipe: C Programming:


I Desired Outcome: A tasty I Desired Output: Correct
cake results, no errors.
I Result of following steps I Result of executing
with ingredients. instructions with variables.
The Essence of the Analogy

I Both paradigms are about transformation: Ingredients to a


dish, Variables to an output.
I Just as you’d troubleshoot a recipe, you debug a program.
I The beauty of creation: Crafting a dish or crafting a
solution.

Programming, like cooking, is an art. Mastery comes with


practice and understanding.
A Simple Task: Finding the Area of a Rectangle
Task:
I Calculate the area of a rectangle with a length of 5 units
and a width of 7 units.
I Mathematically: Area = length ⇥ width
C Program:
# include < stdio .h >

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 >

I It’s a preprocessor directive.


I Tells the compiler to include the standard input-output
header file.
I Enables the use of ‘printf’ and other I/O functions.
I Think of it as importing a toolkit before starting work.
Dissecting the Program: The main() Function

Function:
int main () {

I It’s the entry point for every C program.


I The execution starts from here.
I Contains the key instructions to run.
I Returns an integer to the operating system upon
completion.
I This is similar to a mathematical function having both input
and output.
Dissecting the Program: Variables

Variable Declaration:
int length = 5;
int width = 7;

I Variables ‘length’ and ‘width’ store the dimensions of the


rectangle.
I ‘int’ denotes the integer data type.
I Variables are initialized with given values.
I They play a role similar to constants in algebra.
Dissecting the Program: Calculating Area

Calculation:
int area = length * width ;

I Multiplication operation calculates the area.


I The result is stored in the ‘area’ variable.
I Direct application of the mathematical formula.
Dissecting the Program: Printing the Result

Printf:
printf (" Area = % d \ n " , area ) ;

I Displays the result on the console.


I ‘%d’ is a placeholder for integers.
I It will be replaced by the value of ‘area’.
I It’s like evaluating a function with a given value.
Dissecting the Program: The Return Statement

Return Statement:
return 0;

I Signifies the successful termination of the program.


I Returns an integer value (0 in this case) to the operating
system.
I Like concluding a mathematical operation or proof.
Thank You and Keep Coding!

"Don’t be pushed around by the fears in your


mind. Be led by the dreams in your heart."
- Roy T. Bennett

You might also like