410 likes | 558 Views
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 .
E N D
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. 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
Memory Allocation Applications
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.
Memory Addresses • Memory addresses are usually represented in hexadecimal. • Addresses listed in these slides are only examples.
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.
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.
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;
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.
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.
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
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;
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.
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.
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;
Use Meaningful Names • Use meaningful names for all of the variables. • It makes programs easier to • Read • Maintain • Debug
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.
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.
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
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.
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.
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;
Keyboard Buffer cin
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.
Display Buffer cout cin
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).
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.
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.
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.
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
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.
Summary • Declaring Variables • Data Types • I/O Streams • Coding Style • IPO Charts – Logic Planning Tool • Next: • Review/Demo P02c_Formatting