Sie sind auf Seite 1von 55

CEN 111

Introduction to Algorithms
and Programming
2013-2014 Spring Term
Faculty of Engneerng and Information
Technologies
Selcuk Cankurt
scankurt@ius.edu.ba
Main Textbook
H.M.Deitel & P.J.Deitel, C++ How to Program, Eighth
Edition, Prentice Hall PTR, 2011.


Supplementary Textbook
H.M.Deitel & P.J.Deitel, C How to Program, seventh
International Edition, Prentice Hall PTR, 2012.
Textbooks

COMPUTER SYSTEM
SOFTWARE
HARDWARE
Computer is a device that can perform computations
and make logical decisions.
Programs are the set of the instructions
which tell to computer what to do and
how to do.
Parts of a computer (e.g., the keyboard,
screen, mouse, hard disks, memory, DVDs and
processing units)
Computer Organization
ALU and CU
INPUT OUTPUT
PROCESSING= Arithmetic Logic
Unit + Control Unit
STORING
MEMORY
Every computer involves six units or sections.
keyboard, mouse
hard disks, DVDs, memory sticks
RAM, ROM
screen, printer
processor
HARDWARE

SOFTWARE

Application
Programs
System Programs
Operating System Programs
UNIX, Linux, Mac OS X, iOS,
Android, Solaris and
Windows.
Programming languages
C, C++, Basic, Pascal,
Fortran, Cobol, Java, C#
Word processing, text editor
Spreadsheets
Notepad, MS Word
MS Excel
Presentation programs
MS PowerPoint
Database programs
MS Access, MS SQL, Oracle, MySQL

Type of the programming
language
i ) low level programming
languages
Machine language
Assemble language

ii ) high level programming
languages
e.g.: Pascal, C, C++, Fortran

Low level languages
Machine languages are the only languages understood by the
computers directly. But, machine languages are not human
readable languages because they consist entirely a stream of
0's and 1's. Each type of processor has its own machine
language and assembly language, so an assembly language
program written for one type of processor won't run on
another. Machine language performs much faster.

Assembly language is similar to machine language, but they are
much easier to program in because they allow a programmer to
use English-like abbreviations rather than numbers.
Programs so-called assemblers are used to convert assembly-
language programs to machine languages.
High-level languages make the programmers able to type instructions
almost like everyday English and mathematical notations. Using high-
level languages speeds the time of the program writing process.
Programs so-called compilers convert high-level language programs
into machine language.
C, C++, Visual Basic.NET, Visual C++, Visual C#, Java are among the most
widely used high-level programming languages.
High level languages
History of C and C++
BCPL was developed in 1967 by Martin Richards as a language for writing
operating systems software and compilers for operating systems.
Ken Thompson modeled many features in his language B after their
counterparts in BCPL.
The C language was evolved from B by Dennis Ritchie at Bell Laboratories.
C++, an extension of C, was developed by Bjarne Stroustrup in the early 1980s
at Bell Laboratories.
Java is one of the most used programming language to develop large-scale enterprise
applications, to enhance the functionality of web servers (the computers that provide
the content we see in our web browsers), to provide applications for consumer
devices (e.g., cell phones, pagers and personal digital assistants) and for many other
purposes.
FORTRAN (FORmula TRANslator) was developed by IBM Corporation in the mid-1950s
to be used for scientific and engineering applications that require complex
mathematical computations. Fortran is still widely used in engineering applications.
COBOL (COmmon Business Oriented Language) was developed in the late 1950s by
computer manufacturers, the U.S. government and industrial computer users. COBOL
is used for commercial applications that require precise and efficient manipulation of
large amounts of data. Much business software is still programmed in COBOL.
The BASIC (Beginners All-purpose Symbolic Instruction Code) programming
language was developed in the mid-1960s at Dartmouth College as a means of
writing simple programs. BASICs primary purpose was to familiarize novices with
programming techniques.
Microsofts latest development tools are part of its corporate-wide strategy for
integrating the Internet and the web into computer applications. This strategy is
implemented in Microsofts .NET platform, which provides the capabilities developers
need to make and run computer applications that can execute on computers
distributed across the Internet. Microsofts three primary programming languages are
Visual Basic (based on the original BASIC), Visual C++ (based on C++) and Visual C# (a
new language based on C++ and Java that was developed expressly for the .NET
platform).
Microsofts .NET platform
Examples for languages:

1111001010
0101010101
1101001010
1101010101
1010101011

load 5
add 4
store a

program add;
begin
var a : integer;
a:=5+4;
end.
Machine
language
Assembly
language
Pascal Language
If you write a program to add the number 5 and the number 4 in
machine, assembly, Pascal and C++ languages typical program
codes will be following:
C++ Language

int main()
{
int a=5+4;
}
ADVANTAGES OF
LOW LEVEL LANGUAGE HIGH LEVEL LANGUAGE
1. It occupies less space in memory.
2. It performs (runs) very fast.
3. Because it is only program that
computer can understand directly,
it doesnt require any compiler
program.
1. It is easier to learn and closer to
human language.
2. It is easier to analyze to be partially
rewritten or updated to conform to
changing conditions.
3. It is simpler to debug to find the
mistakes and problems.
4. It makes enable the programmers
to type programs without having to
know the details of the parts of the
computer.
Typical C++ Development Environment
C++ programs typically go
through six phases:
1. edit,
2. preprocess,
3. compile,
4. link,
5. load and
6. execute.
How to make C++ programs
C++ Compilers

C++ Interpreters

C++ IDEs (Integrated Development Environments)
You can select your C++ programming tool according to your
platform (Type of the computer such as x86, x64, and Itanium
and operating system).
Integrated Development Environments provides comprehensive tools for

typing and editing codes

adding and editing resources

building (compiling and linking)

debugging codes

deploying applications


Intel Software Development Products with C++
Intel Parallel Studio XE 2013 SP1

Intel Composer XE 2013 for Windows and Linux,
formerly Intel Compiler Suite

Intel C++ Composer XE 2013 for Windows, Linux, and
Mac OS X, formerly Intel C++ Compiler
Performance
Source: http://www.microway.com.au/catalog/intel/
Intel

Cilk Plus is tool of Intel C++ and


is a powerful capability for increasing
C++ application performance.
It is a lot more than a Fortran and C++
compiler. It includes three powerful,
function libraries that offer the easiest
way in which to add parallelism-based
performance to your application
software.
The three libraries are:
Intel

Math Kernel Library, also


known as Intel

MKL
Intel

Integrated Performance
Primitives, also known as Intel

IPP
Intel

Threading Building Blocks,


also known as Intel

TBB
Source: http://www.microway.com.au/catalog/intel/
Embarcadero RAD Studio XE5
With RAD Studio's multi-device app development,
you don't have to maintain separate development
projects to deliver your app natively for multiple
devices (PCs, tablets and smartphones) on multiple
platforms (Android, iOS, Windows, and OS X).
Embarcadero C++Builder XE5
For building true native apps for Windows and OS X.
Oracle Solaris Studio 12.3

#1 Development Platform for Oracle Systems with C, C++ and Fortran development tool suite,
delivers the latest in compiler optimizations, multithread performance, and powerful analysis
tools for the best application performance and reliability on Oracle Solaris, Oracle Linux, and
Red Hat Enterprise Linux operating systems.

http://www.oracle.com/technetwork/server-storage/solarisstudio/downloads/index.html
FastReport
InterBase
TeeChart
IntraWeb
Beyond Compare
AQtime
CodeSite
glyFX Icon Library
IP*Works
Tools and Components included in C++Builder

C++Builder XE5 includes sophisticated tools and components from Embarcadero
Technology Partners at no extra cost to make you able to develop applications.
Some of them are listed below:
Microsoft Visual Studio Express 2013 for Windows
Desktop / for Web / for Windows free versions.

You can use Visual Studio Express 2013 to make powerful
desktop apps / innovative web apps and web APIs /
Windows Store apps in C#, Visual Basic, and C++.
Microsoft Visual Studio 2013 Ultimate / Premium / Professional paid versions.
Visual Studio 2013 is an integrated development environment that includes advanced
tools.
http://msdn.microsoft.com/en-us/dn369242 for download
Code::Blocks IDE for C/C++
Code::Blocks is a free, open-source, cross-platform C/C++ IDE
Dev-C++
A portable, free C/C++/C++11 IDE
Eclipse
Eclipse is an IDE to develop Java by default, and has plugins to support: Ada, C,
C++, COBOL, Perl, PHP, Python, R, Ruby, Scala, Clojure, Groovy and Scheme.
NetBeans is an IDE for developing primarily with Java, but also with other
languages, in particular PHP, C/C++, and HTML5. The NetBeans IDE is written
in Java and can run on Windows, OS X, Linux, Solaris and other platforms
supporting a compatible JVM.
Interpreters
Every time a program is run, Interpreter directly execute the source codes (program
instructions) statement by statement (instruction by instruction).
Compilers
Compiler translates the source codes (program instructions) which is written with the
help of a human readable language into the machine readable language which can be
understood by a computer.
Scripts languages work by the same way with the interpreters.
Some programming languages, such as C#, Visual Basic .NET, and Java firstly compile the
source codes into the intermediate language which is so-called common intermediate
language (CIL). Then their components called virtual machine (VM) or runtimes translate
those intermediate codes into the actions like interpreters.
Compilers vs. Interpreters
In the compilation, firstly complier program translates the source code into an
intermediary form and produce a file called an object file with the extension .obj or .o.
Then another program called linker combines one or more object files with one or
more library files and produces an executable file with the extension .exe.
Interpreting is a far faster process to only edit and test codes. But once a source
code is compiled, compiled program runs much faster than interpreted program.

Another advantage of compiler is that you can distribute the executable
program to people who dont have the compiler. You need to deliver the source codes
with the interpreter and each time you want to run the source code you need the
interpreter.
A use of C++ Interpreter
In the Large Hadron Collider (LHC) experiments at CERN
project, The four major LHC experiments have today more
than 100 Petabytes of data in the form of C++ objects
managed by the ROOT software. This software employs
C++ interpreter.

Several GB of libraries
Hundreds of thousands of types / templates
50 MLOC C++ codes

C++ Compilers
GNU Compiler Collection (GCC) http://gcc.gnu.org/

GCC is a compiler system produced by the GNU Project supporting various programming
languages. GNU is a Unix-like computer operating system developed by the GNU
Project. It is composed wholly of free software.
GCC is included in all Unixes. For Windows, you could either install MinGW GCC or Cygwin
GCC.
The GNU C and C++ compiler are gcc and g++, respectively.
C++ Compilers
http://www.compileonline.com/compile_cpp_online.php
Compile and Execute C++ Online (GNU GCC version 4.8.1)
Cygwin = GNU + Cygnus + Windows. Cygwin is a Open-source Linux-like environment for
Windows. It consists of two parts: (1)A DLL (cygwin1.dll) which acts as a Linux API emulation
layer providing substantial Linux API functionality. (2) A collection of tools which provide
Linux look and feel.
Cygwin http://www.cygwin.com/
MinGW (short for "Minimalist GNU for Windows") is a port of the GNU Compiler Collection
(GCC), including C, C++, ADA and Fortran offers a development environment for native
Microsoft Windows applications.
MinGW http://www.mingw.org/
http://www.youtube.com/watch?v=f9Xfh8pv3Fs&list=PLAD733A8B80755D88
C++ Interpreters
Actually C++ is a compiled language, but there are some C++ interpreters.
CINT
CINT is an interpreter for C and C++ code.
http://root.cern.ch/drupal/content/cint
CLING
The C++ interactive interpreter cling is based on LLVM and its C++ frontend clang.
A tutorial for a practical use of CLING:
http://root.cern.ch/drupal/content/cling-brief
Languages like , on the other hand, are procedural, so programming tends to be
action oriented. In C, the unit of programming is the function.
Languages like C++ are object oriented. Programming in such a language is
called object-oriented programming (OOP). In C++, the unit of programming is
the class from which objects are eventually instantiated (an OOP term for
created). C++ classes contain functions that implement operations and data
that implements attributes.
C programmers concentrate on writing functions. Programmers group actions
that perform some common task into functions, and group functions to form
programs.
C vs. C++
First Program in C++: Printing Text
// a C++ program to display text messages on the screen.
#include <iostream> // adds iostream library file
using namespace std; // define the scope of standard namespace

int main() // this is main function
{
cout << "Welcome to C++!"<<endl; // display message
cout << "This is our first C++ program!" << endl;
system("pause");
return 0; // indicate that program ended successfully
} // end function the main function
using namespace std;

int main()
{

}
Basic anatomy of C++ programs
Every C++ program has the same essential format.
First Program in C++: Printing a Line of Text
// a C++ program to display text messages on the screen.
#include <iostream> // adds iostream library file
using namespace std; // define the scope of standard namespace

int main() // this is main function
{
cout << "Welcome to C++!"<<endl; // display message
cout << "This is our first C++ program!" << endl;
system("pause");
return 0; // indicate that program ended successfully
} // end function the main function
Any line begins with // indicates that the remainder of each line is a comment. You
insert comments to document your programs and to help other people read and
understand them. Comments do not cause the computer to perform any action when
the program is runtheyre ignored by the C++ compiler and do not cause any
machine-language object code to be generated.
Block style (C-like style) can span multiple lines.

/* Start of the comment
....
....
End of the comment */

Line comments -- use the double-slash // and only can be wide of a line.

a = 5; // This is an example for the single line comment

Comments
Comments are used for documenting program codes. They are ignored by the compiler
like whitespaces.
First Program in C++: Printing a Line of Text
// a C++ program to display text messages on the screen.
#include <iostream> // adds iostream library file
using namespace std; // define the scope of standard namespace

int main() // this is main function
{
cout << "Welcome to C++!"<<endl; // display message
cout << "This is our first C++ program!" << endl;
system("pause");
return 0; // indicate that program ended successfully
} // end function the main function
# include is a preprocessor directive which is a message to the C++ preprocessor.
Preprocessor runs before the compiler and do some certain task.

Here preprocessor directive tells to add standard input - output stream file which is a
library - a large collection of prewritten functions, data types, and other items that
any C++ programmer may use. For example, to use cin and cout objects, you have to
have #include<iostream> in your program; to use sqrt(x) and pow(x, y) functions, you
have to have #include<cmath> in your program.
iostream called header file.
First Program in C++: Printing a Line of Text
// a C++ program to display text messages on the screen.
#include <iostream> // adds iostream library file
using namespace std; // define the scope of standard namespace

int main() // this is main function
{
cout << "Welcome to C++!"<<endl; // display message
cout << "This is our first C++ program!" << endl;
system("pause");
return 0; // indicate that program ended successfully
} // end function the main function
using namespace std;

Namespaces are a relatively recent addition to C++. A namespace defines and makes
an area in which various program elements can be located. Elements declared in one
namespace are separate from elements declared in another. Namespaces help in the
organization of large programs. The using statement informs the compiler that you
want to use the std namespace. This is the namespace in which the entire Standard
C++ library is declared. By using the std namespace, you simplify access to the
standard library.
First Program in C++: Printing a Line of Text
// a C++ program to display text messages on the screen.
#include <iostream> // adds iostream library file
using namespace std; // define the scope of standard namespace

int main() // this is main function
{
cout << "Welcome to C++!"<<endl; // display message
cout << "This is our first C++ program!" << endl;
system("pause");
return 0; // indicate that program ended successfully
} // end function the main function
Every C++ program must have main( ) function. The opening curly brace { on the line
that follows main( ) indicates the start of the main( ) functions code. The closing curly
brace } identifies end of the main( ) function.
C++ programs typically consist of one or more functions and classes. C++ programs
begin executing at function main, even if main is not the first function in the program.

The keyword int to the left of main indicates that main returns an integer (whole
number) value. A keyword is a word in code that is reserved by C++ for a specific use.
First Program in C++: Printing a Line of Text
// a C++ program to display text messages on the screen.
#include <iostream> // adds iostream library file
using namespace std; // define the scope of standard namespace

int main() // this is main function
{
cout << "Welcome to C++!"<<endl; // display message
cout << "This is our first C++ program!" << endl;
system("pause");
return 0; // indicate that program ended successfully
} // end function the main function
Standard output stream object cout (std::cout), sends the stream of characters
"Welcome to C++!" to the default output device which is here screen. A string is
sometimes called a character string or a string literal. We refer to characters between
double quotation marks simply as strings.
endl (std::endl)a so-called stream manipulator. The name endl is an abbreviation
for end line and belongs to namespace std. The std::endl stream manipulator outputs
a newline, then flushes the output buffer. This simply means that, on some systems
where outputs accumulate in the machine until there are enough to make it worthwhile
to display them on the screen, std::endl forces any accumulated outputs to be displayed
at that moment. This can be important when the outputs are prompting the user for an
action, such as entering data.
cout << "Welcome to C++!"<<endl;
First Program in C++: Printing a Line of Text
// a C++ program to display text messages on the screen.
#include <iostream> // adds iostream library file
using namespace std; // define the scope of standard namespace

int main() // this is main function
{
cout << "Welcome to C++!"<<endl; // display message
cout << "This is our first C++ program!" << endl;
system("pause");
return 0; // indicate that program ended successfully
} // end function the main function
return 0; is one of several means well use to exit a function. When the return
statement is used at the end of main, as shown here, the value 0 indicates that the
program has terminated successfully.

According to the C++ standard, if program execution reaches the end of main without
encountering a return statement, its assumed that the program terminated
successfullyexactly as when the last statement in main is a return statement with the
value 0.
For that reason, to use return 0; is optional, you can omit the return statement
at the end of main in subsequent programs.
cout << "Welcome to C++!"<< endl;
the standard output stream object std::cout
a string literal
the stream insertion operator << operator
the manipulator
is called a statement.
Every C++ statement must end with a semicolon (also known as the statement terminator).
But preprocessor directives (like #include) do not end with a semicolon. The pre-processor
directive ( starts with #) is not a code instruction, it is a directive for the preprocessor,
therefore it assumes the whole line as the directive and does not require a semicolon (;) at the
end of it.
Note: C++ is case sensitive. That means cout, Cout, COUT, coUT all are treated the
different.
statements
Output and input in C++ are accomplished with streams of characters. Thus, when the
preceding statement is executed, it sends the stream of characters Welcome to C++!
to the standard output stream object cout (std::cout) which is connected to the
screen by the default value.
More closer to the cout
cout << "Welcome to C++!"<<endl;
The << operator is referred to as the stream insertion operator. When this program
executes, the value to the operators right, the right operand, is inserted in the output
stream. Notice that the operator points in the direction of where the data goes. The
right operands characters normally print exactly as they appear between the double
quotes.
#include <iostream>
using namespace std;
int main()
{
cout << Hello C++!"<<endl;
cout << Hello class!" << endl;
system("pause");
return 0;
}
Compiler
001010100101010100
0101010010010001010
0100101000100100101
0100100100010010100
100100100101000
linker


Application file

Source file contain source codes.
iostream file is a library file which is
used in source file.
Object code(.obj) which contains machine codes
produced by compiler.
Executable program file (.exe) which is
produced by linker.
Building (Compile & link) application
// iostream standard header
#pragma once
#ifndef _IOSTREAM_
#define _IOSTREAM_
#ifndef RC_INVOKED
.
A token is the smallest unit of a C++ program that has meaning to be translated by the
compiler. C++ has the following type of the tokens: identifiers, keywords, literals,
operators, punctuators (separators), whitespaces.
C++ tokens
Token type Explanation Examples
Keywords
words of C++ programming
language
int, using, return, if, for
Identifiers predefined or user defined names include, main, cout, std
Literals constant values "Hello C++!", 101, -3.5, '?', 'F
Operators operators +, -, /,*,&&, >,<, <<
Punctuation/Separators
symbols defining the structure of a
program
{ } ( ) ; ,
Whitespace
spaces and comments which are
ignored by the compiler
Spaces, tabs, newlines,
comments
Keywords are reserved by the C++ programming language to
implement various features. Keywords must not be used as
identifiers, such as variable names.
Predefined Identifiers
The difference between the two terms reserved word and predefined identifier is that the
predefined identifiers can be used in C++ program to name variables, functions so on, but the
keywords not .

For example, you could declare a variable called main inside your program, and you can use it
in your mathematical expressions. On the other hand, you could not do this with a variable
named int. The difference is that int is a reserved word, while main is "only" a predefined
identifier.
Some of the predefined identifiers are include, iostream, std, main, cout, cin, endl, system,
string, NULL.
I dentifier
In a C++ program identifiers are used to give names to a function, a variable, or any
other user-defined item. Which means you can give any name and programmers
are free to use any words.
Identifiers can be from one to several characters long. The first 1024 characters
will be significant.
Variable names may start with any letter of the alphabet or with an underscore
and dollar sign. Next may be either a letter, a digit, or an underscore. The
underscore can be used to enhance the readability of a variable name, as in
first_name. Dont use space for this purpose.
Uppercase and lowercase are different; that is, to C++, number1 and Number1
can be used as the separate names.
You cannot use any of the C++ keywords as identifier names. Also, you should
not use the name of any standard function, such as abs, for an identifier.
Naming Rules
Which of the following are valid C++ identifiers?
I dentifier
Valid identifiers begin with a letter, then followed by any digit, letter or the
underscore character ( _ ).

birthday
First_Initial
grade
OldName
These are examples for
the valid identifiers
These are examples for
the invalid identifiers
Compiling, Linking and Building C/C++ Applications with gcc
http://www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
http://msdn.microsoft.com/en-us/library/vstudio/cyz1h6zd.aspx
Compiling, Linking and Building C/C++ Applications with VC++
http://msdn.microsoft.com/en-us/library/vstudio/ms164311%28v=vs.100%29.aspx
http://msdn.microsoft.com/en-us/library/ms235639.aspx
WEB sources for video tutorials from
http://www.youtube.com/watch?v=AReo1nXd6i8&list=PL0170B6E7DD6D8810
C Programming Tutorial
http://www.youtube.com/watch?v=tvC1WCdV1XU&list=PLAE85DE8440AA6B83
Buckys C++ Programming Tutorials with CodeBlocks
http://www.youtube.com/watch?v=j79SE36O62w&list=PLA68C1F33757B4A38
C++ Video Tutorials with MS Visual Studio
http://www.youtube.com/watch?v=tyVhn0FWWB4&list=PL1D10C030FDCE7CE0
C++ Tutorials
http://www.youtube.com/user/ThurmanOSUOKC?feature=watch
C++ Tutorials by Barry Thurman
WEB sources:
http://www.cplusplus.com/doc/tutorial/
C++ Language Tutorial
http://www.cprogramming.com/tutorial.html
Programming Tutorials - C, C++, OpenGL, STL
http://www.tutorialspoint.com/cplusplus/cpp_overview.htm
C++ Tutorial
http://www.youtube.com/channel/HCbrnJ0174HZU/videos
Microsoft Visual Studio Tutorials
http://www.embarcadero.com/products/cbuilder/how-to-create-software-applications
C++Builder Videos
http://www.youtube.com/user/EmbarcaderoTechNet/search?query=cbuilder
EmbarcaderoTechNet
"How Do I?" Videos for Visual C++
http://msdn.microsoft.com/en-us/vstudio/bb496952
WEB sources:
Microsoft Virtual Academy
http://www.microsoftvirtualacademy.com/#fbid=-paiMhTa7Zl
MSDN Library
http://msdn.microsoft.com/en-us/library/aa187916.aspx
Visual C++ Resources
http://msdn.microsoft.com/en-us/vstudio/hh386302
http://channel9.msdn.com/Browse/Series
MSDN on Channel 9
http://msdn.microsoft.com/en-US/windows/desktop/bg161546
Windows Dev Center
Visual Studio samples
http://code.msdn.microsoft.com/vstudio/site/search?f[0].Type=ProgrammingLanguage&
f[0].Value=C%2B%2B&f[0].Text=C%2B%2B
Project sources:
http://www.codeplex.com/
CodePlex is Microsoft's free open source project hosting site
Developer code samples
http://code.msdn.microsoft.com/site/search?f[0].Type=ProgrammingLanguage&f[0].Valu
e=C%2B%2B&f[0].Text=C%2B%2B
http://channel9.msdn.com/coding4fun/projects
Coding4Fun projects are ready-to-run, consumer-ready applications designed for real-
life use. In addition to being free, all applications also are also open-source.
http://www.deitel.com/ResourceCenters/Programming/ProgrammingProjects/Program
mingProjects/tabid/770/Default.aspx
From www.deitel.com programming projects
Reference book :
C++ : How to program P.J. Deitel, H.M. Deitel. -- 8th ed.

Das könnte Ihnen auch gefallen