Arithmetic Expressions Function Calls Output

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Numeric Types, Expressions, and Output ROBERT REAVES.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 2: Basic Elements of C++
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Data types and variables
Chapter 2: Basic Elements of C++
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Introduction to C Programming
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
A First Book of ANSI C Fourth Edition
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 3 Arithmetic Expressions, Function Calls, and Output
Simple Data Types Built-In and User Defined Chapter 10.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++ Chapter 1.
Topics Designing a Program Input, Processing, and Output
Chapter 7: Expressions and Assignment Statements
Chapter 2 Introduction to C++ Programming
Completing the Problem-Solving Process
Basic Elements of C++.
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
A First Book of ANSI C Fourth Edition
Chapter 3: Input/Output
Chapter 3 Input output.
CS150 Introduction to Computer Science 1
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Building Java Programs
Presentation transcript:

Arithmetic Expressions Function Calls Output Chapter 3

Coming Up Next Understanding and evaluating expressions. Formatting output Calling functions and using parameters

Precedence Rules Expressions are made up of variables operators parentheses Precedence similar to algebraic precedence Highest is ( ) * / % + - Mult division mod (remainder) Addn subt

Precedence Rules What is the output of this program ? Integer Division

Type Coercion Integer values stored differently in memory than float values Variables declared as float can hold only float values This would “coerce” the program to change 5 to 5.0 and store it in num Happens implicitly

Type Casting Sometimes you wish to explicitly force the program to convert from one type to another Use the name of the type as if it is a function

Mixed Type Expressions When operands are of different data types Results in (implicit) type coercion What are the types of these expressions?

Function Calls & Library Functions A function is called (invoked) by using its name as a statement or command as a value (to be printed, assigned, etc.)

Function Calls and Library Functions All functions have a parameter list The list may be empty, but the parentheses must be included in the call

Functions -- Two Versions Value returning function called as if it is a value needs a return statement Function to do a task a void function called as if it is a statement

Value Returning Functions Function call is used as a value (as in an expression) Do not use as a separate statement The function computes a value (or result) The function returns exactly one result A function written by a programmer should not to tasks (such as I/O)

Library Functions A standard library of often used functions exists See partial listing, pg 66 of text See Appendix C, pg A2 for larger listing

Library Functions What do the following functions do? Value returning or void? exp(x) atof(str) strcpy(dest, source) tolower(ch) isdigit (ch)

Void Functions When declared, type is void A function that does not return a value return statement will be a syntax error It’s purpose is to do a task Called a procedure or subroutine in some languages Invoked or called as a statement not as a value

Formatting the Output Normally cout will just jam values together uses scientific notation for float values We need formatting capabilities include blanks (blank lines, blank spaces) right and left justify output in a specified number of columns specify fieldwidth, precision for float output

Creating Blank Lines cout << “whatever” << endl; Stands for “end Line” Sends a “carriage return” and “line feed” Caused paper roller to advance (rotate) one line Sent the typing head to the left margin

Inserting Blanks Within a Line Used to separate output values Include character space or string of spaces

Manipulators We have used endl as a “manipulator” Behaves like a function causes an action to occur Looks like a data object appears in midst of insertion << operations

Manipulators To right justify output in specified number of spaces To specify number of decimal digits to be output Note required include and cout.setf Note rounding

Style -- Program Formatting Header with /* comments */ about the program Required documentation // comments for clarification of identifiers // comments with descriptions of program sections Use white space, indenting

Testing and Debugging Hints Check precedence in expressions Explicit type casts make program more clear Both sides of assignment statements should be same type Library functions require correct #include statement Functions need correct number and order for parameter lists