0% found this document useful (0 votes)
13 views88 pages

Week1 - Lec1 3 1

The document outlines a course on Computer Programming (CSE 109) taught by Lec Tasfia Sara, detailing the syllabus, assessment strategy, and reference materials. It introduces programming concepts, the importance of programming languages, and specifically focuses on C and C++ as foundational languages. The document also covers programming structures, data types, variables, and the use of format specifiers in C programming.

Uploaded by

Mehrin Islam
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)
13 views88 pages

Week1 - Lec1 3 1

The document outlines a course on Computer Programming (CSE 109) taught by Lec Tasfia Sara, detailing the syllabus, assessment strategy, and reference materials. It introduces programming concepts, the importance of programming languages, and specifically focuses on C and C++ as foundational languages. The document also covers programming structures, data types, variables, and the use of format specifiers in C programming.

Uploaded by

Mehrin Islam
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/ 88

CSE 109

Computer Programming

Lec Tasfia Sara


tasfi[email protected]
Lec Tasfia Sara
Part A Contents

Lec Tasfia Sara


Part B Contents

Lec Tasfia Sara


Reference Books
1. Teach Yourself C (3rd Edition) by Herbert Schidlt

2. Programming in Ansi C (6th Edition) by E Balagurusamy

3. C: The Complete Reference (4th Edition) by Herbert Schildt

4. C++: The Complete Reference (4th Edition) by Herbert Schildt

5. C Programming Language (2nd Edition) by Dennis M. Ritche

Lec Tasfia Sara


Google Classroom Code:

2f3wts7

Lec Tasfia Sara


Assessment Strategy
Components Grading

Class Test 20%

Continuous Assessment Class Participation and Attendance 10%


(40%)
Mid Term 10%

Final Examination 60%

Total Marks 100%

Lec Tasfia Sara


Introduction

Lec Tasfia Sara


Introduce yourself
Redundantly, Inefficiently, Manually

Lec Tasfia Sara


Introduction, but better!
Steps:
1. Start by the leftmost column’s first bench student standing up.
2. Say the following: Name, School & College, Home District, Reason of
choosing EECE as your major. Then sit down.
3. Then, the student behind stands up and does step 2, then repeats this step
(step 3) if someone is behind. If no one is behind do step 4.
4. If there is a column to the right, the student on the right stands up and does
step 2, then do step 5. If there is no column on the right, do step 6.
5. Then the student in front stands up and does step 2, then repeats this step
(step 5) if someone is in front. If no one is in front, do step 4.
6. End
10

Lec Tasfia Sara


What is
programming?

Lec Tasfia Sara


Lec Tasfia Sara
What is programming?

A set of instructions followed sequentially to automate a

task that is redundant, repetitive, tedious, and has patterns

to complete it efficiently.
(this is not an official definition and you do not have to follow it word for word; just understand
the gist of it.)

Lec Tasfia Sara


What is Computer Programming?
● Series of instructions to a computer to accomplish a task
● Instructions must be written in a way the computer can understand
● Programming languages are used to write programs

14

Lec Tasfia Sara


Why programming?

Lec Tasfia Sara


Applications in Electrical Engineering

Robotics Arduino
16

Lec Tasfia Sara


Applications in Electrical Engineering

Power Systems Raspberry Pi


17

Lec Tasfia Sara


Applications in general

ChatGPT Instagram Powerpoint Valorant


18

Lec Tasfia Sara


Programming
Language

Lec Tasfia Sara


Programming Language Examples

20

Lec Tasfia Sara


So many languages!
What will we learn?

Lec Tasfia Sara


C and C++

22

Lec Tasfia Sara


Why C programming language?
● Mother of Programming Languages
● Hardware friendly
● Programs written in C can be easily adapted to different
platforms with little or no modification
● C is the go-to language for microcontroller programming,
used in industries like automotive, aerospace, and consumer
electronics.

23

Lec Tasfia Sara


Computer Languages
● Human languages are known as natural languages. Unfortunately, computers do
not understand natural languages; as a result, we must communicate with
computers using computer languages.
● These languages are-
1. High Level Languages
2. Low Level Languages (Assembly Language)
3. Machine Language

Abstraction increases
Complexity decreases

24

Lec Tasfia Sara


Machine Language
● Machine code, or machine language, is a system of instructions and
data executed directly by a computer's CPU. It is the lowest-level
programming language that can only be understood by computers.
● This type of language can be executed without the need for
translation by a compiler or an assembler.
● Machine Language example: “01001110101010”
● Consists of only binary digits
● Difficult for human usage

25

Lec Tasfia Sara


Low Level Language
● A low-level programming language is a programming language
that provides little or no abstraction from a computer's
instruction set architecture.
● Assembly language is the best example of low-level language;
this is in between machine language and high-level language.
● Low-level languages can convert to machine code without a
compiler or interpreter.
● A program written in a low-level language can be made run
very quickly

26

Lec Tasfia Sara


High Level Language
● A high-level programming language is a programming language that
provides strong abstraction from a computer's instruction set
architecture.
● A programming language that is closer to human languages is
considered a high-level language.
● High-level languages must use an interpreter or compiler to convert a
human-understandable program to computer-readable code (machine
code).
● Example: C, C++, Java, Pascal

27

Lec Tasfia Sara


Program Code Converters
● Interpreter- Interpreter converts a source code, usually on a
step-by-step, line-by-line, and unit-by-unit basis into machine
code. Used by Python, Ruby, PHP, Perl.
● Compiler- Compiler is basically a translator. It is a program
that compiles(translates) the whole source code into
executable instructions(machine language) that a computer
can understand. Used by C, C++.
● Assembler- Assembler normally converts assembly
language’s source code into machine language. Used by
assembly language.
Related links 28

Lec Tasfia Sara


Compiler

29

Lec Tasfia Sara


Compiler
● High Level language or the source code is fed into a compiler
● Compiler translates the high level language to low level
language or machine language

Related links 30

Lec Tasfia Sara


C Programming

Lec Tasfia Sara


Standard Structure of a C Program
// Documentation
There are 6 basic sections responsible for the /**
proper execution of a program. Sections are * file: sum.c
mentioned below: * author: you
* description: program to find sum.
*/
1. Documentation
2. Preprocessor Section (header file) // Header file
#include <stdio.h>
3. Definition
// Definition
4. Global Declaration #define X 20

5. Main() Function // Global Declaration


int sum(int y);
6. Sub-Programs
// Main() Function
int main(void)
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
} 32

Lec Tasfia Sara


Standard Structure of a C Program

33

Lec Tasfia Sara


Standard Structure of a C Program

34

Lec Tasfia Sara


Time for coding

Lec Tasfia Sara


Task
1. Print “Hello World” using printf () function.
2. Print a number using printf () function.
3. Print your details in separate lines using printf() function.
Your details should contain:
i. Your name
ii. Your department name
iii. Your student ID
Iv. Your level

36

Lec Tasfia Sara


How to take
user input?

Lec Tasfia Sara


scanf Statement

declaring the variable

Because we are taking an integer input


memory address
of the variable
Lec Tasfia Sara
Memory Address

▪ Think of it like trying to find a student in a classroom using their


student_id.
▪ If they are arranged randomly, need to check every student from
start to end one by one.
▪ If the student are arranged according to their student_id, it makes
the search alot more efficient.
▪ That’s why computers use addresses. To make searching information
easier.

Lec Tasfia Sara


Memory Address
▪ Let’s assume computer memory looks like this 1 2 3 4
after running the following codes:
int a = 5; 1
int b;
2 a=5
▪ Here, address of a is 22, and address of b is 34.
▪ These numbers are called memory addresses. 3 b=
Actual memory addresses can be huge numbers garbage
and so used in Hexadecimal number system. value

▪ In an actual computer memory, things are a lot 4


more complex, this is a mere simplified analogy.

Lec Tasfia Sara


What is ‘&’ (ampersand) ?
▪ The scanf function expects to receive in the address of the variable. It
can then store the input value into the memory address referred by
the variable. ‘&’ sign indicates the address of the variable.
▪ Every variable identifier indicates a value and the address it stores
the value at. Since just the identifier directly corresponds to the
value, an additional symbol (&) was used along with it to indicate the
address.
▪ ‘&’ reads as “address of”.

Lec Tasfia Sara


More about scanf format string

● When you add text other than white space in a format string that you
pass as an argument to scanf, the user must type input exactly as it
appears in the format string.

● You are essentially requiring the user to type input in a very particular
way.

Lec Tasfia Sara


More about scanf format string

In this given code:

Lec Tasfia Sara


More about scanf format string

In this given code,

Another Input & Output:

Lec Tasfia Sara


More about scanf format string

In this given code,

Expected Input & Output:


Input/Output with multiple variables

Lec Tasfia Sara


Keywords

Lec Tasfia Sara


Keywords
● Linguistically Keyword is a word which has greater significance.
● Keywords in C are words which has some special significant meaning
(which can not be changed) in C programming.
● There are 32 keywords in C. Each of them serves a special purpose.

48

Lec Tasfia Sara


Identifiers

Lec Tasfia Sara


Identifiers
● Identifier refers to the name of variables, functions and
arrays.
● It is a user-defined name.
● Identifier is a series of characters consisting of letters,
digits, and underscores that does not begin with digits.
● Both uppercase and lowercase are permitted.

50

Lec Tasfia Sara


Rules for Identifiers
● An identifier can only have alphanumeric characters (a-z ,
A-Z, 0-9) and underscore (_).
● The first character of an identifier can only contain
alphabet(a-z, A-Z) or underscore (_).
● Identifiers are also case-sensitive in C. For example, name
and Name are two different identifiers in C.
● Keywords are not allowed to be used as identifiers.
● No special characters, such as semicolon, whitespace, slash,
comma etc are permitted to be used in or as identifier
● Should contain maximum 31 characters
51

Lec Tasfia Sara


Example of Identifiers
● (EECE)
● EECE
● 22EECE
● EECE_MIST
● EECE MIST
● 123
● EECE22

52

Lec Tasfia Sara


Example of Identifiers
● (EECE) invalid
● EECE valid
● 22EECE invalid
● EECE_MIST valid
● EECE MIST invalid
● 123 invalid
● EECE22 valid

53

Lec Tasfia Sara


Data Types

Lec Tasfia Sara


Data Types
● In programming everything we deal with is “Data”.

● There are various types of data. Like character, decimal


numbers, natural numbers etc.

● There are different data types for these.

55

Lec Tasfia Sara


Data Types
To visualize the need for different data types we can see an
example:
● Suppose you have 3 material : sand, water, oxygen gas and
3 containers: 1 paper box, 1 bowl and 1 cylinder. You can
put one material in one container. What will you put in
which container?

56

Lec Tasfia Sara


Data Types

57

Lec Tasfia Sara


Data Types

58

Lec Tasfia Sara


Data Types

Data Type Size Format


Characteristics Values Range Examples
Keywords (bytes) Specifiers

int Positive and 4 -2147483648 to 2147483647 %d 0, 5, -15

long long negative whole 34359738368


numbers 8 -(2^63) to (2^63)-1 %lld
int (i.e. 2^35)

float Decimal/Fraction 4 Upto 7 digits after radical point %f 1.25

double all numbers 8 Upto 15-16 digits after radical point %lf 3.1416….

Letters, symbols, ASCII Chart Characters


char 1 %c ‘A’, ‘b’, ‘9’ etc.
digits (-128 to 127)

59

Lec Tasfia Sara


ASCII TABLE

60

Lec Tasfia Sara


ASCII TABLE
An interesting detail

The difference between numbers


assigned to the small case and the
large case of the same letter in the
ASCII chart is 32.

Example:

Lec Tasfia Sara


Variables

Lec Tasfia Sara


Variables
● A variable is data name that may be used to store a data value.
1. Variable names correspond to locations in the
computer's memory

2. Every variable has a name, a type, a size and a value

3. Whenever a new value is placed into a variable, it


replaces (and destroys) the previous value

4. A variable must be declared before it is used

5. Variable declaration format:


i. data_type variable_name;
ii. data_type variable1_name, variable2_name, variable3_name;

63

Lec Tasfia Sara


Variables - Declaration Syntax

Lec Tasfia Sara


Variable Declaration and Assignment

Lec Tasfia Sara


Variable Declaration and Assignment
More Variations

Lec Tasfia Sara


Variable Declaration and Assignment
More Variations

Lec Tasfia Sara


Variable Declaration and Assignment
More Variations

Lec Tasfia Sara


Variable Declaration and Assignment
More Variations

Lec Tasfia Sara


Variable Declaration and Assignment
Common Mistakes

Lec Tasfia Sara


Variable Declaration and Assignment
Common Mistakes

Lec Tasfia Sara


Format Specifiers

Lec Tasfia Sara


Format Specifiers in C
● The format specifier is used during taking input from an user
and showing output.
● It is a way to tell the compiler what type of data is in a
variable during taking input using scanf() or printing using
printf().

73

Lec Tasfia Sara


Outputting Variable Syntax
and Format Specifiers….

declaring the variable

This is where the value in the variable giving the variable


gets pasted - a format specifier to printf
Lec Tasfia Sara
Outputting Variable Syntax
and Format Specifiers….

Lec Tasfia Sara


Outputting Variable Syntax

Notice the single quotation ‘

Lec Tasfia Sara


Outputting Variable Syntax

Lec Tasfia Sara


Effect of using incorrect format specifiers
#include<stdio.h>
int main() ▪ Output:
{
char var;
A
var= ‘A’;
printf(“\n%c”, var); 65
printf(“\n%d”, var);
}

Lec Tasfia Sara


Effect of using incorrect format specifiers
#include<stdio.h>
int main() ▪ Output:
{
int var;
var= 1.3;
1
printf(“\n%d”, var);
} This is implicit typecasting

Lec Tasfia Sara


Example: Printing Integer Value
#include<stdio.h>
int main() {
int var;
var = 7 + 8;
printf(“%d”, var);
printf(“\nResult is %d.”, var);
}

80

Lec Tasfia Sara


Example: Printing float Value
#include<stdio.h>
int main() {
float operand1, operand2, result;
operand1 = 2.1;
operand2 = 3.1;
result = operand1 + operand2;
printf(“\nResult of the addition operation is %f”, result );
}

81

Lec Tasfia Sara


Example: Printing char Value
#include<stdio.h>
int main() {
char var;
var= ‘A’;
printf(“\n%c”, var);
}

82

Lec Tasfia Sara


Example: Printing char Value
#include<stdio.h>
int main() {
char var;
var= ‘A’;
printf(“\n%c”, var);
printf(“\n%d”, var);
}

83

Lec Tasfia Sara


Example: Taking Input and Printing float Value

84

Lec Tasfia Sara


More with Format Specifier

#include<stdio.h>
int main() {
float var=1.3143547;
printf(“\n%f”, var);
printf(“\n%.2f”, var);
}

85

Lec Tasfia Sara


Why ampersand is used in scanf() function but
not in printf() function?
● Ampersand sign = &
● The scanf function
#include<stdio.h> expects to receive in the
int main() { address of our variable. It
float operand1, operand2, result; can then store the value
scanf(“%f”,&operand1 ); into our variable. & sign
scanf(“%f”,&operand2 ); indicates the address of
the variable
result = operand1 + operand2;
● The printf function directly
printf(“\nResult of the addition of %f prints the value stored in
and %f is %f”, result ); the variable. It does not
} need to print value from
the variable’s address
86

Lec Tasfia Sara


TASK
1. Take input of an integer number and print it
2. Take input of 3 integer numbers and print the sum of them
3. Take input of 2 integer numbers and calculate the sum,
subtraction, multiplication of them. Print each of them in new
lines.
4. Take input of 1 integer and 1 float number. Print the sum of
them.
5. Take input of 5 integer numbers , calculate their average and
print the average result.
6. Take a character input and print its corresponding ASCII
value. 87

Lec Tasfia Sara


Lec Tasfia Sara

You might also like