Fundamental Programming: 310201 1 310201 Fundamental Programming Introduction to C++

Slides:



Advertisements
Similar presentations
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Advertisements

What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Data types and variables
Chapter 2: Introduction to C++.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
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 Data Types, Declarations, and Displays.
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.
Programming Languages
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.
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.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
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.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
1 C++ Syntax and Semantics, and the Program Development Process.
Week 1 Algorithmization and Programming Languages.
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++
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Fundamental Programming Fundamental Programming for Loops.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Fundamental Programming Fundamental Programming More Expressions and Data Types.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Fundamental Programming Fundamental Programming Introduction to Functions.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
310201: Fundamental Programming Fundamental Programming Introduction to Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Lesson 1.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Computing Fundamentals
Basic Elements of C++.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Programming Funamental slides
CS150 Introduction to Computer Science 1
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Fundamental Programming
Presentation transcript:

Fundamental Programming: Fundamental Programming Introduction to C++

Fundamental Programming: Aims Of Course  develop an understanding of what programs do and how they do it  develop logical and procedural thinking  develop familiarity with the C++ language  develop skill in program code development  provide an introduction to programming languages – some fundamental concepts  develop analysis and design skills  provide an introduction to structured design  encourage good s/w engineering practices

Fundamental Programming: Status  last week we started work on:  developing an understanding of what programs do and how they do it  developing logical and procedural thinking  this week we start work on:  developing familiarity with C++ language  developing skill in software development (in lab)  providing an introduction to some fundamental programming concepts

Fundamental Programming: Hello World  it’s traditional to start with the “Hello World!” program  that is, a very small program which simply outputs the message “Hello World!”  a pseudocode design for this program would only have one line: write “Hello World!“

Fundamental Programming: Hello World Program  in C++, the Hello World program is: #include using namespace std; void main (void) { cout << "Hello World!"; }  lets’ take a look at this program  we’ll leave some details to later  there are 6 lines in this program…

Fundamental Programming: Hello World – Line 1  most programs rely on other software  the first line of the program tells the compiler to “include” code, from the “iostream” library, before it is compiled #include  if this code were not included, the compiler would report an error on Line 5: cout << "Hello World!";  it would say: “cout is undefined”

Fundamental Programming: Hello World – Line 1 All elements of standard C++ library are declared within namespace std. So in order to access its functionality we declare with this expression that we will be using these entities.

Fundamental Programming: Hello World – Line 3  a structured program is one that has been broken up into chunks - “functions” in C++  this programs only has one function – the “main” function – Line 3 declares the start of this function void main (void)  every C++ program has a “main“ function  we’ll talk about the “void” bits later…

Fundamental Programming: Hello World – Lines 3 & 5  Lines 4 and 6 go with Line 3 – to help the compiler find the end of a function, curly brackets (or “braces”) are used void main (void) { }  it’s common practice to align the brace that marks the end of a block of code with the brace that marks the start of the block

Fundamental Programming: Hello World – Line 4  Lines 5 is the only “executable” line of code here – it is executed by the computer… { cout << "Hello World!"; }  this line sends “Hello World!” to the display  notice how this line is indented  we use indentation to make code easier to read  it’s not required by the C++ compiler

Fundamental Programming: Hello World – Line 4 cout << "Hello World!";  you can think of:  cout as the display  << as write  “c” in cout as “character” - “character output”  so, cout << "Hello World!"; says: write the string of characters Hello World! to the display

Fundamental Programming: Hello World – Line 4 cout << "Hello World!";  in C++, we use braces to mark the start and end of a block of code  here we use double quotes to mark the start and end of a string of characters  also notice:  you must use double quotes around Hello World!  the line ends with a semi-colon (you get a compilation error if it is missing)  compilers are very fussy about punctuation

Fundamental Programming: Sample Program Revisited write “Number of marks in exam ==> “ read NbrMarks write “Student’s mark ==> “ read StudentMark set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage  in C++, this looks like…

Fundamental Programming: Sample Program In C++ #include using namespace std; void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; cout "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage; }

Fundamental Programming: Declaring Variables  in C++, you must declare variables before you use them  also, you must declare the type of value each variable will hold  the first variable declared is NbrMarks – it can hold any integer value int NbrMarks = 0;  an integer is any +ve or –ve whole number, or zero: …-3, -2, -1, 0, 1, 2, 3, …

Fundamental Programming: Naming Variables  start with a letter - A to Z  can contain upper or lower case letters, or numerals - 0 to 9  good idea to limit names to 32 characters  must avoid C++ reserved words like int, float, void, etc  more later…

Fundamental Programming: Initialising Variables  our declaration of NbrMarks also gives it an initial value - 0 int NbrMarks = 0;  if you do not give a variable an initial value, it will hold whatever data was left in memory by the previous program  incorrect initialisation of variables is a very common logic error made by programmers  please read the previous point again!

Fundamental Programming: More Declarations  our program also declares two floating point variables – StudentMark and Percentage float StudentMark = 0, Percentage = 0;  we use variables of type float to hold values that may not be an integer  examples:  an student’s mark: 15.5 (marks)  a distance: (meters)  a temperature: -2.3 (degrees C)

Fundamental Programming: Why “Floating Point”  some programs need to store very big numbers – eg. 1,234,500,000,000,000,000; others need to store very small numbers  these two numbers can be represented as:  * and *  float variables are stored in two parts:  number part : in above cases  power part : 18 or –12 in above cases  decimal point “floats around”, depending on power part – more details later

Fundamental Programming: More On Declarations  we could declare these variables like this: float StudentMark = 0; float Percentage = 0;  instead, a comma was used to separate two variables of the same type float StudentMark = 0, Percentage = 0;  breaking the declaration over two lines is optional in C++ - it’s “common practice” float StudentMark = 0, Percentage = 0;  notice how a comma is used here to separate two use of punctuation -, and ;  “floating point” because you can store big numbers with several digits on left of decimal point, or small numbers with several digits on right of decimal point – more later

Fundamental Programming: Data Types  selecting the correct data type for a variable is an important issue  if StudentMark were declared as an integer variable, the program would not work correctly if the user entered a mark of 22.5  we’ll look at data types in more detail later  back to our program…

Fundamental Programming: Basic Datatypes No data typevoid Integerint Floating pointfloat Double Precisiondouble Chareaterchar Booleanbool

Fundamental Programming: Modified Type bool char unsigned char signed char int unsigned int signed int short int unsigned short int signed short int long int signed long int unsigned long int float double long double wchar_t

Fundamental Programming: Sample Program In C++ #include using namespace std; void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; cout "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage; }

Fundamental Programming: Output Statements  our program has 4 output statements: cout "; cout << " Student’s percentage: "; cout << Percentage;  in the last statement, the value held in variable Percentage is output to the display  the following statements are very different: cout << Percentage; cout << “Percentage”;

Fundamental Programming: Input Statements  our program has 2 input statements: cin >> NbrMarks; cin >> StudentMark;  you can think of:  cin as the keyboard  >> as read  “c” in cin as “character” - “character input”  so, cin >> NbrMarks; says: read character input from the keyboard and assign the value entered to NbrMarks

Fundamental Programming: Assignment Statements  our program has 1 assignment statement: Percentage = 100 * StudentMark / NbrMarks;  note: values are also assigned to variables by input statements cin >> NbrMarks;  what about if-then-else and while ?  C++ includes an if-else statement - no “then”  a simple example is…

Fundamental Programming: if-else Statements #include using namespace std; void main (void) { float StudentMark = 0; cout "; cin >> StudentMark; if (StudentMark >= 50) { cout << "Pass!"; } else { cout << "Fail!"; }

Fundamental Programming: if-else Statements  notice that comparison between StudentMark and 50 is enclosed in parentheses if (StudentMark >= 50)  also, braces mark start and end of the if- branch (shown below), and the else-branch { cout << "Pass!"; }  indentation makes code easier to read – it’s not required by the compiler

Fundamental Programming: if-else-if Statements  if-else-if statements can be coded as: if (StudentMark >= 75) { cout << “Distinction!"; } else if (StudentMark >= 50) { cout << “Pass!"; } else { cout << "Fail!"; }

Fundamental Programming: Activity – Code in C++ write “Select conversion - (1) C to F, (2) F to C ==> “ read ConversionType write “Input temperature ==> “ read Temperature write “Converts to ” if ConversionType = 1 then write 32 + (Temperature * 1.8) write “ degrees Fahrenheit” else write (Temperature – 32) / 1.8 write “ degrees Centigrade”

Fundamental Programming: A Start #include void main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; }

Fundamental Programming: Activity Break

Fundamental Programming: A Solution #include void main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; cout "; cin >> Temperature; if (ConversionType == 1) { cout << 32 + (Temperature * 1.8); cout << " degrees Fahrenheit"; } else { cout << (Temperature - 32) / 1.8; cout << " degrees Centigrade"; } note: C++ uses == instead of = for testing equality

Fundamental Programming: while Statements  last, but not least, the while statement  C++ includes a while statement  a simple example follows…  some activities for the following example:  see if you can work out what it does  what do you think the following line will do? cout << endl;

Fundamental Programming: while Statements #include void main (void) { int NbrTimesTold = 0, NbrTimesToTell = 0; cout "; cin >> NbrTimesToTell; while (NbrTimesTold < NbrTimesToTell) { cout << “ No new taxes!"; cout << endl; NbrTimesTold = NbrTimesTold + 1; }

Fundamental Programming: Activity Break

Fundamental Programming: Activity Feedback  the program displays “No new taxes!” the number of times requested by the user…  cout << endl; moves to start of next line How many times must I tell you? ==> 3 No new taxes!

Fundamental Programming: while Statements  things to notice:  comparison enclosed in parentheses while (NbrTimesTold < NbrTimesToTell) { cout << endl; cout << " No new taxes!"; NbrTimesTold = NbrTimesTold + 1; }  use of braces and indentation

Fundamental Programming: One More Detail About C++  all compilers are fussy about punctuation  C++ compilers are also fussy about case  the following code has 3 compilation errors… #Include void Main (void) { Cout << "Hello World!"; }

Fundamental Programming: C++ is Case-Sensitive #Include void Main (void) { Cout << "Hello World!"; }  this program has three errors:  it should be #include, not #Include  it should be main, not Main  it should be cout, not Cout  C++ compilers are case-sensitive

Fundamental Programming: C++ Syntax Summary  input : cin >> ;  output : cout ;  assignment : = ;  a selection statement: if ( ) { } else { }  a repetition statement: while ( ) { }

Fundamental Programming: Summary  the iostream library must be included to do input and output – cin and cout  every C++ program has a main function  variables are declared before they are used  the choice of data type is important  variables must be initialised correctly  Syntax Summary shows C++ implementation of Study Guide’s five pseudocode statements  more…

Fundamental Programming: Summary  C++ compilers are fussy about punctuation:  angle-brackets around library names  braces around blocks of code  commas between variables in a declaration  double quotes around strings  semi-colons at end of lines  parentheses around comparisons  C++ compilers are also case sensitive  indentation makes code easier to read