Sie sind auf Seite 1von 7

Content of Topic 1

EEE145 Computer Programming


Introduction

Topic 1

The Course The Computer The Computer Programming

Dr. Ahmet BNGL Department of Engineering Physics University of Gaziantep


01/02/2010

Modifications by Dr. Andrew BEDDALL Department of Electric and Electronics Engineering

First C++ Program Compiling and Running a First C++ Program

Sayfa 1

Sayfa 2

Introduction
Computer programming is an essential part of the work of many scientists and engineers. This semester we will teach C++ Programming Language which
is not easy to learn (compared to Fortran) has a lot of technical programming details is good at system and hardware programming is mostly prefered by engineers and young programmers! is widely used in the software industry

THE COURSE

Sayfa 3

Sayfa 4

Web Resources for the Course


Course web page (for Exam questions/solutions/results) www.gantep.edu.tr/~bingul/eee145 University of Gaziantep C++ resource page cpp.gantep.edu.tr - Tutorials, Sample Programs, Howtos - We will follow the Basic tutorial section. C++ Resources Network: www.cplusplus.com C++ Reference: www.cppreference.com Trke C Programlama Diline Giri: www.gantep.edu.tr/~bingul/c Also search Google and Wikipedia for any of the expressions used in this course.
Sayfa 5

The Course Books


Programming with C++ John R. Hubbard Schaum Outline Series (2000) ~ 15 $ Practical C++ Programming Steve Qualline OReilly Media (2003) ~ 25 $
Sayfa 6

The Course Content


Introduction to Programming History of C++ Structure of C++ C++ Basics
Fundamental Input / Output, Data Types, Strings, Operators, Variables, Constants, Intrinsic Functions

Lectures, Labs, Attendance, Exams


Mondays
two-hour lecture 20:40-22:30 EEE D1

Fridays days
two-hour programming session 20:40-22:30 BIM lab 3

Control Structures: Selection & Loops Functions Arrays File Processing References and Pointers
Sayfa 7

Attendance
You must attend at least 70% of the course

Exams
All all written exams First midterm 30% Second midterm 30% Final 40%
Sayfa 8

Computer Labs
We will learn C++ using the Windows operating system
You may also use a Linux environment, e.g. Ubuntu or gul4.bim .

Every week you should write a few programs yourself in the BIM lab (and more in your free time). In the BIM Computer Labs your programs will be deleted when the computer reboots. So you need to save them on a flash drive or on the internet.

THE COMPUTER

DO NOT FORGET TO BRING YOUR LAPTOP or FLASH DISK WITH YOU


Sayfa 9 Sayfa 10

The Computer
See http://en.wikipedia.org/wiki/Computer A computer is a machine that manipulates data according to a set of instructions. First computers were developed in 19401945, they were very large in size. Modern computers are based on integrated circuits making them very fast and small in size.
Sayfa 11 2009 EP241 - Computer Programming Sayfa 12

Parts of a Digital Computer


A computer can be divided into two main parts: Hardware and Software.
Hardware (=Donanm) See http://en.wikipedia.org/wiki/Hardware Hardware is the electronic and mechanical parts of the computer. - Storage Units - Input Units - Output Units - Process Units
Sayfa 13

Parts of a Digital Computer


Storage Units
These are used in both input and output of data:
HDD (Hard Disk Drive) high capacity, relatively cheap, slow. RAM (Random Access Memory) low capacity, expensive, but very fast. Others: Flash memory (memory cards, USB flash drives), CD, DVD...

Sayfa 14

Parts of a Digital Computer


Input Units
Used for input of data:
Keyboard, Mouse Touch screen/pad RAM, HDD

Parts of a Digital Computer


Output Units
Used for output of data:
Monitor Printer Speaker RAM HDD Flash Memory

Flash memory

Sayfa 15

Sayfa 16

Parts of a Digital Computer


Process Units
CPU: Central Processing Unit This coordinates the operation of computer system and performs arithmetic logic operations.

Parts of a Digital Computer

A program is input from an HDD(1) and executed in RAM(2).

Input unit

CPU

Output unit

Data is input from a keyboard(3) which is again stored in RAM(4). The CPU operates on the program and data in RAM(5) and outputs results to the HDD(6) as well as the monitor(7). This is all controlled by the CPU requiring only basic data flow instructions from the programmer.
Sayfa 17 Sayfa 18

Parts of a Digital Computer


Software (=Yazlm) See http://en.wikipedia.org/wiki/Software Software consists of programs loaded from storage units. The programs execute on the computer hardware forming, for example, Operating System (OS) Compilers Application Programs

Parts of a Digital Computer


Operating System (OS)
The OS is a program written to interface between the computer and its user. All other software runs under the OS. Examples are: Miscrosoft Windows (XP, Vista, 7) Linux (e.g. Redhat, Ubuntu, Pardus) Mac OS X.

Sayfa 19

Sayfa 20

Parts of a Digital Computer


Compilers
See http://en.wikipedia.org/wiki/Compiler Many programming languages require a compiler to translate translates the statements of program written in a high level language into a low level language (machine code). Examples are: Fortran C C++ Java Pascal Basic.
Sayfa 21

Parts of a Digital Computer


Application Programs
These are (usually compiled) programs written to perform a specific task. Examples are: Microsoft Word AutoCAD Mozilla Firefox....

Sayfa 22

Computer Programming
See http://en.wikipedia.org/wiki/Computer_programming

INTRODUCTION TO PROGRAMMING

Computer programming (coding) is the process of writing, Testing / debugging / troubleshooting maintaining the source code of computer programs. The source code is written in a programming language, e.g.
// A simple C++ program #include <iostream> int main(){ cout << "Hello World!\n"; return 0; }
Sayfa 23 Sayfa 24

Generating an Executable File


set of instructions written by the programmer (high level code) hello.cpp

Problem Solving with Computers


Problem solving with computers involves several steps:
1. 2. 3. 4. 5. 6. 7. 8. Clearly define the problem Analyse the problem and formulate a method to solve it Describe the solution in the form of an algorithm. Draw a flowchart of the algorithm Write the computer program Compile and run the program (debugging) Test the program (debugging) Interpretation of results

Translates the source code into machine code (low level code)

The compiled code (low level code)

Generates a machine code (executable code or executable program)

The executable program that you run under an operating system (hello.exe)
2009 Sayfa 25 Sayfa 26

Algorithms & Flow Charts


Algorithm gives a step-by-step description of the solution
S1: S2: S3: S4: S5: S6: S7: S8: Start Input N Set M = 1 and F = 1 Set F = F*M If M = N GOTO S7 Set M = M + 1 and GOTO S4 Output F End Beginning or end of an algorithm

Input or output of information A computation Decision making

The beginning of the repetition structure. The direction of flow of the algorithm. Circles with arrows connect the flowchart between pages.

Flow chart gives the logical flow of the solution in a diagrammatic form.
Sayfa 27

Sayfa 28

What is C++?
See http://en.wikipedia.org/wiki/C++ C++ (pronounced "C plus plus")
is a general-purpose and middle-level programming language is an enhancement to C was developed by Danish computer scientist Bjarne Stroustrup in 1979 (called C with Classes) at Bell Labs (named C++ in 1983) was ratified in 1998 ISO/IEC 14882:1998 and in 2003 ISO/IEC 14882:2003

A FIRST C++ PROGRAM

Recently, a revised ISO C++ standard, known informally as C++0x has been produced. You can find a recent talk given by him at CERN:
http://indico.cern.ch/conferenceDisplay.py?confId=67017
Sayfa 29 Sayfa 30

A First C++ Program


// First C++ program #include <iostream> using namespace std; int main() { cout << "Hello World!"; return 0; } Program source file name: hello.cpp In general C++ files have extentions: .cpp, .c++, .cxx, .cc
Sayfa 31

1: 2: 3: 4: 5: 6: 7: 8: 9:

// First C++ program #include <iostream> using namespace std; int main() { cout << "Hello World!"; return 0; }

Line 1: Lines starting with // are considered as comment. Line 2: Lines starting with # are directives for preprocessor #include <iostream> tells the compiler to include the iostream file containing declarations of basic input output. Line 3: All elements (variables, objects, ...) of C++ library declared in std namespace. cout is the part of std namespace. Line 4: An empty line does nothing but helps readability. Line 5: Actual program, program starts with main() function. Each C++ program must have only one main() function. The beginning and end of the main() block is indicated by braces { }. Line 7: Outputs Hello World to the user screen. Line 9: return statement terminates the function (here main program). return 0 sends a message to OS: program ends without an error
EP241 - Computer Programming Sayfa 32

Compile and Run


We will use Dev-C++ compiler
http://www1.gantep.edu.tr/~cpp/howto-compileDev-C++.php

A Second Program
// Calculates the sum of two integers #include <iostream> using namespace std; int main() { int a, b, total; cout << "Enter two integers: "; cin >> a >> b; total = a + b; cout << "The sum is " << total << endl; return 0; }

output

EP241 - Computer Programming

Sayfa 33

2009

EP241 - Computer Programming

Sayfa 34

Standard Input and Output (I/O)


The standard C++ library includes the header file iostream, where the input and output stream objects are declared. cout to output data to the screen cin to input data from the keyboard.
This specific file (iostream) can be found usually under the folder: for Linux (GCC): /usr/include/ for Windows (Dev-C++): C:\Dev-Cpp\include\
Sayfa 35

Standard Input and Output (I/O)


Basic Output
cout cout cout cout << << << << "Hello World!"; "Hello " << "World!"; 1453; x; // // // // Outputs Outputs Outputs Outputs Hello World! Hello World! the number 1453 the content of x

Line break on output


cout << "University of "; cout << "Gaziantep"; cout << "University of\n "; cout << "Gaziantep"; // outputs: // University of Gaziantep // University of // Gaziantep

cout << "University of " << endl; // University of cout << "Gaziantep; // Gaziantep

Sayfa 36

Standard Input and Output (I/O)


Basic Input
cin >> a; cin >> a >> b; // reads variable a from keyboard // reads variables a and b

Standard Input and Output (I/O)


If you remove the line using namespace std; then the source code on the previous pages needs modifying as follows:
// Calculates the sum of two integers #include <iostream> int main() { int a, b, total; std::cout << "Enter two integers: "; std::cin >> a >> b; total = a + b; std::cout << "The sum is " << total << std::endl; return 0; }
Sayfa 37 Sayfa 38

// Calculates the sum of two integers #include <iostream> using namespace std; int main() { int a, b, total; cout << "Enter two integers: "; cin >> a >> b; total = a + b; cout << "The sum is " << total << endl; return 0; }

TO DO BEFORE THE NEXT LECTURE 1. Browse through the course website


http://www1.gantep.edu.tr/~bingul/eee145

2. Do the homework (topic 1):


cpp.gantep.edu.tr 3. Print next lectures notes: http://www1.gantep.edu.tr/~bingul/eee145

2009

EP241 - Computer Programming

Sayfa 39

Das könnte Ihnen auch gefallen