Você está na página 1de 40

MCIS 6204 - Data Structure and

Algorithms (C++ Labs)


Day 1
Dr. Jongyeop Kim

MCIS 2016

Overview
Syllabus
Environment

Setup
- Eclipse and MinGW
Structure of a Program
Fundamental Data Types
Declaration of Variables
Initialization of Variables

Southern Arkansas University

Development Environment Java JDK


Java JDK download
http://
www.oracle.com/technetwork/java/javase/downloads/index.html

Southern Arkansas University

Development Environment Java JDK


32 bit or 64 bit machines

Southern Arkansas University

Development Environment Java JDK


Java JDK download
- Accept License Agreement
- Choose your operating system

Southern Arkansas University

Development Environment Java JDK


Make sure your path to be installed :

Southern Arkansas University

Development Environment Java JDK


Class path
- Control panel > System and Security > System

Southern Arkansas University

Development Environment Java JDK


Class path
- Control panel > System and Security > System

Southern Arkansas University

Development Environment Java JDK


Class path
- Control panel > System and Security > System
System Variables > edit

Southern Arkansas University

Development Environment Java JDK


Class path
- Control panel > System and Security > System
System Variables > edit

10

Southern Arkansas University

Development Environment Java JDK


Verity your java Version
java -version

11

Southern Arkansas University

Development Environment MinGW


Installing MinGW
- Visit web site and click downloads
http://
www.mingw.org/wiki/howto_install_the_mingw_gcc_compiler_su
ite

12

Southern Arkansas University

Development Environment MinGW


Installing MinGW
- Select : mingw-deloper-toolkit
mingw32-base
mingw32-gcc-g++

13

Southern Arkansas University

Development Environment MinGW


Installing MinGW
- Apply the changes

14

Southern Arkansas University

Development Environment MinGW


Class path
- Control panel > System and Security > System
System Variables > edit

15

Southern Arkansas University

Test your program


Compile and run first program
Hellow.cpp

//My First Program in C++


#include <iostream>
using namespace std;
int main ( )
{
cout << "Welcome to SAU";
return 0;
}
C:\Users\kjyos>g++ Hello.cpp
C:\Users\kjyos>a
Welcome to SAU
16

Southern Arkansas University

Development Environment Eclipse IDE for


C/C++ Developers
Eclipse mars 2 packages
Mars Package/ 64 bit
http://
www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/k
eplersr2

17

Southern Arkansas University

C++ project with Eclipse


run Eclipse
- New -> C++ Project

18

Southern Arkansas University

C++ project with Eclipse


Project name : welcomeSAU

19

Southern Arkansas University

C++ project with Eclipse


Build & Run

20

Southern Arkansas University

Structure of a program
//My First Program in C++

// This is a comment line

#include <iostream>

Lines beginning with a hash sign(#) are


directives for the preprocessor.

using namespace std;

All the elements of the standard C++


Library are declared within what is
called namespace.

int main ( )

The beginning of the definition of the


main function.

{
C++ Line statement.
cout << Welcome to SAU;
return 0;

The return statement caused the main


function to finish.

}
21

Southern Arkansas University

Comments
Comments

are parts of the source code disregarded by the

compiler.
Their purpose is only to allow the programmer to insert notes
or descriptions embedded within the source code.
// Line comment
Discard everything from where the pair of slash signs.
/* Block Comment */
Discards everything between the /* characters and the first
appearance of the */ characters.
22

Southern Arkansas University

#include <iostream>
Beginning

with a hash (#) are not regular code lines.


It indicates for the compilers preprocessor.
include <iostream> tells the preprocessor to include the
iostream standard file.

using name space std;


When

programs get very large and complex, and make heavy


use of libraries from a variety of sources, the possibility of
name.
Identifiers can be grouped into separate sets, each set associate
with a particular library of body of code.
The collision can be resolved by qualifying the names with the
name space.
23

Southern Arkansas University

int main( )
The

main function is the point by where all C++ Programs


start their execution, independently of its location within the
source code.
It does not matter whether there are other functions with other
names defined before or after it.
The word main is followed in the code by a pair of
parentheses( )

return 0
The

return statement causes the main function to finish. Return


may be followed by a return code 0.
A return code 0 for the main function is generally interpreted as
the program worked as expected without any errors.
24

Southern Arkansas University

#cout << hello world


Beginning

with a hash (#) are not regular code lines.


It indicates for the compilers preprocessor.
include <iostream> tells the preprocessor to include the
iostream standard file.

25

Southern Arkansas University

Fundamental data types


Store

variables in our computers memory, but the computer


has to know what kind of data we want to store them.
The memory in our computer is organized in bytes.

26

Southern Arkansas University

Declaration of variables
In

order to use a variable in C++, we must declare it


specifying which data type we want it to be.
int a; // declares a variable of type int with the
// identifier a
int b; int x; int iA;
float myNumber; //declares a variable of type float
//with identifier myNumber;
Declare more than one variable of the same type
int a, b, c;

27

Southern Arkansas University

Examples
#include <iostream>
using namespace std;
int main( )
{
int a, b;
//Declaring variables
int results;
a = 5;
//Process
b = 2;
result = a b;
cout << result;

// print out the result

// terminate the program


return 0
}
28

Southern Arkansas University

Scope of Variables
Global

variable - A variable declared in the main body of the


source code, outside all functions.
Local variable Declared within the body of a function or a
block.

29

Southern Arkansas University

Initialization of variable
When

declaring a regular local variable, its value is by default


undetermined. But you may want to store concrete value at the
same moment
int a =5;
//initial value = 5
int b(2);
//initial value = 2
int result; //initial value Undetermined

30

Southern Arkansas University

Introduction to Strings
Variables

that can store non-numerical values that longer than one single
character are known as strings.

// my first string
#include <iostream>
#include <string>
using namespace std;
int main ( )
{
string myString = This is a string;
cout << myString;
return 0;
}
31

Southern Arkansas University

Constants -1
Constants

are expression with a fixed value.

Literals

- Literals are used to express particular values within the source


code of a program.
a = 5; //the 5 is literal constant.
Literal constants can be divided in
Inter Numerals, Floating-point Numerals, Characters,
String and Boolean values.

32

Southern Arkansas University

Constants -2
Integer

Numerals
1776 , 707, -273
75 //Decimal
o113 //Octal
0x4b //Hexadecimal
Floating point Numbers
- They express numbers with decimals and /or exponents.
3.14159 // 3.14159
6.02e23 // 6.02 * 10 ^ 23
1.6e-19 // 1.6 * 10^-19
3.0
// 3.0
Character and String literals
`z` `p`
Hello World
33

Southern Arkansas University

Constants -3
Character

and String literals

`z` `p`
Hello World
Boolean literals
true and false

34

Southern Arkansas University

Escape code
Special

characters that are difficult or impossible to express


otherwise in the source code of a program.
All of them are preceded by a backslash ( \ )

35

Southern Arkansas University

Define Constants (#define)


Simply

use the #define preprocessor directive


#include <iostream>
using namespace std;
#define PI 3.14159
#define NewLine \n
int main ( )
{
double r = 5.0; //Radius
double circle;
circle = 2 * PI * r;
cout << circle;
cout << NewLine;
return 0;
}

36

Southern Arkansas University

Operators
Assignment

: Assigns a value to a variable


#include <iostream>
using namespace std;
int main( )
{
int a, b ;
// a: ? b: ?
a = 10;
// a: 10, b: ?
b = 4;
// a: 10, b : 4
a=b;
// a:4 , b:4
b = 7;
// a:4 , b:7
cout << a:;
cout << a;
cout << b:;
cout << b;
return 0;
}
37

Southern Arkansas University

Arithmetic Operators ( +, -, * , % )
The

+
*
/
%

38

five arithmetical operations by the C++


Addition
Subtraction
Multiplication
Division
Modulo

Southern Arkansas University

Compound assignment

+= , -=, /=, *=

Expression

Is equivalent to

Value += increase;

Value = Value + increase;

A -= 5

A= A 5 ;

A /= B;

A= A / B;

Price *= units 1

Price = price * ( units + 1 )

int main ( )
{
int a , b = 3;
a = b;
a+=2; // a = a + 2;
cout << a;
return 0;
}
39

Southern Arkansas University

Labs
Lab1A

- Basic Debugging
Lab1B - Add functionality

40

Southern Arkansas University

Você também pode gostar