1 / 41

CIS162AB - C++

CIS162AB - C++. Variables Juan Marquez 02_variables.ppt. Overview of Topics. Declaring Variables Data Types I/O Streams Coding Style IPO Charts – Logic Planning Tool. P02 is a program which allows the input of a rate and hours, then uses the input to calculate gross, taxes, and netpay.

Download Presentation

CIS162AB - C++

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. CIS162AB - C++ Variables Juan Marquez 02_variables.ppt

  2. Overview of Topics • Declaring Variables • Data Types • I/O Streams • Coding Style • IPO Charts – Logic Planning Tool

  3. P02 is a program which allows the input of a rate and hours, then uses the input to calculate gross, taxes, and netpay. We enter 9.25 and 25 for a netpay of 161.13. The additional cases used to test the program demonstrate that any two values can be entered, and the program will work. If a procedure or process can be defined, it can be programmed! To allow for any two values, we use variables. The values can vary each time we run it. P02.exe

  4. Memory Allocation Applications

  5. Variables Allocated Memory • When a variable is declared, memory is assigned to it within the memory allocated to the application (P02). • Each memory location (byte) has an address. • Each time the program is ran, a different address may be assigned to the variable. • The computer will use the actual memory address assigned to the variable at runtime. • However, since we as programmers don’t know the address that will be assigned to our variables, we use variable names to reference the location. • Variables are called identifiers because they identify the memory location assigned to them.

  6. Variables in P02 Memory

  7. Memory Addresses • Memory addresses are usually represented in hexadecimal. • Addresses listed in these slides are only examples.

  8. MCC Analogy • MCC is located at 1833 W. Southern. • When you leave home to go to campus, you don’t say “I’m going to 1833 W. Southern.” • Most of you didn’t even know the address until this slide was presented . • We use an identifier such as MCC, Mesa, School, or Work.

  9. Memory Analogy

  10. Variable Initialization • When a variable is declared and a memory location is assigned, we don’t know what is currently stored in that memory location. (We call that “garbage”.) • Variables must be initialized before using them in a calculation or trying to display them. • Variables are initialized by assigning a value to them.

  11. Assigning Values to Variables • Assigning values at declaration is called initialization. • Two ways to initialize, equal sign or parenthesis. int hours; int hours = 0; int hours (0); • cin >> hours; • Assignment statement - Use the equal sign for assignment. gross = hours * rate;

  12. When to Declare Variables • Variables can technically be declared anytime before they are used. • However, in structured programming, we define our variables at the beginning. • Most will be within main( ). • In your assignments I expect all variables to be declared at the top and within main( ), unless instructed otherwise.

  13. Naming Rules • Start with a letter or underscore. int hours; int _hours; • Rest can be letters, digits, or underscores. int hours; int hours1; int hours_1; • C++ is case sensitive. int hours; int Hours; int HOURS; Each of these would have their own memory allocated. • Keywords or reserved words cannot be used.

  14. Preferred Naming Conventions • We’ll be using a standard found in Java and quickly spreading to C++ and VB. • Camel-case: all lowercase with the first letter of other significant words in upper case. hours hoursWorked netHourly empTotalHours reportTotalHours

  15. Constant Variables • Variables are defined as constants if their value will not change during the execution. • Should be in all uppercase with significant words separated with an underscore. double TAX_RATE = .05; • Should use const modifier to prevent change of value. double const TAX_RATE = .05;

  16. Use of Constant Variables • Constant variables are used to • Make a program easier to maintain. • Make a program easier to read. • If we use a federal tax rate of 15% throughout the program it is easier to create a constant variable such as: double const FED_RATE = .15; • Of course meaningful names would need to be used. • Values may also need to be displayed on reports so users will know what values were used to calculate the results.

  17. Program is Easier to Maintain • If the value ever changed, we would need to hunt through the program for .15. • There may be another .15 that we change that wasn’t supposed to be changed. • If we have the value in a constant variable, we only need to change it one place – where the variable is declared.

  18. Program is Easier to Read • When you or another programmer is working on the program it would be easier to read the code if it was stated as:tax = gross * FED_RATE; • If would be difficult to determine what type of tax was begin calculated if it was stated as:tax = gross * .15;

  19. Use Meaningful Names • Use meaningful names for all of the variables. • It makes programs easier to • Read • Maintain • Debug

  20. Tribune, 12/30/1999 Programmers Double-check for Last-minute Y2K Bugs. …Experts said early efforts focused on checking dates – typically identified with a name of “mm_dd_yy” or “date” – buried within computer code. But prankster programmers sometimes used unusual names that can make these data variables nearly impossible to find.

  21. Tribune, 12/30/1999 continued Data Integrity said it found a date variable called “Shirley”… The programmer responsible, it turned out, was dating a woman named Shirley when he wrote the software. Air Force experts compete in a “variable of the week” contest to find the most obscure title for a date field. The name of a girlfriend, athlete, movie stars is unfortunately all too common as programmers express their creative free will.

  22. Use Good Variable Names

  23. Data Types • When creating variables, we need to be aware of the names we use, and we must determine the data type of the values that will be stored in the variable. • Integer • Floating Point • Character • String (class) • Boolean

  24. Integers – whole numbers (+ or -)

  25. Floating Point – decimal values

  26. Character – all characters

  27. Boolean – True or False

  28. I/O Streams • We use variables to store the values entered by the user and to store the results of the processing, so that we display the results. • We interface with the user through Input/Output Streams. • Recall the Information Processing Cycle: Input Process Output • They are called streams because you can think of them as a constant flow of data from the source.

  29. Input Stream cin >> rate >> hours; 9.25 space 25 enter • Can list more than one variable in cin statement, but must be separated by >> • Each value entered is separated by whitespace (space, tab, carriage return). • cin will wait until a value for all variables listed is entered. • If you want zero, a zero must be entered.

  30. Prompts • Since cin will wait until a value is entered, it is important to display a prompt to the user for the required data. • A prompt is a brief description or label for the data to be entered. • The user interface must be friendly. • cout << “Enter the rate and hours separated by a space: “; cin >> rate >> hours;

  31. Keyboard Buffer cin

  32. Output Stream • cout is connected by default to the console. • cout << “P02 Juan Marquez \n\n“; • Output flows to the console character by character. • You need to tell it exactly how to format the information on the screen, including spaces and moving to the next line.

  33. Display Buffer cout cin

  34. Escape Characters • To help with the formatting, special formatting characters need to be used. • \n = newline \t = tab\\ = backslash \” = double quote • cout << “He said \”Yes!\” to me. \n”; • cout << endl; • Rule of thumb -use \n if you have an open quote else use endl (endline – it is an L not the number one).

  35. Echoing Input • The values that were entered by the user should be echoed back to them on the output. • Makes programs user friendly. • Input values may have scrolled off screen. • Allows user to quickly confirmed the result.

  36. Coding Style • Variables names should be meaningful. • Place constant values in variables.Should be capitalized and use const modifier. • IndentingGroup and indent related statements.Place braces on separate lines. • Include comments – ignored by compiler// This is a comment line/* This is a comment line */Can span multiple lines with /* and */ • Must document each major section and complex logic.

  37. IPO Charts • Input, Processing, and Output (IPO) • When designing a program, we usually begin with the end in mind (output). • From the output, we can determine the inputs that will be required to complete the processing.

  38. IPO Chart for P02

  39. P02 Source Code //Get the input values cin >> rate >> hours; //Calculate gross, taxes, netpay, and netHourly gross = rate * hours; fica = gross * FICA_RATE; federal = gross * FEDERAL_RATE; state = gross * STATE_RATE; netpay = gross - (fica + federal + state + UNION_DUES);netHourly = netpay / hours; //Display the results cout << endl << "Hourly Rate: \t" << rate << endl << "Hours Worked:\t" << hours << endl << "Gross Pay: \t" << gross << endl

  40. Using IPO Charts • Consider using IPO Charts when planning the logic for your assignments. • It is much easier to develop a program if you have a plan. It will save you a lot of time. • It is very difficult to design a program on the fly in front of the keyboard. • Even if you find the first couple assignments “easy”, you should spend some time designing the solution so that you will have experience when we get to the more complicated assignments.

  41. Summary • Declaring Variables • Data Types • I/O Streams • Coding Style • IPO Charts – Logic Planning Tool • Next: • Review/Demo P02c_Formatting

More Related