Professional Documents
Culture Documents
Mek0101 - 02 - 11.10.2021
Mek0101 - 02 - 11.10.2021
Bilgisayar
Programlama I
2021-2022 Güz
Hafta 2: Algoritmalar
2
Teaching Team
3
Recap from the last week
5
Plan for Today
• Recap from last week
• Algorithms
• Fundamental Concepts in Programming
• Getting Started with C
• Our First C Program
• Compiling and Running Our First C Program
6
Writing a computer program
• To write a program for a computer to follow, we must go
through a two-phase process:
• problem solving and
• implementation
7
Understand the Translate the algorithm
problem and what the (the general solution)
solution must do into a programming
language
Specify the logical
sequences of steps that
solve the problem Have the computer
follow the instructions.
If you find errors,
Follow the steps to see analyze the program
if the solution really and algorithm, correct
does solve the problem
8
• If you have a well-structured algorithm, you can write your
program in the language of your choice.
• Algorithms are the hearth of programming process
9
Summary of the steps:
PROBLEM
13
Algorithms
• The solution to any computing problem involves executing a
series of actions in a specific order.
• A procedure for solving a problem in terms of
– the actions to be executed, and
– the order in which these actions are to be executed
• is called an algorithm.
• Correctly specifying the order in which the actions are to be
executed is important.
14
Plan for Today
• Recap from last week
• Algorithms
• Fundamental Concepts in Programming
• Getting Started with C Language
• Our First C Program
• Compiling and Running Our First C Program
15
Some Fundamental Concepts in
Programming
1. Variable (Değişken)
2. Assignment (Atama)
3. Function (Fonksiyon)
4. Counter (Sayaç)
5. Loop (Döngü)
16
Variable (Değişken)
17
Assignment (Atama)
18
variable = expression
değişken = ifade
• "variable" is the name of any variable
• "expression" can be a mathematical,
logical, or alphanumeric expression. Y=9
• "=" symbol in between is called the X = 26
"assignment operator" and assigns the X=3
result of the expression or operation on
the right to the variable on the left Y=X+5
• In this case, the old value of the variable As a result of the " = " operation,
(if any) is deleted. the previous value of Y is deleted
and replaced with a value of 8.
19
Function (Fonksiyon)
Programs that return an output
parameter(s) after processing
an input parameter(s) are
called functions.
20
Counter (Sayaç)
• In our programs, some operations may need to be
performed a certain number of times or the processed
values may need to be counted. Such variables used for
counting purposes are called counters.
• Example: In the program that finds how many vowels are in a sentence entered from the
keyboard:
• each letter of the sentence is called in order and is investigated whether it belongs to
the set of vowels.
• If the called letter belongs to this set, the value of the variable that will count them is
incremented by one
21
counter = counter + 1
• "1" is added to the previous value of the variable named "counter", and the
result found is transferred to itself as the new value.
• Such variables are called "counters" in the algorithm.
• Each time the process flow comes to it, its value increases/decreases as much
as the specified step value.
22
Example
In the algorithm below, numbers 1-5 (1 inclusive, 5 excluded) are printed on the
screen using the counter.
S1: Start
S2: counter = 1
S3: If the counter equlas to 5, go to step 7
S4: Print counter
S5: counter = counter + 1
S6: Go to step 3
S7: End
23
Loop (Döngü)
• In many programs, some operations are
performed for certain sequential values or
for a certain number of times.
• Process flow cycles that repeat certain
blocks of operations in programs a certain
number of times are called loops.
24
Rules for loop
1. The initial value of the loop variable is determined.
2. The ending value of the loop variable is determined.
3. It is tested whether the loop variable has reached its ending value.
4. The requested operation is performed.
5. The loop variable is incremented or decremented by the amount of
steps in the loop.
25
Example
The following algorithm calculates the sum of odd numbers from 1 to 10. .
S1: Start
S2: sum = 0
S3: counter = 1
S4: If the counter > 10, go to step 8 Loop
S5: sum = sum + counter
S6: counter = counter + 2
S7: Go to step 4
S8: Print sum
S9: End
26
More algorithms
The algorithm of the program that finds the sum of two numbers entered as
input is as follows:
27
More algorithms (cont.)
The algorithm of the program that finds the sum of two numbers entered as
input is as follows.
S1: Start
S2: Enter the value of number1
S3: Enter the value of number2
S4: sum = number1 + number2
S5: Print the sum
S6: End
28
More algorithms (cont.)
Develop an algorithm that prints the cumulative sum of numbers from
0 to 100.
S1: Start
S2: total = 0
S3: counter = 1
S4: If the counter equals to 100, go to step 7
S5: total = total + counter
S6: counter = counter + 1 and go back to step 4
S7: Print the total
S8: End
29
Algorithms can be designed through the use of:
- flow charts or
- pseudocode
30
Pseudocode (Sözde kod)
• Pseudocode
• is one of the tools that can be used to write a preliminary plan that can be
developed into a computer program.
• is a generic way of describing an algorithm without use of any specific
programming language syntax
• Pseudocode consists only of action and decision statements—those that are
executed when the program has been converted from pseudocode to a
programming language and is run.
• Definitions are not executable statements. They do not cause any action—such
as input, output, a calculation or a comparison—to occur when the program is
executed.
• They’re simply messages to the compiler.
© 2016 Pearson Education, Ltd. All rights reserved.
31
Pseudocode (Cont.)
Some examples:
35
Example:
Develop the algorithm of the program that
calculates the average of the 2 given numbers and
draw the flowchart.
36
Example: Start
S1: Start
average = (number1 + number2)/2
S2: Enter the value of number1
S3: Enter the value of number2
S4: average = (number1 + number2)/2 average
S5: Print the value of average
S6: End
End
37
Example:
Develop an algorithm that prints the sum and
average of numbers from 1 to 100 (including 100)
and draw a flowchart.
38
Start
Example: counter = 0
39
40
Summary of the steps:
PROBLEM
C was created around 1970 (by Dennis M. Ritchie) to make writing Unix and
Unix tools easier.
• Part of the C/C++/Java family of languages (C++ and Java were created
later)
• High – level programming language
42
High
Human language Add the first value to the second
(e.g. English, Turkish) and store the result to the last.
Low
43
Why C?
Key advantages of learning C:
• Easy to learn
-> It may not feel like it but C is one of the easiest languages to learn.
• C is small (only 32 keywords)
• C is stable (the language doesn’t change much)
• C is the language of choice for fast, highly efficient programs.
• It can handle low-level activities
-> C lets you work at a lower level to manipulate and understand the underlying system.
• Many tools (and even other languages, like Python!) are built with C.
• C is popular for systems programming (operating systems, networking, etc.)
44
The TIOBE Programming
Community index is an
indicator of the popularity of
programming languages
https://www.tiobe.com/tiobe-index/
45
Some Facts about C
➢ The reason why the language ➢ C was invented to write an
was named “C” by its creator operating system called
was that it came after B UNIX
language!
The Unix operating system was originally created at Bell
➢ C is the mother of many Labs. It was implemented on the PDP-7 computer using the
assembly language. Later, Unix was ported to PDP-11 using
other languages! assembly language but the developers thought about
rewriting it using B language.
- C++, C#, Python, Java, JavaScript, Perl, PHP….
-> the development of all these languages has been The B language fell short of abilities to take benefit of the
influenced by C language. features offered by the newer member of the PDP series,
For example: the PDP-11. That’s when Dennis Ritchie started working on
- Python uses C for creating standard libraries a new high-level programming language taking clues from
- The syntaxes and control structures of C++, B language.
PHP and Perl are based on C.
46
Some programmer jargon
Some words that will be used a lot:
➢ Source code: The stuff you type into the computer. The program you are writing.
➢ Compile (build): Taking source code and making a program that the computer
can understand.
➢ Executable: The compiled program that the computer can run.
➢ Language: (Special sense) The core part of C central to writing C code.
➢ Library: Added functions for C programming which are bolted on to do certain
tasks.
➢ Header file: Files ending in .h which are included at the start of source code.
47
➢ Programming languages are developed so that the machine-
machine and machine-human can understand each other
➢ A program written in a specific programming language (such as C)
doesn’t work/run on a computer directly
➢ For the programs to work, a compiler and a linker is needed
48
example.c
“example.c “ is your C source-code. It
can be opened and read by a standard
text editor (such as Notepad, MS Word,
etc.).
The total process of going
from source code files to an
executable might be referred
to as a build
C
Compiler and example.exe
Linker
49
• Compilation:
• is the processing of source code files (.c, .cc, or .cpp) and the creation of an 'object' file.
• This step doesn't create anything the user can actually run.
• Instead, the compiler merely produces the machine language instructions that correspond to
the source code file that was compiled.
• For instance, if you compile (but don't link) three separate files, you will have three object files
created as output, each with the name <filename>.o or <filename>.obj (the extension will
depend on your compiler). Each of these files contains a translation of your source code file
into a machine language file -- but you can't run them yet! You need to turn them into
executables your operating system can use. That's where the linker comes in.
• Linking:
• refers to the creation of a single executable file from multiple object files.
• In this step, it is common that the linker will complain about undefined functions (commonly,
main itself). During compilation, if the compiler could not find the definition for a particular
function, it would just assume that the function was defined in another file.
50
Computers can only understand commands that is constructed
as 1`s and 0`s.
-> called binary format
51
Plan for Today
• Course Structure and Rules
• Syllabus Overview
• Introduction
• Some Fundamentals and Getting Started with C
• Our First C Program
• Compiling and Running Our First C Program
52
Our First C Program
/* • C program should be written
* hello.c into one or more text files
* This program prints a welcome message
* to the user. with extension ".c“.
*/ Ex: “hello.c”
• You can use any text editor
#include <stdio.h> // for printf (Notepad, vim, TextPad, Visual
/* my first program in C */ Studio Code) to write your C
int main() { program into a file.
printf(" Hello BTU MCH0113 class! \n"); • The files you create with your
return 0; editor are called the source
}
files.
55
Our First C Program
/* Main function :
* hello.c
* This program prints a welcome message int main() {}
* to the user.
*/ • Is the main function where the
program execution begins
#include <stdio.h> // for printf • Entry point for the program
• It is "called" by the operating
/* my first program in C */ system when the user runs the
int main() { program
printf("Hello BTU 2021-2022 class! \n"); • Your programs must have a main
return 0; function
}
➢ Comments
➢ Preprocessor commands
➢ Functions
➢ Variables
➢ Statements & Expressions
62
Let’s Compile and Execute our First C Program
Let us see how to save the source code in a file, and how to compile and run it...
63
64
How to master the course
65
Haftaya görüşmek
üzere!
66