SlideShare a Scribd company logo
CSCI 101
First Program
Memory variables, input, disp, arithmetic
expressions, math functions
Overview
● Review: How to write a program?
● Substitution in mathematics & programming
● Building our first program
● Memory
● Getting input
● Displaying output
● Arithmetic expressions
● Sample programs
Substitution in mathematics & programming
● Software evolved first to serve solving
mathematical problems
● Therefore, it follows the main concepts of
evaluating expressions
● Substitution in mathematical expressions
works the same way in code
Substitution in mathematics & programming
𝑥 = 4
𝑦 = 𝑒 𝑥
● Solving the above mathematically by
substituting manually step by step:
𝑦 = 𝑒√4
𝑦 = 𝑒2
𝑦 = 2.7182
𝑦 = 7.38
Substitution in mathematics & programming
● In MATLAB, it is written as:
x = 4;
y = exp(sqrt(x));
● By substituting step by step:
1. First 4 is assigned to x
2. Then x is substituted for, y = exp(sqrt(4))
3. Function sqrt(4) is called which takes 4 as an input, gets
evaluated and returns back 2, now y = exp(2)
4. Function exp(2) is called which takes 2 as an input, gets
evaluated and returns back 7.38 which gets assigned to y
Exercise: x = 4; y = cos(exp(x/2-2)*pi)/sin(pi/2);
How to write a program?
1.Read the problem statement, and identify
–The input and its range
–The output
–The relationship between the input and the output (how
to compute the output) [Comprehend]
2.Write your thoughts as a sequence of steps. [Algorithm]
3.Convert these steps to Code. [Program]
4.Test your code and compare your program result against a
human result. [Testing]
Calculate area of a rectangle
(Problem  Algorithm)
How a computer can solve this problem?
I get the values of the height and the width from you and I store them in my
memory.
 Get height as h
Get width as w
Then I multiply them in my brain and store the result in my memory
 Calculate area = h*w
and inform you about the resultant value.
 display area
Calculate area of a rectangle
(Algorithm  Program)
1. Get height h
 h=input(‘enter height: ’);
2. Get width w
 w=input(‘enter width: ’);
3. Calculate area = h * w
 area = h * w;
4. Display area
 disp (area);
Memory
•Memory is divided into bytes
•Each byte has its own address
represented by binary numbers
•4 or 8 bytes form a memory
location to store integer or
floating (Ex 10.2345) numbers
•Software Programs uses letters
and decimal numbers to name
the memory location.
15
12
45
2
-3
4
x
y
sum
A
A1
numOfBus
Memory Location Name
(Variables)
•Variable: a named space for storing a value
radius
•Valid names start with a letter, can contain
digits.
•Use meaningful variable names.
•MATLAB is case sensitive (area and Area are different)
Xyz 3xyz x-y ab2cd a12345 myvar AbCdE1234?
Get input from user
h=input(‘enter height: ’);
Memory location
to store the input
Keyword to hold the
program execution to
get input from the keyboard
Message to the user
Write a Matlab statement to take the circle radius from the user and store it
in ‘R’.
>> enter height:
5h
?
5
Get an array from user
X=input(‘enter array elements: ’);
>> enter array elements: [4 5 2 6 8]
length(X): number of elements
To access the array elements use X(index)
Where index is an integer from 1 to length(X)
What is the value of: X(4)= X(6)=
4
5
2
6
8
X(1)
X(2)
X(3)
X(4)
X(5)
?
Display to Screen
disp(h);
Memory location
to display
Keyword to display
to the screen
>> 5
5
102
h
disp(a);
>> 102
a
Arithmetic Expressions
Operations
^ Exponentiation (5^2 is 25)
*, / Multiplication and division
+, - Addition and subtraction
Examples
x=4; y=3.5; z=2;
a= x +y – 5 / (3+z);
b=a + x / 2;
4
3.5
2
6.5
8.5
x
y
z
a
b
Operations are executed only one at a time according to their
precedence. We will study it further in future lectures
Calculate area of a rectangle
(Program and Testing)
h=input(‘enter height: ’);
w=input(‘enter width: ’);
area = h * w;
disp (area);
enter height: 5
enter width: 4
20
5
4
20
h
w
area
Calculate area of a circle
(Algorithm, Program, Testing)
1. Get radius r
2. Calculate area = pi*r^2
3. Display area
r=input(‘enter radius:’);
area =pi*r^2;
disp(area);
enter radius: 5
49.3480
5
49.3480
radius
area
Calculate number of buses to
transfer students (Algorithm)
1.Get number of students as numS
2.Get the bus capacity as busC
3.Calculate num of buses as
numB = numS/busC (round up)
4. Display numB
Calculate number of buses to
transfer students (Program)
numS=input(‘enter number of students:’);
busC=input(‘enter bus capacity:’);
numB = numS/busC;
disp(numB);
ceil(1.2) = 2 ceil round up to nearest integer
 numB = ceil(numS/busC);
If numS=60 and busC = 50 then numB = 60/50 = 1.2 ???
But it should be 2
Calculate number of dozens and
remainder (Algorithm)
1.Get a number as N
2.Calculate dozen = N/12 (no fraction)
3.Calculate the reminder (r) of dividing N by 12
4.Display dozen
5.Display r
Calculate number of dozens and
remainder (Program)
N=input(‘enter a number’);
dozen = N/12;
r = ??
disp(dozen);
disp(r);
fix(5.8) = 5 fix removes the fraction
rem(10,3) = 1 rem calculates the reminder of dividing 10 by 3
rem(100,2) = 0
 dozen = fix(N/12);
 r= rem(N,12);

More Related Content

PPT
DOCX
Computer graphics
PPSX
Software Metrics
PPT
Implementation
PDF
Computer graphics practical(jainam)
PDF
PDF
Assignment on Numerical Method C Code
PDF
16890 unit 2 heuristic search techniques
Computer graphics
Software Metrics
Implementation
Computer graphics practical(jainam)
Assignment on Numerical Method C Code
16890 unit 2 heuristic search techniques

What's hot (20)

PPTX
Dag representation of basic blocks
PPTX
2D Plot Matlab
PPTX
Lecture 21 problem reduction search ao star search
PPT
Directed Acyclic Graph
PPT
Iterative deepening search
PDF
C- Programming Assignment 3
PPTX
Assignment statements
PDF
Digital logic design1
DOCX
Array
DOCX
GSP 215 Enhance teaching/tutorialrank.com
DOCX
GSP 215 Inspiring Innovation/tutorialrank.com
PDF
Programming with matlab session 1
DOC
COMPUTER GRAPHICS LAB MANUAL
PDF
Computer graphics lab manual
PDF
PECCS 2014
PDF
C - Programming Assignment 1 and 2
PDF
C- Programming Assignment 4 solution
PDF
maXbox starter68 machine learning VI
PDF
Specialized indexing for NoSQL Databases like Accumulo and HBase
Dag representation of basic blocks
2D Plot Matlab
Lecture 21 problem reduction search ao star search
Directed Acyclic Graph
Iterative deepening search
C- Programming Assignment 3
Assignment statements
Digital logic design1
Array
GSP 215 Enhance teaching/tutorialrank.com
GSP 215 Inspiring Innovation/tutorialrank.com
Programming with matlab session 1
COMPUTER GRAPHICS LAB MANUAL
Computer graphics lab manual
PECCS 2014
C - Programming Assignment 1 and 2
C- Programming Assignment 4 solution
maXbox starter68 machine learning VI
Specialized indexing for NoSQL Databases like Accumulo and HBase
Ad

Similar to Csci101 lect01 first_program (20)

PDF
introduction to python programming course 2
PDF
BCS401 ADA First IA Test Question Bank.pdf
PPTX
CSE115 C Programming Introduction North south university
PPTX
L1_DatabAlgorithm Basics with Design & Analysis.pptx
DOCX
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
PPTX
L1_Start_of_Learning_of_Algorithms_Basics.pptx
PPT
c-programming
PPTX
Pythonlearn-02-Expressions123AdvanceLevel.pptx
PDF
Introduction to programming - class 3
PDF
[FLOLAC'14][scm] Functional Programming Using Haskell
PDF
R programmingmilano
PDF
MATLAB for Technical Computing
PDF
CSC1100 - Chapter12 - Flow Charts
PPT
DAA-Introduction-Divide and Conquer Technique
PPTX
Support Vector Machines Simply
PDF
Problem solving using computers - Unit 1 - Study material
PPT
C Tutorials
PPTX
Mathematical Modeling With Maple
PPTX
Lecture 2.pptxLecture 2.pptxLecture 2.pptxLecture 2.pptx
PDF
Lecture1_computer vision-2023.pdf
introduction to python programming course 2
BCS401 ADA First IA Test Question Bank.pdf
CSE115 C Programming Introduction North south university
L1_DatabAlgorithm Basics with Design & Analysis.pptx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
L1_Start_of_Learning_of_Algorithms_Basics.pptx
c-programming
Pythonlearn-02-Expressions123AdvanceLevel.pptx
Introduction to programming - class 3
[FLOLAC'14][scm] Functional Programming Using Haskell
R programmingmilano
MATLAB for Technical Computing
CSC1100 - Chapter12 - Flow Charts
DAA-Introduction-Divide and Conquer Technique
Support Vector Machines Simply
Problem solving using computers - Unit 1 - Study material
C Tutorials
Mathematical Modeling With Maple
Lecture 2.pptxLecture 2.pptxLecture 2.pptxLecture 2.pptx
Lecture1_computer vision-2023.pdf
Ad

More from Elsayed Hemayed (20)

PPTX
14 cie552 camera_calibration
PPTX
12 cie552 object_recognition
PPTX
11 cie552 image_featuresii_sift
PPTX
10 cie552 image_featuresii_corner
PPTX
09 cie552 image_featuresi
PPTX
08 cie552 image_segmentation
PPTX
07 cie552 image_mosaicing
PPTX
06 cie552 image_manipulation
PPTX
05 cie552 image_enhancement
PPTX
04 cie552 image_filtering_frequency
PPTX
03 cie552 image_filtering_spatial
PPTX
02 cie552 image_andcamera
PPTX
01 cie552 introduction
PPTX
Csci101 lect04 advanced_selection
PPTX
Csci101 lect10 algorithms_iii
PPTX
Csci101 lect09 vectorized_code
PPTX
Csci101 lect08b matlab_programs
PPTX
Csci101 lect08a matlab_programs
PPTX
Csci101 lect07 algorithms_ii
PPTX
Csci101 lect06 advanced_looping
14 cie552 camera_calibration
12 cie552 object_recognition
11 cie552 image_featuresii_sift
10 cie552 image_featuresii_corner
09 cie552 image_featuresi
08 cie552 image_segmentation
07 cie552 image_mosaicing
06 cie552 image_manipulation
05 cie552 image_enhancement
04 cie552 image_filtering_frequency
03 cie552 image_filtering_spatial
02 cie552 image_andcamera
01 cie552 introduction
Csci101 lect04 advanced_selection
Csci101 lect10 algorithms_iii
Csci101 lect09 vectorized_code
Csci101 lect08b matlab_programs
Csci101 lect08a matlab_programs
Csci101 lect07 algorithms_ii
Csci101 lect06 advanced_looping

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Classroom Observation Tools for Teachers
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
RMMM.pdf make it easy to upload and study
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Cell Types and Its function , kingdom of life
PDF
01-Introduction-to-Information-Management.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Lesson notes of climatology university.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Final Presentation General Medicine 03-08-2024.pptx
Classroom Observation Tools for Teachers
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Module 4: Burden of Disease Tutorial Slides S2 2025
Renaissance Architecture: A Journey from Faith to Humanism
RMMM.pdf make it easy to upload and study
O5-L3 Freight Transport Ops (International) V1.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Pharma ospi slides which help in ospi learning
PPH.pptx obstetrics and gynecology in nursing
VCE English Exam - Section C Student Revision Booklet
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Types and Its function , kingdom of life
01-Introduction-to-Information-Management.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Complications of Minimal Access Surgery at WLH
Lesson notes of climatology university.
Microbial diseases, their pathogenesis and prophylaxis
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Csci101 lect01 first_program

  • 1. CSCI 101 First Program Memory variables, input, disp, arithmetic expressions, math functions
  • 2. Overview ● Review: How to write a program? ● Substitution in mathematics & programming ● Building our first program ● Memory ● Getting input ● Displaying output ● Arithmetic expressions ● Sample programs
  • 3. Substitution in mathematics & programming ● Software evolved first to serve solving mathematical problems ● Therefore, it follows the main concepts of evaluating expressions ● Substitution in mathematical expressions works the same way in code
  • 4. Substitution in mathematics & programming 𝑥 = 4 𝑦 = 𝑒 𝑥 ● Solving the above mathematically by substituting manually step by step: 𝑦 = 𝑒√4 𝑦 = 𝑒2 𝑦 = 2.7182 𝑦 = 7.38
  • 5. Substitution in mathematics & programming ● In MATLAB, it is written as: x = 4; y = exp(sqrt(x)); ● By substituting step by step: 1. First 4 is assigned to x 2. Then x is substituted for, y = exp(sqrt(4)) 3. Function sqrt(4) is called which takes 4 as an input, gets evaluated and returns back 2, now y = exp(2) 4. Function exp(2) is called which takes 2 as an input, gets evaluated and returns back 7.38 which gets assigned to y Exercise: x = 4; y = cos(exp(x/2-2)*pi)/sin(pi/2);
  • 6. How to write a program? 1.Read the problem statement, and identify –The input and its range –The output –The relationship between the input and the output (how to compute the output) [Comprehend] 2.Write your thoughts as a sequence of steps. [Algorithm] 3.Convert these steps to Code. [Program] 4.Test your code and compare your program result against a human result. [Testing]
  • 7. Calculate area of a rectangle (Problem  Algorithm) How a computer can solve this problem? I get the values of the height and the width from you and I store them in my memory.  Get height as h Get width as w Then I multiply them in my brain and store the result in my memory  Calculate area = h*w and inform you about the resultant value.  display area
  • 8. Calculate area of a rectangle (Algorithm  Program) 1. Get height h  h=input(‘enter height: ’); 2. Get width w  w=input(‘enter width: ’); 3. Calculate area = h * w  area = h * w; 4. Display area  disp (area);
  • 9. Memory •Memory is divided into bytes •Each byte has its own address represented by binary numbers •4 or 8 bytes form a memory location to store integer or floating (Ex 10.2345) numbers •Software Programs uses letters and decimal numbers to name the memory location. 15 12 45 2 -3 4 x y sum A A1 numOfBus
  • 10. Memory Location Name (Variables) •Variable: a named space for storing a value radius •Valid names start with a letter, can contain digits. •Use meaningful variable names. •MATLAB is case sensitive (area and Area are different) Xyz 3xyz x-y ab2cd a12345 myvar AbCdE1234?
  • 11. Get input from user h=input(‘enter height: ’); Memory location to store the input Keyword to hold the program execution to get input from the keyboard Message to the user Write a Matlab statement to take the circle radius from the user and store it in ‘R’. >> enter height: 5h ? 5
  • 12. Get an array from user X=input(‘enter array elements: ’); >> enter array elements: [4 5 2 6 8] length(X): number of elements To access the array elements use X(index) Where index is an integer from 1 to length(X) What is the value of: X(4)= X(6)= 4 5 2 6 8 X(1) X(2) X(3) X(4) X(5) ?
  • 13. Display to Screen disp(h); Memory location to display Keyword to display to the screen >> 5 5 102 h disp(a); >> 102 a
  • 14. Arithmetic Expressions Operations ^ Exponentiation (5^2 is 25) *, / Multiplication and division +, - Addition and subtraction Examples x=4; y=3.5; z=2; a= x +y – 5 / (3+z); b=a + x / 2; 4 3.5 2 6.5 8.5 x y z a b Operations are executed only one at a time according to their precedence. We will study it further in future lectures
  • 15. Calculate area of a rectangle (Program and Testing) h=input(‘enter height: ’); w=input(‘enter width: ’); area = h * w; disp (area); enter height: 5 enter width: 4 20 5 4 20 h w area
  • 16. Calculate area of a circle (Algorithm, Program, Testing) 1. Get radius r 2. Calculate area = pi*r^2 3. Display area r=input(‘enter radius:’); area =pi*r^2; disp(area); enter radius: 5 49.3480 5 49.3480 radius area
  • 17. Calculate number of buses to transfer students (Algorithm) 1.Get number of students as numS 2.Get the bus capacity as busC 3.Calculate num of buses as numB = numS/busC (round up) 4. Display numB
  • 18. Calculate number of buses to transfer students (Program) numS=input(‘enter number of students:’); busC=input(‘enter bus capacity:’); numB = numS/busC; disp(numB); ceil(1.2) = 2 ceil round up to nearest integer  numB = ceil(numS/busC); If numS=60 and busC = 50 then numB = 60/50 = 1.2 ??? But it should be 2
  • 19. Calculate number of dozens and remainder (Algorithm) 1.Get a number as N 2.Calculate dozen = N/12 (no fraction) 3.Calculate the reminder (r) of dividing N by 12 4.Display dozen 5.Display r
  • 20. Calculate number of dozens and remainder (Program) N=input(‘enter a number’); dozen = N/12; r = ?? disp(dozen); disp(r); fix(5.8) = 5 fix removes the fraction rem(10,3) = 1 rem calculates the reminder of dividing 10 by 3 rem(100,2) = 0  dozen = fix(N/12);  r= rem(N,12);