cse1222: lecture 1the ohio state university1. computing basics computers cpu, memory input/output...

Post on 18-Jan-2018

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

CSE 1222  Operating System: Unix (Linux)  Programming Language: C++  Editor: emacs  Compiler: GNU C++ compiler (g++) CSE1222: Lecture 1The Ohio State University3

TRANSCRIPT

CSE1222: Lecture 1 The Ohio State University 1

Computing Basics Computers

CPU, Memory & Input/Output (IO)

ProgramSequence of instructions for the computer

Operating system (OS)Program which controls all other programs

CompilerProgram to convert programs written in C, C++,

Java, Fortran, etc. into machine language (i.e., 0’s and 1’s)

CSE1222: Lecture 1 The Ohio State University 2

CSE 1222 Operating System: Unix (Linux)

Programming Language: C++

Editor: emacs

Compiler: GNU C++ compiler (g++)

CSE1222: Lecture 1 The Ohio State University 3

First C++ Program - helloworld.cpp// This is a comment. The compiler ignores comments.

// header information// File iostream contains "cout" and "endl"// Namespace std contains "cout" and "endl"#include <iostream>using namespace std;

int main(){ cout << "Hello World!" << endl; cout << "Goodbye World!" << endl;

// Exit program. return 0; }

CSE1222: Lecture 1 The Ohio State University 4

Syntax and Semantics A programming language is similar to a human language, e.g. English,

French, German, etc.

Syntax is the set of rules for forming “grammatically” correct statements

The compiler tells informs you if your program is syntactically correct (or incorrect)So, don’t misspell statements or leave out important symbols

Semantics refers to the meaning associated with syntax

For example, cout is the command to display output to your monitor

One of your jobs in this course is to learn the syntax and semantics of the C++ programming language

You must study C++ syntax and semantics of C++ closelyYou will be expected to write correct C++ programs on paper during the Midterm and Final Exams. So, practice writing programs on paper.

CSE1222: Lecture 1 The Ohio State University 5

Compiling and running helloworld.cpp> g++ helloworld.cpp

> a.outHello World!Goodbye World!

>

CSE1222: Lecture 1 The Ohio State University 6

The main Function

The main() function is where your programs will start executionYou will always need one of these

It tells the other modules in what order to execute.In a way, it “drives” your program

CSE1222: Lecture 1 The Ohio State University 7

int main(){

// program statements here

return 0;}

include #include <iostream> is known a

preprocessor directive

It attaches the file, iostream, at the head of the program before it is compiled

“iostream” is needed to use the cout object

Note that preprocessor directives do not end with a semicolon

CSE1222: Lecture 1 The Ohio State University 8

namespace using namespace std;

Tells the compiler to look in the namespace std (standard namespace) for objects (functions, classes, etc.) – more on this later

cout is in the namespace std

CSE1222: Lecture 1 The Ohio State University 9

Outputting with cout

cout allows us to easily output data to the standard output display (your monitor)

Its name comes from “Console OUTput”.

In cout’s context, << is known as the insertion operator

Any literal (character string) that is to be output must be in between double quotes

The quotes delimit the text so the computer knows it is not an instruction

CSE1222: Lecture 1 The Ohio State University 10

… cout << "Hello World!" << endl; cout << "Goodbye World!" << endl;…

helloworld2.cpp#include <iostream>using namespace std;

int main(){ // statements can be on multiple lines cout << "Hello World!" << endl; cout << "Goodbye World!" << endl; // comments can be here

return 0; // exit program}

CSE1222: Lecture 1 The Ohio State University 11

Program Errors Syntax Errors

You probably misspelled something, forget a symbol(s), or added an erroneous symbol(s)

Also called compiler errors○ The job of the compiler is to deal with the syntactical

correctness of your program The following examples contain syntax errors

Logic and Run-Time Errors

These errors occur after the compiler has accepted your program as syntactically correct

These errors occur during the execution of your program

CSE1222: Lecture 1 The Ohio State University 12

helloworldNoInclude.cpp1. // Example of compiler (syntax) error.2. 3. // Forgot "#include <iostream>"4. using namespace std;5. 6. int main()7. {8. cout << "Hello World!" << endl;9. cout << "Goodbye World!" << endl;10. 11. return 0; // exit program12. }

CSE1222: Lecture 1 The Ohio State University 13

> g++ helloworldNoInclude.cpphelloworldNoInclude.cpp: In function ’int main()’:helloworldNoInclude.cpp:8: ’cout' undeclared (first use this function)helloworldNoInclude.cpp:8: (Each undeclared identifier is reported only once for each function it appears in.)helloworldNoInclude.cpp:8: ‘endl' undeclared (first use this function)

CSE1222: Lecture 1 The Ohio State University 14

1. // Example of compiler(syntax) error.2. 3. // Forgot "#include <iostream>"4. using namespace std;5. 6. int main()7. {8. cout << "Hello World!" << endl;9. cout << "Goodbye World!" << endl;10. 11. return 0; // exit program12. }

helloworldNoNamespace.cpp1. // Example of compiler (syntax) error.2. 3. #include <iostream>4. // Forgot "using namespace std;"5. 6. int main()7. {8. cout << "Hello World!" << endl;9. cout << "Goodbye World!" << endl;10. 11. return 0; // exit program12. }

CSE1222: Lecture 1 The Ohio State University 15

> g++ helloworldNoInclude.cpphelloworldNoInclude.cpp: In function ’int main()’:helloworldNoInclude.cpp:8: ’cout’ undeclared (first use this function)helloworldNoInclude.cpp:8: (Each undeclared identifier is reported only once for each function it appears in.)helloworldNoInclude.cpp:8: ’endl’ undeclared (first use this function)

CSE1222: Lecture 1 The Ohio State University 16

1. // Example of compiler (syntax) error.2. 3. #include <iostream>4. // Forgot "using namespace std;"5. 6. int main()7. {8. cout << "Hello World!" << endl;9. cout << "Goodbye World!" << endl;10. 11. return 0; // exit program12. }

helloworldError1.cpp1. // Example of compiler (syntax) error.2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. cout << Hello World! << endl;9. cout << Goodbye World! << endl;10. 11. return 0; // exit program12. }

CSE1222: Lecture 1 The Ohio State University 17

> g++ helloworldError1.cpphelloworldError1.cpp: In function `int main()’:helloworldError1.cpp:8: ’Hello' undeclared (first use this function)helloworldError1.cpp:8: (Each undeclared identifier is reported only once for each function it appears in.)helloworldError1.cpp:8: parse error before ’!' tokenhelloworldError1.cpp:9: ’Goodbye' undeclared (first use this function)helloworldError1.cpp:9: parse error before ’!' token

CSE1222: Lecture 1 The Ohio State University 18

1. // Example of compiler (syntax) error.2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. cout << Hello World! << endl;9. cout << Goodbye World! << endl;10. 11. return 0; // exit program12. }

helloworldError2.cpp1. // Example of compiler (syntax) error.2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. cout < "Hello World!" < endl;9. cout < "Goodbye World!" < endl;10. 11. return 0; // exit program12. }

CSE1222: Lecture 1 The Ohio State University 19

> g++ helloworldError2.cpphelloworldError2.cpp: In function ’int main()':helloworldError2.cpp:8: no match for ’std::ostream& < const char[13]' operatorhelloworldError2.cpp:8: candidates are: operator<(const char*, const char*) <builtin>helloworldError2.cpp:8: operator<(void*, void*) <builtin>helloworldError2.cpp:9: no match for ’std::ostream& < const char[15]' operatorhelloworldError2.cpp:9: candidates are: operator<(const char*, const char*) <builtin>helloworldError2.cpp:9: operator<(void*, void*) <builtin>

CSE1222: Lecture 1 The Ohio State University 20

1. // Example of compiler (syntax) error.2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. cout < "Hello World!" < endl;9. cout < "Goodbye World!" < endl;10. 11. return 0; // exit program12. }

helloworldError3.cpp1. // Example of compiler (syntax) error.2. 3. #include <iostream>4. using namespace std5. 6. int main()7. {8. cout << "Hello World!" << endl;9. cout << "Goodbye World!" << endl;10. 11. return 0; // exit program12. }

CSE1222: Lecture 1 The Ohio State University 21

> g++ helloworldError3.cpphelloworldError3.cpp:6: parse error before ’int'helloworldError3.cpp:9: syntax error before ’<<' token/usr/local/include/g++-v3/bits/stl_algobase.h: In function ’const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = size_t]':/usr/local/include/g++-v3/bits/stl_algobase.h:643: instantiated from here/usr/local/include/g++-v3/bits/stl_algobase.h:134: ’__b' undeclared (first use this function)/usr/local/include/g++-v3/bits/stl_algobase.h:134: (Each undeclared identifier is reported only once for each function it appears in.)/usr/local/include/g++-v3/bits/stl_algobase.h:134: ’__a' undeclared (first use this function)...

CSE1222: Lecture 1 The Ohio State University 22

…3. #include <iostream>4. using namespace std5. 6. int main()7. {8. cout << "Hello World!" << endl;…

helloworldError4.cpp1. // Example of compiler warning.2. 3. #include <iostream>;4. using namespace std;5. 6. int main()7. {8. cout << "Hello World!" << endl;9. cout << "Goodbye World!" << endl;10. 11. return 0; // exit program12. }

CSE1222: Lecture 1 The Ohio State University 23

> g++ helloworldError4.cpphelloworldError4.cpp:3:20: warning: extra tokens at end of #include directive

> a.outHello World!Goodbye World!

>

CSE1222: Lecture 1 The Ohio State University 24

1. // Example of compiler error.2. 3. #include <iostream>;4. using namespace std;5. 6. int main()7. {8. cout << "Hello World!" << endl;9. cout << "Goodbye World!" << endl;10. 11. return 0; // exit program12. }

helloworldError5.cpp1. // Example of compile error2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. cout << "Hello World!" << endl9. cout << "Goodbye World!" << endl10. 11. return 0; // exit program12. }

CSE1222: Lecture 1 The Ohio State University 25

> g++ helloworldError5.cpphelloworldError5.cpp: In function ’int main()':helloworldError5.cpp:9: parse error before ’<<' token

>

CSE1222: Lecture 1 The Ohio State University 26

1. // Example of compile error2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. cout << "Hello World!" << endl9. cout << "Goodbye World!" << endl10. 11. return 0; // exit program12. }

helloworldError6.cpp1. // Example of compile error2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. cout << 'Hello World!' << endl;9. cout << 'Goodbye World!' << endl;10. 11. return 0; // exit program12. }

CSE1222: Lecture 1 The Ohio State University 27

> g++ helloworldError6.cpphelloworldError6.cpp: In function ’int main()':helloworldError6.cpp:8: character constant too longhelloworldError6.cpp:9: character constant too long

>

CSE1222: Lecture 1 The Ohio State University 28

1. // Example of compile error2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. cout << 'Hello World!' << endl;9. cout << 'Goodbye World!' << endl;10. 11. return 0; // exit program12. }

Program Errors Syntax Errors

You are expected to recognize and remember common Compiler errors You may be asked to identify these on an exam

Logic Errors

Your program made it past the compiler, i.e. is syntactically correct Your program successfully completed execution But, it gave you a wrong answer! If your program executes and outputs “2 + 2 = 5”, then your program

has a logical error

Run-time error

Your program made it past the compiler, i.e. is syntactically correct An error occurred during the execution of your program that made it

prematurely end before it could finish

CSE1222: Lecture 1 The Ohio State University 29

helloworldError7.cpp1. // Example of logical error2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. cout << "Hello World!";9. cout << "Goodbye World!";10. 11. return 0; // exit program12. }

CSE1222: Lecture 1 The Ohio State University 30

> g++ helloworldError7.cpp

> a.outHello World!Goodbye World!>

CSE1222: Lecture 1 The Ohio State University 31

1. // Example of logical error2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. cout << "Hello World!";9. cout << "Goodbye World!";10. 11. return 0; // exit program12. }

helloworld3.cpp/* This is also a comment. The compiler ignores comments. */

/* This is a multiline comment. The compiler ignores comments.*/

#include <iostream>using namespace std;

int main(){ /* These statements use '\n' for newline in place of "<< endl" */ cout << "Hello World!\n"; cout << "Goodbye World!\n";

return 0; /* exit program */}

CSE1222: Lecture 1 The Ohio State University 32

mathExample1.cpp// math example

#include <iostream>#include <cmath> /* File cmath contains math functions:

sqrt, exp, sin, cos, ... */using namespace std; /* cout, endl, sqrt, exp are in

the namespace std */

int main(){ cout << "1+2+3+4+5+6 = " << 1+2+3+4+5+6 << endl; cout << "The average of 1,2,3,4,5,6 is “ <<

(1.0+2.0+3.0+4.0+5.0+6.0)/6.0 << endl; cout << "The reciprocal of 1+2+3+4+5+6 is “ <<

1.0/(1.0+2.0+3.0+4.0+5.0+6.0) << endl; cout << "The square root of 1+2+3+4+5+6 is “ << sqrt(1.0+2.0+3.0+4.0+5.0+6.0) << endl; cout << "e^(1+2+3+4+5+6) = " << exp(1.0+2.0+3.0+4.0+5.0+6.0) <<

endl;

return 0; // exit program}

CSE1222: Lecture 1 The Ohio State University 33

Compiling and running mathExample1.cpp> g++ mathExample1.cpp

> a.out1+2+3+4+5+6 = 21The average of 1,2,3,4,5,6 is 3.5The reciprocal of 1+2+3+4+5+6 is 0.047619The square root of 1+2+3+4+5+6 is 4.58258e^(1+2+3+4+5+6) = 1.31882e+09

>

CSE1222: Lecture 1 The Ohio State University 34

mathExample1.cpp (2)

Multiple objects can be inserted into cout.

Objects that should not be taken literally should not be enclosed by double quotes. Here, we actually want to compute the expression 1+2+3+4+5+6.

CSE1222: Lecture 1 The Ohio State University 35

… cout << "1+2+3+4+5+6 = " << 1+2+3+4+5+6 << endl;

mathError1.cpp// math error

#include <iostream>#include <cmath>using namespace std;

int main(){ // These statements are incorrect cout << "The average of 1,2,3,4,5,6 = “ << (1+2+3+4+5+6)/6 <<

endl; // WRONG! cout << "The reciprocal of 1+2+3+4+5+6 is “ << 1/(1+2+3+4+5+6)

<< endl; // WRONG!

return 0; // exit program}

CSE1222: Lecture 1 The Ohio State University 36

> g++ mathError1.cpp

> mathError1The average of 1,2,3,4,5,6 = 3The reciprocal of 1+2+3+4+5+6 is 0

>

CSE1222: Lecture 1 The Ohio State University 37

…int main(){ // These statements are incorrect cout << "The average of 1,2,3,4,5,6 = “ << (1+2+3+4+5+6)/6 <<

endl; // WRONG! cout << "The reciprocal of 1+2+3+4+5+6 is “ << 1/(1+2+3+4+5+6)

<< endl; // WRONG!

return 0; // exit program}

C++ Program Template (for now)#include <iostream>using namespace std;

int main(){ // program statements here

return 0; // exit program}

CSE1222: Lecture 1 The Ohio State University 38

C++ Program Template (with Math)#include <iostream>#include <cmath>using namespace std;

int main(){ // program statements here

return 0; // exit program}

CSE1222: Lecture 1 The Ohio State University 39

Comments and Programming Style Good programming style

The readability of your program by other C++ programmers is very important

This is required in your assignments

Pay attention to: Indent when appropriate. You will develop a feel for this as you

see more programs.

Place comments to help explain your code. Use them to describe what the program does, to put your name on the program, to describe a function, etc.

// ... is for single line comments/* ... */ are for multi-line comments

Comments are treated as white-space, and are unseen by the compiler

CSE1222: Lecture 1 The Ohio State University 40

Textbook Readings Now go home and start your assigned

reading for Chapters 1 and 2 in your text

CSE1222: Lecture 1 The Ohio State University 41

top related