CSE1222: Lecture 1The Ohio State University1. Computing Basics  Computers CPU, Memory & Input/Output (IO)  Program Sequence of instructions for the.

Slides:



Advertisements
Similar presentations
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
Advertisements

CSE202: Lecture 1The Ohio State University1 Introduction to C++
Objectives You should be able to describe: Introduction to Programming
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter 2: Introduction to C++.
Introduction to C++ Programming
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
Hello World 2 What does all that mean?.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Creating your first C++ program
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Basic Program Construction
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 2 part #1 C++ Program Structure
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Bill Tucker Austin Community College COSC 1315
C++ First Steps.
Topic Pre-processor cout To output a message.
Chapter 2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Lecture 1B Introduction Richard Gesick
Completing the Problem-Solving Process
Chapter 2 part #1 C++ Program Structure
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Hello World 2 What does all that mean?.
Chapter 2 – Getting Started
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Creating your first C program
Chapter 3: Input/Output
Programming Fundamentals Lecture #3 Overview of Computer Programming
Chapter 2: Introduction to C++.
Subject:Object oriented programming
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

CSE1222: Lecture 1The Ohio State University1

Computing Basics  Computers CPU, Memory & Input/Output (IO)  Program Sequence of instructions for the computer  Operating system (OS) Program which controls all other programs  Compiler Program to convert programs written in C, C++, Java, Fortran, etc. into machine language (i.e., 0’s and 1’s) CSE1222: Lecture 1The Ohio State University2

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

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 using namespace std; int main() { cout << "Hello World!" << endl; cout << "Goodbye World!" << endl; // Exit program. return 0; } CSE1222: Lecture 1The Ohio State University4

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++ closely You will be expected to write correct C++ programs on paper during the Midterm and Final Exams. So, practice writing programs on paper. CSE1222: Lecture 1The Ohio State University5

Compiling and running helloworld.cpp > g++ helloworld.cpp > a.out Hello World! Goodbye World! > CSE1222: Lecture 1The Ohio State University6

The main Function  The main() function is where your programs will start execution You 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 1The Ohio State University7 int main() { // program statements here return 0; }

include  #include 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 1The Ohio State University8

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 1The Ohio State University9

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 1The Ohio State University10 … cout << "Hello World!" << endl; cout << "Goodbye World!" << endl; …

helloworld2.cpp #include 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 1The Ohio State University11

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 1The Ohio State University12

helloworldNoInclude.cpp 1. // Example of compiler (syntax) error // Forgot "#include " 4. using namespace std; int main() 7. { 8. cout << "Hello World!" << endl; 9. cout << "Goodbye World!" << endl; return 0; // exit program 12. } CSE1222: Lecture 1The Ohio State University13

> g++ helloworldNoInclude.cpp helloworldNoInclude.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 1The Ohio State University14 1.// Example of compiler(syntax) error // Forgot "#include " 4.using namespace std; 5. 6.int main() 7.{ 8. cout << "Hello World!" << endl; 9. cout << "Goodbye World!" << endl; return 0; // exit program 12.}

helloworldNoNamespace.cpp 1. // Example of compiler (syntax) error #include 4. // Forgot "using namespace std;" int main() 7. { 8. cout << "Hello World!" << endl; 9. cout << "Goodbye World!" << endl; return 0; // exit program 12. } CSE1222: Lecture 1The Ohio State University15

> g++ helloworldNoInclude.cpp helloworldNoInclude.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 1The Ohio State University16 1.// Example of compiler (syntax) error #include 4.// Forgot "using namespace std;" 5. 6.int main() 7.{ 8. cout << "Hello World!" << endl; 9. cout << "Goodbye World!" << endl; return 0; // exit program 12.}

helloworldError1.cpp 1. // Example of compiler (syntax) error #include 4. using namespace std; int main() 7. { 8. cout << Hello World! << endl; 9. cout << Goodbye World! << endl; return 0; // exit program 12. } CSE1222: Lecture 1The Ohio State University17

> g++ helloworldError1.cpp helloworldError1.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 ’!' token helloworldError1.cpp:9: ’Goodbye' undeclared (first use this function) helloworldError1.cpp:9: parse error before ’!' token CSE1222: Lecture 1The Ohio State University18 1.// Example of compiler (syntax) error #include 4.using namespace std; 5. 6.int main() 7.{ 8. cout << Hello World! << endl; 9. cout << Goodbye World! << endl; return 0; // exit program 12.}

helloworldError2.cpp 1. // Example of compiler (syntax) error #include 4. using namespace std; int main() 7. { 8. cout < "Hello World!" < endl; 9. cout < "Goodbye World!" < endl; return 0; // exit program 12. } CSE1222: Lecture 1The Ohio State University19

> g++ helloworldError2.cpp helloworldError2.cpp: In function ’int main()': helloworldError2.cpp:8: no match for ’std::ostream& < const char[13]' operator helloworldError2.cpp:8: candidates are: operator helloworldError2.cpp:8: operator helloworldError2.cpp:9: no match for ’std::ostream& < const char[15]' operator helloworldError2.cpp:9: candidates are: operator helloworldError2.cpp:9: operator CSE1222: Lecture 1The Ohio State University20 1.// Example of compiler (syntax) error #include 4.using namespace std; 5. 6.int main() 7.{ 8. cout < "Hello World!" < endl; 9. cout < "Goodbye World!" < endl; return 0; // exit program 12.}

helloworldError3.cpp 1. // Example of compiler (syntax) error #include 4. using namespace std int main() 7. { 8. cout << "Hello World!" << endl; 9. cout << "Goodbye World!" << endl; return 0; // exit program 12. } CSE1222: Lecture 1The Ohio State University21

> g++ helloworldError3.cpp helloworldError3.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 1The Ohio State University22 … 3.#include 4.using namespace std 5. 6.int main() 7.{ 8. cout << "Hello World!" << endl; …

helloworldError4.cpp 1. // Example of compiler warning #include ; 4. using namespace std; int main() 7. { 8. cout << "Hello World!" << endl; 9. cout << "Goodbye World!" << endl; return 0; // exit program 12. } CSE1222: Lecture 1The Ohio State University23

> g++ helloworldError4.cpp helloworldError4.cpp:3:20: warning: extra tokens at end of #include directive > a.out Hello World! Goodbye World! > CSE1222: Lecture 1The Ohio State University24 1.// Example of compiler error #include ; 4.using namespace std; 5. 6.int main() 7.{ 8. cout << "Hello World!" << endl; 9. cout << "Goodbye World!" << endl; return 0; // exit program 12.}

helloworldError5.cpp 1. // Example of compile error #include 4. using namespace std; int main() 7. { 8. cout << "Hello World!" << endl 9. cout << "Goodbye World!" << endl return 0; // exit program 12. } CSE1222: Lecture 1The Ohio State University25

> g++ helloworldError5.cpp helloworldError5.cpp: In function ’int main()': helloworldError5.cpp:9: parse error before ’<<' token > CSE1222: Lecture 1The Ohio State University26 1.// Example of compile error 2. 3.#include 4.using namespace std; 5. 6.int main() 7.{ 8. cout << "Hello World!" << endl 9. cout << "Goodbye World!" << endl return 0; // exit program 12.}

helloworldError6.cpp 1. // Example of compile error #include 4. using namespace std; int main() 7. { 8. cout << 'Hello World!' << endl; 9. cout << 'Goodbye World!' << endl; return 0; // exit program 12. } CSE1222: Lecture 1The Ohio State University27

> g++ helloworldError6.cpp helloworldError6.cpp: In function ’int main()': helloworldError6.cpp:8: character constant too long helloworldError6.cpp:9: character constant too long > CSE1222: Lecture 1The Ohio State University28 1.// Example of compile error 2. 3.#include 4.using namespace std; 5. 6.int main() 7.{ 8. cout << 'Hello World!' << endl; 9. cout << 'Goodbye World!' << endl; return 0; // exit program 12.}

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 1The Ohio State University29

helloworldError7.cpp 1. // Example of logical error #include 4. using namespace std; int main() 7. { 8. cout << "Hello World!"; 9. cout << "Goodbye World!"; return 0; // exit program 12. } CSE1222: Lecture 1The Ohio State University30

> g++ helloworldError7.cpp > a.out Hello World!Goodbye World! > CSE1222: Lecture 1The Ohio State University31 1. // Example of logical error 2. 3.#include 4.using namespace std; 5. 6.int main() 7.{ 8. cout << "Hello World!"; 9. cout << "Goodbye World!"; return 0; // exit program 12.}

helloworld3.cpp /* This is also a comment. The compiler ignores comments. */ /* This is a multiline comment. The compiler ignores comments. */ #include 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 1The Ohio State University32

mathExample1.cpp // math example #include #include /* File cmath contains math functions: sqrt, exp, sin, cos,... */ using namespace std;/* cout, endl, sqrt, exp are in the namespace std */ int main() { cout << " = " << << endl; cout << "The average of 1,2,3,4,5,6 is “ << ( )/6.0 << endl; cout << "The reciprocal of is “ << 1.0/( ) << endl; cout << "The square root of is “ << sqrt( ) << endl; cout << "e^( ) = " << exp( ) << endl; return 0; // exit program } CSE1222: Lecture 1The Ohio State University33

Compiling and running mathExample1.cpp > g++ mathExample1.cpp > a.out = 21 The average of 1,2,3,4,5,6 is 3.5 The reciprocal of is The square root of is e^( ) = e+09 > CSE1222: Lecture 1The Ohio State University34

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 CSE1222: Lecture 1The Ohio State University35 … cout << " = " << << endl; …

mathError1.cpp // math error #include using namespace std; int main() { // These statements are incorrect cout << "The average of 1,2,3,4,5,6 = “ << ( )/6 << endl; // WRONG! cout << "The reciprocal of is “ << 1/( ) << endl; // WRONG! return 0; // exit program } CSE1222: Lecture 1The Ohio State University36

> g++ mathError1.cpp > mathError1 The average of 1,2,3,4,5,6 = 3 The reciprocal of is 0 > CSE1222: Lecture 1The Ohio State University37 … int main() { // These statements are incorrect cout << "The average of 1,2,3,4,5,6 = “ << ( )/6 << endl; // WRONG! cout << "The reciprocal of is “ << 1/( ) << endl; // WRONG! return 0; // exit program }

C++ Program Template (for now) #include using namespace std; int main() { // program statements here return 0; // exit program } CSE1222: Lecture 1The Ohio State University38

C++ Program Template (with Math) #include using namespace std; int main() { // program statements here return 0; // exit program } CSE1222: Lecture 1The Ohio State University39

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 1The Ohio State University40

Textbook Readings  Now go home and start your assigned reading for Chapters 1 and 2 in your text CSE1222: Lecture 1The Ohio State University41