Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept.

Slides:



Advertisements
Similar presentations
Introducing C++ Elements. CSCE 1062 Outline Main algorithms’ constructs General form of a C++ program {section 2.5} C++ language elements {section 2.1}
Advertisements

Chapter 2: Basic Elements of C++
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
By Dr. Awad Khalil Computer Science & Engineering Department
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
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.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 6: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept.
Overview of C++ Chapter C++ Language Elements t Comments make a program easier to understand t // Used to signify a comment on a single line.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
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.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Week 1 Algorithmization and Programming Languages.
Overview of C++ Chapter C++ Language Elements t Comments make a program easier to understand t // Used to signify a comment on a single line.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Bill Tucker Austin Community College COSC 1315
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Computing Fundamentals
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2: Introduction to C++.
Chapter 2: Overview of C++
The Fundamentals of C++
Presentation transcript:

Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

2 Lecture Contents: t Programming Environments. Integrated Development Environments (IDE) t Microsoft Visual Studio: C++ IDE t Introduction to project concept –Solution and project(s) t Sample programs in C++

3 Previous lecture reminder Title: A Tutorial Introduction to C++ Source: Friedman/Koffman, Chapter 02 Have a quick look at next approx 40 slides to refresh your knowledge on previous lecture

Overview of C++ Chapter 2

5 2.1 C++ Language Elements t Comments make a program easier to understand t // Used to signify a comment on a single line t /* Text text */ use if comments on multi lines t Don’t embed comments within /* */ comments

6 Compiler Directives t #include –Compiler directive –Processed at compilation time –Instructs compiler on what you want in the program t #include –Adds library files to program –Used with –Also “ “ user defined

7 Compiler Directives t Stream data type –Object that is a stream of characters –Defined in iostream –Entered on the keyboard( cin ) –Displayed on monitor( cout )

8 Declarations t Based on data needs (data identifiers) t Each identifier needed must be declared t Comma used to separate identifiers  cin and cout are standard, undeclared identifiers –Special elements called streams –cin - input stream –cout - output stream –Included with the iostream, not declared

9 Executable Statements  cout get output cout << " \nEnter the fabric size in square meters: " ;  cin get input cin >> sizeInSqmeters; t Assignment sizeInSqyards = metersToYards * sizeInSqmeters;

Reserved Words and Identifiers t Reserved words have special meanings –Can NOT be used for other purposes ( const, float and void are some examples) t Identifiers (variables) –Used to store data by the program (user defined) –Valid identifiers - letter, letter1, _letter –Invalid identifiers - 1letter, const, hell o

11 Reserved Words and Identifiers t Special symbols –C++ has rules for special symbols – = * ; { } ( ) // >

12 Upper and Lower Case t C++ - case sensitive language –Compiler differentiates upper & lower case –Identifiers can be either –Be careful though ( cost != Cost ) t Blank spaces –Use space to make program readable –Use care in placing spaces

Data Types and Declarations t Predefined data types –int (integer numbers) Positive or negative whole numbers INT_MAX - largest int allowed by compiler –float or double (real numbers) Positive or negative decimal numbers

14 Data Types and Declarations t Predefined data types –bool (Boolean) true false –char (Characters) Represent single characters

15 Data Types and Declarations  The basic integer type is int –The size of an int depends on the machine and the compiler On PC’s it is normally 16 or 32 bits t Other integers types –short : typically uses less bits –long : typically uses more bits

16 Data Types and Declarations t Floating-point types represent real numbers –Integer part –Fractional part  The number breaks down into the following parts –108 - integer part – fractional part

17 Data Types and Declarations t C++ provides three floating-point types: –float –double –long double

18 Data Types and Declarations t Predefined data types –char (characters) Individual character value (letter or number) Character literal enclosed in single quotes ‘A’ –bool (true / false) t Ordinal types –intboolchar –Values can be listed

19 Data Types and Declarations  Character type char is related to the integer types t Characters are encoded using a scheme where an integer represents a particular character

20 Data Types and Declarations t ASCII is the dominant encoding scheme –Examples ' ' encoded as 32 '+' encoded as 43 'A' encoded as 65 'Z' encoded as 90 'a' encoded as 97 'z' encoded as 122

21 string Class t String object data type –A literal string constant is a sequence of zero or more characters enclosed in double quotes – "Are you aware?\n" –Individual characters of string are stored in consecutive memory locations –The null character ( '\0' ) is appended to strings so that the compiler knows where in memory string ends

22 string Class t String literal –"A" –"1234" –"Enter the distance:" t Additional data types included in library #include –Various operations on strings

23 Declarations t Identifiers should be –Short enough to be reasonable to type (single word is norm) Standard abbreviations are fine (but only standard abbreviations) –Long enough to be understandable When using multiple word identifiers capitalize the first letter of each word

24 Declarations t Examples –char response; –int minelement; –float score; –float temperature; –int i; –int n; –char c; –float x;

25 Constant Declarations t Types of named constants: –integer –float –char –bool –string objects t Associate meaningful terms –const float PAYRATE = 10.25;

26 Hello.cpp –full text of program // FILE: Hello.cpp // DISPLAYS A USER'S NAME #include using namespace std; int main() { char letter1, letter2; string lastName; // Enter letters and print message. cout << "Enter 2 initials and last name: "; cin >> letter1 >> letter2 >> lastName; cout << "Hello " << letter1 << ". " << letter2 << ". " << lastName << "! "; cout << "We hope you enjoy studying C++." << endl; return 0; }

27 Hello.cpp Program output Enter first 2 initials and last name and press return: EBKoffman Hello E. B. Koffman! We hope you enjoy studying C++.

Executable Statements t Memory status –Before and after t Assignments –Form: result = expression; –sizeInSqyards = metersToYards * sizeInSqMeters; –sum = sum + item;

29 Arithmetic Operators t +Addition t - Subtraction t *Multiplication t /Division t % Modulus

30 Input / Output Operations t Input –#include library –cin >> sizeInSqmeters; t Extracted from cin (input stream)  >> Directs input to variable  cin associated with keyboard input (stdin) t Used with int, float, char, bool and strings

31 Data Types and cin t Don’t mix types with cin int x; cin >> x; Keyboard input 16.6 Value placed in x would be 16

32 Other Characteristics of cin t Leading blanks ignored (floats, int, char, bool and strings) t Char read 1 at a time (1 non blank) t Case issues  int or float will read until space  Strings same as int and float

33 General Form for cin Form: cin >> dataVariable; cin >> age >> firstInitial;

34 Program Output  Output stream cout  << Output operator (insertion operator) –cout << “my height in inches is: “ << height; t Blank lines – endl or “\n” or ‘\n’ Form:cout << dataVariable;

General Form of a C++ Program t General program form –Function basic unit (collection of related statements) –A C++ program must contain a main function void main() {... } –int - function returns integer value –void - function does not return value –main - lower case with () –{ } - Braces define the function body

36 General Form of a C++ Program t General form of function body parts –Declaration statements Variables and constants –Executable statements C++ statements

37 General Form of a C++ Program t General form // File: filename // Program description: #include directives int main() { Declarations section Executable statements section }

38 General Form // Name: Mike Hudock // Date: March 10, 2012 // Files: file1.cpp // Changes : // Program description:

39 General Form t Use comments throughout code to highlight points of interest t Strange identifiers t Function explanations t Algorithm definitions

Arithmetic Expressions  int data type –+ - * /, Assignment, input and output on int –% Only used with int t Examples of integer division 15 / 3 = 5 15 / 2 = 7 0 / 15 = 0 15 / 0 undefined

41 Modulus and Integer t Used only with integer and yields remainder t Examples of integer modulus 7 % 2 = % 100 = % 5 = 4 15 % 0 undefined

42 Mixed-type Assignments t Expression evaluated t Result stored in the variable on the left side t C++ can mix types float a, b, x; int m, n; a = 10; b = 5; x = m / n;

43 Expressions With Multiple Operators t Operator precedence tells how to evaluate expressions t Standard precedence order –() Evaluated first, if nested innermost done first –* / % Evaluated second. If there are several, then evaluate from left-to-right –+ - Evaluate third. If there are several, then evaluate from left-to-right

44 Mathematical Formulas in C++

Common Programming Errors t Syntax –Programs rarely compile –Something always goes wrong –Systematic solutions t Compiler not descriptive –Look at line number before and after error –Watch missing ; and }

46 Common Programming Errors t Run-time errors –Illegal operation (divide by 0) t Logic errors –Program functions differently than you expect

47 IDE MS Visual Studio

48 Introduction to MS Visual C t When you activate IDE for the first time, it’s time for you to make your choice: t General development settings t Or t C++ development settings t Or t …

49 Intro to MS Visual C++ Creating Unmanaged Application To build a console application that will run without.NET framework, follow these steps: 1. Open Visual Studio. 2. On the Start Page, click New Project…. If the Start Page isn’t visible, Choose File, New, Project. 3. Select the Visual C++\Win32 from Installed Project Templates on the left and select Win32 Console Application Project type on the right. 4. Enter project name, for example ConsoleApplication1, project location, for example Q: drive and click OK. 5. Click Finish from the wizard that appears. The wizard generates the skeleton of an unmanaged C++ application. To build the project, follow these steps: 1. Choose Build, Build Solution or press F7 To run the project, follow these steps: 1. Choose Debug, Start Without Debugging or press Ctrl+F5

50

51

52

53

54

55

56

57 Exercise 5.1 Build and run a program: To compute and display the volume of a pool.

58 Exercise 5.4 Build and run a program: To convert Celsius degrees to Fahrenheit degrees Fahr = 9./5.*Cel+32

59 Exercise 5.5 Build and run a program: To convert Fahrenheit degrees to Celsius degrees Cel = 5./9.*(Fahr-32)

60 Exercise 5.3 Build and run a program: To add, subtract, multiply and divide two numeric values.

61 Exercise 5.2 Build and run a program: To convert feet and inches to meters.

62 Before lecture end Lecture: Computer Languages. Programming environments (IDE) More to read: Friedman/Koffman, Chapter 02

63 Thank You For Your Attention!