1 / 124

Variables and Assignments in C++: Overview and Examples

Learn about variables in C++ and how to assign values to them. Understand the syntax for declaring variables, initializing them, and using assignment statements. Explore examples and practice exercises to reinforce your understanding.

bvandyke
Download Presentation

Variables and Assignments in C++: Overview and Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 C++ Basics

  2. Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style

  3. 2.1 Variables and Assignments

  4. Variables and Assignments Display 2.1 • Variables are like small blackboards • We can write a number on them • We can change the number • We can erase the number • C++ variables are names for memory locations • We can write a value in them • We can change the value stored there • We cannot erase the memory location • Some value is always there

  5. Identifiers • Variables names are called identifiers • Choosing variable names • Use meaningful names that represent data to be stored • First character must be • a letter • the underscore character • Remaining characters must be • letters • numbers • underscore character

  6. Keywords • Keywords (also called reserved words) • Are used by the C++ language • Must be used as they are defined in the programming language • Cannot be used as identifiers

  7. Declaring Variables (Part 1) • Before use, variables must be declared • Tells the compiler the type of data to storeExamples: int number_of_bars; double one_weight, total_weight; • int is an abbreviation for integer. • could store 3, 102, 3211, -456, etc. • number_of_bars is of type integer • double represents numbers with a fractional component • could store 1.34, 4.0, -345.6, etc. • one_weight and total_weight are both of type double

  8. At the beginning int main(){ int sum;… sum = score1 + score2; … return 0; } Declaring Variables (Part 2) Two locations for variable declarations • Immediately prior to use int main(){… int sum; sum = score1 + score 2;… return 0; }

  9. Declaring Variables (Part 3) • Declaration syntax: • Type_name Variable_1 , Variable_2, . . . ; • Declaration Examples: • double average, m_score, total_score; • double moon_distance; • int age, num_students; • int cars_waiting;

  10. Assignment Statements • An assignment statement changes the value of a variable • total_weight = one_weight + number_of_bars; • total_weight is set to the sum one_weight + number_of_bars • Assignment statements end with a semi-colon • The single variable to be changed is always on the leftof the assignment operator ‘=‘ • On the right of the assignment operator can be • Constants -- age = 21; • Variables -- my_cost = your_cost; • Expressions -- circumference = diameter * 3.14159;

  11. Assignment Statements and Algebra • The ‘=‘ operator in C++ is not an equal sign • The following statement cannot be true in algebra • number_of_bars = number_of_bars + 3; • In C++ it means the new value of number_of_bars is the previous value of number_of_bars plus 3

  12. Initializing Variables • Declaring a variable does not give it a value • Giving a variable its first value is initializing the variable • Variables are initialized in assignment statements double mpg; // declare the variable mpg = 26.3; // initialize the variable • Declaration and initialization can be combinedusing two methods • Method 1 double mpg = 26.3, area = 0.0 , volume; • Method 2 double mpg(26.3), area(0.0), volume;

  13. Section 2.1 Conclusion • Can you • Declare and initialize two integers variables to zero? The variables are named feet and inches. • Declare and initialize two variables, one int and one double?Both should be initialized to the appropriate form of 5. • Give good variable names for identifiers to store • the speed of an automobile? • an hourly pay rate? • the highest score on an exam?

  14. Self-Test Exercise for 2.1 – Page 49 Demo and Self-Practice Try out Questions 1 to 5 for Practice

  15. Remember? • Remember the Two-Phase in Program Design Process? • Problem-Solving Phase • Results to an Algorithm • First step is to be certain that the task is completely and precisely specified. • Determine the needed Input • Determine the desired Output • Determine the process to turn needed Input into desired Output. • Implementation Phase • Algorithm is translated into a programming language

  16. Problem Solving Phase • Two ways to express an Algorithm • Pseudocode • A English-like expression of the sequence of steps (i.e. Algorithm) necessary to solve a problem or to come up with a solution. • Flowchart • It is a graphical tool to picture out (or depict) the logical sequence of steps necessary to carry task (i.e. the task to solve a problem).

  17. Example problem • Problem : Write an algorithm (or program later on) that initialize two integers and then outputs both their sum and their product. • Determine the desired Output • Display the Sum and Product of two integers • Determine the needed Input • Two integers • Determine the process to turn the needed Input into the desired Output • Sum = first integer + second integer • Product = first integer * second integer

  18. Pseudocode • Declare and initialize variables first_num and second_num • Compute Sum • Sum = first_num + second_num • Compute Product • Product = first_num + second_num • Display Sum • Display Product

  19. Flowchart symbols

  20. Flowchart symbols (Continued)

  21. Flowchart for the problem Start first_num = 10, second_num = 5 Sum = first_num + second_num Product = first_num * second_num Display Sum Display Product End

  22. Implementation Phase #include <iostrem> using namespace std; int main() { int first_num, second_num, sum, product; first_num = 10; second_num = 5; sum = first_num + second_num; product = first_num * second_num; cout << first_num << “ + “ << second_num << “ = “ << sum << endl; cout << first_num << “ * “ << second_num << “ = “ << product << endl; return 0; }

  23. 2.1 Answer Self-Test Questions 2.1

  24. Non-Graded Project for 2.1 • Make a flowchart and then a C++ program that would: • Declare integer variables for an employee’s number of hours worked, rate per hour and total salary • Assign values for number of hours worked and rate per hour • Compute salary using the formula • Salary = number of hours worked * rate per hour • Display Salary to screen

  25. Solution for Non-Grade Project 2.1 - Please take notes while I show the solution.

  26. Graded Project for 2.1 • Make a flowchart and then a C++ program that would compute the penalty for books beyond the return date at COM Library. • Declare double variables for penalty per day and the number of days overdue. • Assign values for penalty per day (e.g. 0.15 per day)and number of days overdue. • Compute penalty using the formula • Total penalty = penalty per day * number of days overdue • Display total penalty to screen • Note: Name your file to GExer2-1-2-Lastname.cpp. For example if your last name is Smith it would be GExer-2-1-1-Smith.cpp

  27. 2.2 Input and Output

  28. Input and Output • A data stream is a sequence of data • Typically in the form of characters or numbers • An input stream is data for the program to use • Typically originates • at the keyboard • at a file • An output stream is the program’s output • Destination is typically • the monitor • a file

  29. Output using cout • cout is an output stream sending data to the monitor • The insertion operator "<<" inserts data into cout • Example: cout << number_of_bars << " candy bars\n"; • This line sends two items to the monitor • The value of number_of_bars • The quoted string of characters " candy bars\n" • Notice the space before the ‘c’ in candy • The ‘\n’ causes a new line to be started following the ‘s’ in bars • A new insertion operator is used for each item of output

  30. Examples Using cout • This produces the same result as the previous sample cout << number_of_bars ; cout << " candy bars\n"; • Here arithmetic is performed in the cout statement cout << "Total cost is $" << (price + tax); • Quoted strings are enclosed in double quotes ("Walter") • Don’t use two single quotes (') • A blank space can also be inserted with cout << " " ;if there are no strings in which a space is desired as in " candy bars\n"

  31. Include Directives • Include Directives add library files to our programs • To make the definitions of the cin and cout available to the program: #include <iostream> • Using Directives include a collection of defined names • To make the names cin and cout available to our program: using namespace std;

  32. Escape Sequences • Escape sequences tell the compiler to treat characters in a special way • '\' is the escape character • To create a newline in output use \n – cout << "\n"; or the newer alternative cout << endl; • Other escape sequences: \t -- a tab \\ -- a backslash character \" -- a quote character

  33. Formatting Real Numbers • Real numbers (type double) produce a variety of outputs double price = 78.5; cout << "The price is $" << price << endl; • The output could be any of these: The price is $78.5 The price is $78.500000 The price is $7.850000e01 • The most unlikely output is: The price is $78.50

  34. Showing Decimal Places • cout includes tools to specify the output of type double • To specify fixed point notation • setf(ios::fixed) • To specify that the decimal point will always be shown • setf(ios::showpoint) • To specify that two decimal places will always be shown • precision(2) • Example: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "The price is " << price << endl;

  35. Input Using cin • cin is an input stream bringing data from the keyboard • The extraction operator (>>) removes data to be used • Example: cout << "Enter the number of bars in a package\n"; cout << " and the weight in ounces of one bar.\n"; cin >> number_of_bars; cin >> one_weight; • This code prompts the user to enter data thenreads two data items from cin • The first value read is stored in number_of_bars • The second value read is stored in one_weight • Data is separated by spaces when entered

  36. Reading Data From cin • Multiple data items are separated by spaces • Data is not read until the enter key is pressed • Allows user to make corrections • Example: cin >> v1 >> v2 >> v3; • Requires three space separated values • User might type 34 45 12 <enter key>

  37. Designing Input and Output • Prompt the user for input that is desired • cout statements provide instructions cout << "Enter your age: "; cin >> age; • Notice the absence of a new line before using cin • Echo the input by displaying what was read • Gives the user a chance to verify datacout << age << " was entered." << endl;

  38. Section 2.2 Conclusion • Can you • write an input statement to place a value in the variable the_number? • Write the output statement to prompt forthe value to store in the_number? • Write an output statement that produces a newline? • Format output of rational numbers to show4 decimal places?

  39. 2.2 Answer Self-Test Questions 2.2

  40. Self-Test Exercise for 2.2 – Page 59 Demo and Self-Practice Answer Questions 8, 10 and 14

  41. Graded Exercise 2.2.1 • Make a flowchart and then a C++ program that would: • Allow a user to enter a number of quarters, dimes and nickels then output the monetary value of the coins in cents. For example, if the user enters 2 for the number of quarters, 3 for the number of dimes and 1 for the number of nickels, then the program should output that the coins are worth 85 cents. • Note: Name your file to GExer-2-2-1-Lastname.cpp. For example if your last name is Smith it would be GExer-2-2-1-Smith.cpp

  42. Program Project for 2.2.2 • Make a C++ program that would: • Improve your program last Program Project that it would display the output in the following format: • Tip : Use Tab Escape Sequence • Note: Name your file to GExer-2-2-2-Lastname.cpp. For example if your last name is Smith it would be GExer-2-2-2-Smith.cpp

  43. 2.3 Data Types and Expressions

  44. Data Types and Expressions • 2 and 2.0 are not the same number • A whole number such as 2 is of type int • A real number such as 2.0 is of type double • Numbers of type int are stored as exact values • Numbers of type double may be stored as approximatevalues due to limitations on number of significant digits that can be represented

  45. Writing Integer constants • Type int does not contain decimal points • Examples: 34 45 1 89

  46. Writing Double Constants • Type double can be written in two ways • Simple form must include a decimal point • Examples: 34.1 23.0034 1.0 89.9 • Floating Point Notation (Scientific Notation) • Examples: 3.41e1 means 34.1 3.67e17 means 367000000000000000.0 5.89e-6 means 0.00000589 • Number left of e does not require a decimal point • Exponent cannot contain a decimal point

  47. Other Number Types Display 2.2 • Various number types have different memoryrequirements • More precision requires more bytes of memory • Very large numbers require more bytes of memory • Very small numbers require more bytes of memory

  48. Integer types • long or long int (often 4 bytes) • Equivalent forms to declare very large integers long big_total; long int big_total; • short or short int (often 2 bytes) • Equivalent forms to declare smaller integers short small_total; short int small_total;

  49. Floating point types • long double (often 10 bytes) • Declares floating point numbers with up to 19 significant digits long double big_number; • float (often 4 bytes) • Declares floating point numbers with up to 7 significant digits float not_so_big_number;

More Related