You are on page 1of 65

MEK0101 :

Bilgisayar
Programlama I
2021-2022 Güz

Hafta 2: Algoritmalar

Dr. Öğr. Üyesi


Selma YILMAZYILDIZ KAYAARMA
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

2
Teaching Team

3
Recap from the last week

✓Basic components of a computer (memory, CPU,


input/output devices)
✓Software and hardware components
✓What programming, computer program and programming
languages are
✓How to write a computer program
✓Algorithm is the heart of programming! (Remember:
Peanut butter and honey sandwich)

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

Modify the program to meet changing


requirements or to correct any errors that show
up while using it.
Mcmillan

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

1. Analysis and specification


2. Design the algorithm to solve the problem
3. Choose the programming language
4. Implement the algorithm
5. Test and verify the completed program
6. Maintain and update the program

© 2016 Pearson Education, Ltd. All rights reserved.


10
Summary of the steps:
PROBLEM

1. Analysis and specification


2. Design the algorithm to solve the problem
3. Choose the programming language
4. Implement the algorithm
5. Test and verify the completed program
6. Maintain and update the program

COMPUTER PROGRAM THAT SOLVES THE PROBLEM

© 2016 Pearson Education, Ltd. All rights reserved.


11
Summary of the steps:
PROBLEM

1. Analysis and specification


2. Design the algorithm to solve the problem
3. Choose the programming language
4. Implement the algorithm PROGRAMMING
5. Test and verify the completed program
6. Maintain and update the program

COMPUTER PROGRAM THAT SOLVES THE PROBLEM

© 2016 Pearson Education, Ltd. All rights reserved.


12
• Analysis and specification: Preliminary studies that provides a complete
understanding of the problem to be solved.
• Algorithm development: Expressing the steps to solve the problem in order (verbal
and/or with fıgures).
• Choosing the programming language: Choosing a computer programming language
that will easily transfer the developed algorithm to the computer environment
• Implementing: The phase of writing the program using the rules of the selected
programming language
• Compilation: Checking the solution that is converted to the programming language
commands for typos and converting the program to machine language commands
• Execution: The stage in which the compiled program is taken from the hard disk by
the operating system and moved to memory and the address of the program's first
instruction is passed to the Central Processing Unit (CPU).
• Test and Validation: The phase where the program is logically tested and checked
whether it produces correct results for each possible input.
• Maintain and Update: The phase in which the program is updated in accordance
with the testing phase.

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)

We use variables to temporarily store information in a


program and to act on this information when needed.

17
Assignment (Atama)

The process of passing a value or the result of an


expression/operation into any variable is called
assignment.

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.

Meat grinder has a function


that turns a piece of meat into
minced meat.

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.

counter = counter + 3 This is an incrementing counter by three


counter = counter - 5 This is a decrementing counter by five

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:

© 2016 Pearson Education, Ltd. All rights reserved. 32


Flow Charts (Akış Diyagramları)
• A flowchart is a graphical representation of an
algorithm or of a portion of an algorithm.
• Flowcharts are drawn using certain special-purpose
symbols such as rectangles, diamonds, rounded
rectangles, and small circles; these symbols are
connected by arrows called flowlines.
• Like pseudocode, flowcharts are useful for
developing and representing algorithms, although
pseudocode is preferred by most programmers.
A simple flowchart representing
a process for dealing with a
non-functioning lamp

© 2016 Pearson Education, Ltd. All rights reserved.


33
Flowcharts (Cont.)

• When drawing a flowchart that represents a complete algorithm, a


rounded rectangle symbol containing the word “Begin/Start” is the first
symbol used in the flowchart; an oval symbol containing the word “End”
is the last symbol used.
• Perhaps the most important flowcharting symbol is the diamond
symbol, also called the decision symbol, which indicates that a decision
is to be made.

© 2016 Pearson Education, Ltd. All rights reserved. 34


https://en.wikipedia.org/

35
Example:
Develop the algorithm of the program that
calculates the average of the 2 given numbers and
draw the flowchart.

36
Example: Start

Develop the algorithm of the program that number1=?


calculates the average of the 2 given numbers and
draw the flowchart.
number2=?

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

Develop an algorithm that prints the sum and total = 0


average of numbers from 1 to 100 (including 100)
and draw a flowchart. counter = counter + 1

S1: Start total = total + counter


S2: total = 0
S3: counter = 0 Counter
S4: counter = counter + 1 = 100

S5: total = total + counter


average = total/counter
S6: If the counter is not equals to 100, go to step 4
S7: average = total/counter total, average
S8: Print the total, average
S9: End End

39
40
Summary of the steps:
PROBLEM

1. Analysis and specification


2. Design the algorithm to solve the problem
3. Choose the programming language
4. Implement the algorithm PROGRAMMING
5. Test and verify the completed program
6. Maintain and update the program

COMPUTER PROGRAM THAT SOLVES THE PROBLEM

© 2016 Pearson Education, Ltd. All rights reserved.


41
The C Language

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.

High-level programming last = first + second


language
(e.g. C, C++, Java)

Low-level programming load first


language add second
(e.g. Assembly) store last

Machine language 101010101111101001011


( binary code) 00011110101100110101

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

C compiler takes “example.c”, converts


it into machine language form and then
the Linker generates and saves the
binary “example.exe”

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.

© 2016 Pearson Education, Ltd. All rights reserved.


53
Our First C Program
/* Program comments:
* hello.c /* your comment */
* This program prints a welcome message OR
* to the user. //
*/ • You can place comments in your
source code that are not
#include <stdio.h> // for printf executed as part of the program
• They will be ignored by the
/* my first program in C */
int main() { compiler
printf("Hello BTU 2021-2022 class! \n"); • They provide clarity to the C
return 0; source code allowing others to
} better understand what the code
was intended
• Adding comments to your C
source code is a highly
recommended in your
© 2016 Pearson Education, Ltd. All rights reserved. assignments and projects
54
Preprocessor (ön-işlemci) directive

Our First C Program #include <stdio.h>


/* • Tells a C compiler to include
* hello.c stdio.h header file before going
* This program prints a welcome message to actual compilation
* to the user. • C libraries (which includes
*/ functions such as “printf”) are
written with angle brackets.
#include <stdio.h> // for printf
/* my first program in C */
int main() {
printf("Hello BTU 2021-2022 class! \n");
return 0;
}

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
}

© 2016 Pearson Education, Ltd. All rights reserved.


56
Our First C Program
/*
* hello.c
* This program prints a welcome message
* to the user.
*/
#include <stdio.h> // for printf
/* my first program in C */
int main() {
printf("Hello BTU 2021-2022 class! \n");
return 0;
} {…}
• Brackets define code
blocks

© 2016 Pearson Education, Ltd. All rights reserved.


57
Our First C Program
/* printf function :
* hello.c
* This program prints a welcome message printf(...)
* to the user.
*/ • printf(...) is another function
available in C which causes the
#include <stdio.h> // for printf message " Hello BTU 2021-
2022 class! " to be displayed on
the screen.
/* my first program in C */
int main() { • Also useful for seeing the results
printf("Hello BTU 2021-2022 class! \n"); of a program execution
return 0;
}

© 2016 Pearson Education, Ltd. All rights reserved.


58
Our First C Program
/*
* hello.c
* This program prints a welcome message
* to the user.
*/
#include <stdio.h> // for printf ;
/* my first program in C */
int main() { • Semicolon marks the end
printf("Hello BTU 2021-2022 class! \n"); of our statements.
return 0; • If you forget to put the
} semicolon (;), you’ll
receive errors, and your
code will not run!

© 2016 Pearson Education, Ltd. All rights reserved.


59
Our First C Program
/*
* hello.c
* This program prints a welcome message
* to the user.
*/
#include <stdio.h> // for printf
/* my first program in C */ return:
int main() {
printf("Hello BTU 2021-2022 class! \n"); return 0
return 0; • It is a part of main() function
} • It terminates the main() function
and returns the value 0.
• Should always return an integer
(0 = success without errors)

© 2016 Pearson Education, Ltd. All rights reserved.


60
In summary a C program basically consists of the following parts:

➢ Comments
➢ Preprocessor commands
➢ Functions
➢ Variables
➢ Statements & Expressions

© 2016 Pearson Education, Ltd. All rights reserved.


61
Plan for Today
• Course Structure and Rules
• Introduction
• Some Fundamentals and Getting Started with C
• Our First C Program
• Compiling and Running Our First C Program

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

• Do not be afraid of using the computer


• “What if I write xyz in my program instead of pqr?”
-> Just do so and find out
• Be adventurous.
• Exercise your knowledge by writing programs that is the real
test

65
Haftaya görüşmek
üzere!

66

You might also like