Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a."— Presentation transcript:

1 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

2

3 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

4 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

5 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)

6 float Can be fractional 1.012.712e+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 123.456789 would be stored as 123.456779232163523127123

7 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 +3 -12 +4 -123 or unsigned 2 5 454 34 (no negatives) For Now Use "int" ONLY Be careful when dividing integers!

8 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

9 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 1010 3aprogram executes cin10 3buser types value 2323 Whatever value is typed by the user is placed in age.

10 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.

11 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 107... forgot to output spacing cout << x; cout << y;

12 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 * 2 + 1 6 + 1 7

13 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 = 5.5 + 5 / 2 5.5 + 2 // integer division 5.5 + 2.0// convert to float 7.5// float addition 7// convert to int BE SURE TO REVIEW TEXT EXAMPLES

14 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 1 3 2 4.59.00.060.0 P 1 P 2 t 1 t 2 V = (P 2 - P 1 ) / (t 2 - t 1 ) 9.0 4.5 60.0 0.0 4.5 60.0 0.075 Fig. 2.13, Problem Solving and Design in C, Addison-Wesley, by Jeri R. Hanly, et. al

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

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

17 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

18 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.

19 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


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

Similar presentations


Ads by Google