Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Slides:



Advertisements
Similar presentations
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
Advertisements

CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
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.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Data types and variables
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
Computer Science 101 Introduction to Programming.
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.
CH Programming An introduction to programming concepts.
Input & Output: Console
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer Science 101 Introduction to Programming.
Numeric Types, Expressions, and Output ROBERT REAVES.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Input, Output, and Processing
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
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 
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CSC 107 – Programming For Science. The Week’s Goal.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
CSC 107 – Programming For Science. Announcements.
Chapter 2 Variables.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
TK1913 C++ Programming Basic Elements of C++.
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Data Types and Expressions
Chapter 1: Introduction to computers and C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Programming Funamental slides
Chapter 2 Variables.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Lecture3.
CS150 Introduction to Computer Science 1
Topics Designing a Program Input, Processing, and Output
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Fundamental Programming
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Chapter 2 Variables.
Presentation transcript:

Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a simple c++ program

Extreme -> Minimum Variables #include void main() {const float PerFtEdging = 1.0; float length, width, CostPYard; cin >> length >> width >> CostPYard; cout << 2*(length+width) * PerFtEdging + ( (length*width)/9.0) * CostPYard; } Once again you have a tradeoff: Less Variables More difficult to follow

Extreme -> Maximum Variables #include void main() {const float PerFtEdging = 1.0; float length, width, Perimeter, CostPYard, CostEdging, CostCarpet, SqFeet, SqYards, TotalCost; cin >> length >> width; Perimeter = 2 * (length + width); SqFeet = length * width; SqYards = SqFeet/9; cin >> CostPYard; CostEdging = Perimeter * PerFtEdging; CostCarpet = SqYards * CostPYard; TotalCost = CostEdging + CostCarpet; cout << TotalCost; } More Variables It's somewhat subjective. Avoid UNDERuse of variables! I have avoided comments and labelling for space on the overhead

HOW Do We Tell C++ We Need Variables? In declarations of the following form! type variable-name; examples: float length; float width; or float length, width; NAMES-> begin with letter have letters, digits, underscores avoid c++ names (cout, cin, float) try to use appropriate names TYPES-> floatint(strings) (more later)

float Can be fractional e+612e-1 Are APPROXIMATE when stored in a computer MOST Base 10 numbers CAN NOT be represented in a finite number of Base 2 digits. -> They would require an infinite amount of memory to be exact! Understand scientific notation. Can be very large or small 1e391e-39 Limited PRECISION -> 7 base 10 digits would be stored as

int Whole numbers ONLY Are EXACT, not approximate Limited size : -2 billion < int < +2 billion (roughly) A number of kinds of integers exist in c++. They vary by the range of size of values (amount of memory) whether they are signed or unsigned (no negatives) For Now Use "int" ONLY Be careful when dividing integers!

Strings Strings as we will use them initially are NOT variable types. For example, we can NOT do the following: #include void main() {int first; // this is OK string second; // do not try this yet..... } Strings are used for output whether it is on the output device printer file screen Strings are underlined in this example. Note no variables!.. first = 10; cout >> "The value of first is " >> first >> "\n" Output is The value of first is 10

Inputting Data What role does the input play in your programs? ANSWER: To transfer values from devices such as the keyboard, disk or mouse to the main memory of the machine. In C++ "cin" Consider the following simple program: int age;// step 1 age = 10;// step 2 cin >> age;// step 3 STEPwhat happens value of age 1memory allocated??? 2 age value set to aprogram executes cin10 3buser types value 2323 Whatever value is typed by the user is placed in age.

More cin READING MULTIPLE VALUES: Suppose you need to read 3 values. cin >> a >> b >> c; // separate variables with >> ORcin >> a; cin >> b; cin >> c; Either. In most situations, they function the same. c++ will continue reading until it finds the data you ask for no matter how many lines it takes. Try running cinEx1.C and cinEx2.C from the public account and run both by entering the data on the same line and different lines (four times altogether). You will see they work the same way! 1. Rules for reading char and strings require special consideration. 2. Special cases are best ignored at this point.

Output in c++ Lets you output to the screen any of the following: intfloatstringand others The values which are printed can be either variables, constants, or (in general) expression values. Separate each value (expression) with a << EXAMPLES: cout << "\n"; // says position output on next line cout << ‘\n’; // same effect but really different cout << endl; // does the same thing x = 10; y = 7; // outputs only the value 10 cout << x; // outputs The value of x is 10 and a newline cout << "The value of x is " << x << "\n"; // outputs forgot to output spacing cout << x; cout << y;

Expressions Don't overwhelm yourself with rules. They'll come with practice! Rules: (Fig. 1.11, p.28) 1. () parentheses are evaluated first 2. * / % are equal and evaluated second + - are equal and evaluated last 3. when operations are equal LEFT->RIGHT Terms:X + 12 operand operator operand Division and Mod for integers: Think of grade-school division (w/o remainder) EXAMPLE: (7 / 2) * 2 + (7 % 2) 3 *

When writing expressions, use parentheses as you learn to use the rules! Mixed Mode Expressions Mixing types within a c++ program generally is a problem. Numerical expressions are an exception. Logic dictates that a float plus an int is a float. As you evaluate an expression, remember TYPE at each stage. int m; m = / // integer division // convert to float 7.5// float addition 7// convert to int BE SURE TO REVIEW TEXT EXAMPLES

Expression Example 1 P 2 - P 1 t 2 - t 1 V = The formula for the average velocity, v, of a particle traveling on a line between points p 1 and p 2 in time t 1 and t 2 is The formula can be written and evaluated in c++ as follows: V = (P 2 - P 1 ) / (t 2 - t 1 ) - / - V P 1 P 2 t 1 t 2 V = (P 2 - P 1 ) / (t 2 - t 1 ) Fig. 2.13, Problem Solving and Design in C, Addison-Wesley, by Jeri R. Hanly, et. al

Expression Example 2 Another expression: The formula can be written and evaluated in c++ as follows: z - (a + b / 2) + w * -y DONE * 4 / Fig. 2.13, Problem Solving and Design in C, Addison-Wesley, by Jeri R. Hanly, et. al 8 z a b w y z - (a + b / 2) + w * -y Unary operators first! Assuming all values are integer!

Another expression: The formula can be written and evaluated in c++ as follows: z - (a + b / 2) + w * -y DONE * 4 / Fig. 2.13, Problem Solving and Design in C, Addison-Wesley, by Jeri R. Hanly, et. al 8 z a b w y z - (a + b / 2) + w * -y Unary operators first! Assuming ONLY a is float! Expression Example 3 - MIXED mode

Redirection Your Lab will show you how to do this! PROBLEM: How do you make your program read a file? helps rerun the program without having to type the data over helps to substitute new data sets no changes to program, only how you RUN it! 1. build a data file with an editor (TESTDATA) 2. instead of running the program as a.out use a.out < TESTDATA When you run the program, you WON'T have to type the input data. It will be read from the file instead. NO PROGRAM CHANGE A similar method exists for writing output to a file

Standard Input & Standard Output CIN -> Reads from the standard input COUT -> Writes to the standard output REDIRECTION simply redefines the standard input and/or standard output to be a file What if you wanted to read (write) 3 files at the same time? ANSWER: Redirection won't work. You need another method which we'll learn later.

Program Structure // Any comments // David Game // typically AUTHOR, filename // Assignment 1 // date, assignment, etc. #include directives #include void main ( ) void main () { constant declarations const float x=1.2; variable declarationsint y; executable statements y = x + 3; (cin,cout, assignment)cout << y; } Always read “Good Programming Practices” at the end of each chapter