Variables with Memory Diagram

Slides:



Advertisements
Similar presentations
C++ Basics Variables, Identifiers, Assignments, Input/Output.
Advertisements

Dale/Weems/Headington
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
Correction of the Handout #include //Preprocessor using namespace std; int main (){ ………….. return 0; } A namespace is a named group of definitions. When.
Chapter 2 Data Types, Declarations, and Displays
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Chapter 2 Data Types, Declarations, and Displays.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 C++ Syntax and Semantics, and the Program Development Process.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
A Sample Program #include using namespace std; int main(void) { cout
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
Integer VariablestMyn1 Integer Variables It must be possible to store data items in a program, and this facility is provided by variables. A variable is.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Constants, Data Types and Variables
Pointers What is the data type of pointer variables?
Chapter 1.2 Introduction to C++ Programming
LESSON 06.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
Variables Mr. Crone.
Documentation Need to have documentation in all programs
Basic Elements of C++.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Learning Objectives Pointers Pointer in function call
Introduction to C++ October 2, 2017.
Dynamic Memory CSCE 121 J. Michael Moore.
Pointers Psst… over there.
void Pointers Lesson xx
Basic Elements of C++ Chapter 2.
Pointer Data Type and Pointer Variables
Pointers Psst… over there.
Anatomy of a Function Part 2
Chapter 2 Elementary Programming
Basics (Variables, Assignments, I/O)
Pointers, Dynamic Data, and Reference Types
Arrays November 8, 2017.
Anatomy of a Function Part 1
How Functions Work Part 1
CS150 Introduction to Computer Science 1
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
How Functions Work Part 2
Let’s all Repeat Together
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Variables and Constants
Presentation transcript:

Variables with Memory Diagram CSCE 121 J. Michael Moore

Some Definitions Object: region of memory with a type that specifies what kind of information can be stored there and what operations can be performed on it Variable: object with a name Declaration: statement that gives a name to an object Definition: declaration that also sets aside memory for a variable Value: data item put into a variable For these simple variable examples, declaration and definition are the same.

Names / Identifiers For variables, functions, types, … Rules: Start with a letter Only composed of letters, digits and underscores (_) Cannot use keywords (e.g. int, if, while, double)

Variables Identifier is associated with a specific location in memory (i.e. address). Use them in programs as if they were the value. In the background, the compiler sets things up to dereference the variable identifier (i.e. get the value held in the address). The variable type dictates how the bits will be interpreted. (More on types later…)

Declaration, Definition, & Initialization Declare Say what an identifier is and what type of object it refers to. Connects a name to an object. Define Sets aside memory. Initialize Set value held in identifier for the first time. Note zyBook conflates Declaration and Definition, but they are DIFFERENT!

Examples int z; (declaration and definition) extern int z; (declaration) This is rare for variables of base types. (i.e. we won’t do this…) int z = 7; (declaration, definition, and initialization)

Memory Diagram It is not helpful for us to refer to specific addresses. We tend to think symbolically about the data. For example we think about x times x rather than thinking x refers to a memory address and if we get the value held at that address and multiply it by that value… Memory diagrams allow us to think about the variables we are using in a program without having to worry about specific memory addresses. They can also help us do ‘hand execution’ of the code.

Program #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; }

An area for the stack and an area for identifiers… Program Set up memory diagram… An area for the stack and an area for identifiers… #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } identifier stack

Set up area to write output… Program Set up area to write output… #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } identifier stack

We will identify the name of the function (only main for now). output Program We will identify the name of the function (only main for now). #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main identifier stack

Add variable identifiers as we encounter them… output Program Note: We don’t know what value rank holds. It could be any random values for its bits. When we initialize we set it to a known value! Add variable identifiers as we encounter them… #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main rank ? identifier stack

Program main output rank 15 identifier stack Set values when assigned… Note: This is first value assigned so it is initialization. #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main rank 15 identifier stack

Program main output classSize 35 rank 15 identifier stack #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main classSize 35 rank 15 identifier stack

Program main output score1 82.45 classSize 35 rank 15 identifier stack #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main score1 82.45 classSize 35 rank 15 identifier stack

Program main output score2 95.25 score1 82.45 classSize 35 rank 15 #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main score2 95.25 score1 82.45 classSize 35 rank 15 identifier stack

Program main output average 88.85 score2 95.25 score1 82.45 classSize #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main average 88.85 score2 95.25 score1 82.45 classSize 35 rank 15 identifier stack

Program main output grade C average 88.85 score2 95.25 score1 82.45 #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main grade C average 88.85 score2 95.25 score1 82.45 classSize 35 rank 15 identifier stack

When assigned a new value, cross out old value and write in new value. output Program When assigned a new value, cross out old value and write in new value. #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main grade C average 88.85 score2 95.25 score1 82.45 classSize 35 rank 15 7 identifier stack

Program main output grade C B average 88.85 score2 95.25 score1 82.45 #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main grade C B average 88.85 score2 95.25 score1 82.45 classSize 35 rank 15 7 identifier stack

Program main output name Michael grade C B average 88.85 score2 95.25 #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main name Michael grade C B average 88.85 score2 95.25 score1 82.45 classSize 35 rank 15 7 identifier stack

Note: endl means go to a new line output Now start output… Note: endl means go to a new line Program Name: Michael #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main name Michael grade C B average 88.85 score2 95.25 score1 82.45 classSize 35 rank 15 7 identifier stack

Program main output Name: Michael Average: 88.85 name Michael grade C #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main name Michael grade C B average 88.85 score2 95.25 score1 82.45 classSize 35 rank 15 7 identifier stack

Program main output Name: Michael Average: 88.85 Rank: 7 of name #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main name Michael grade C B average 88.85 score2 95.25 score1 82.45 classSize 35 rank 15 7 identifier stack

Program main output Name: Michael Average: 88.85 Rank: 7 of 35 name #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } main name Michael grade C B average 88.85 score2 95.25 score1 82.45 classSize 35 rank 15 7 identifier stack

Program main output Name: Michael Average: 88.85 Rank: 7 of 35 Grade: #include <iostream> #include <string> using namespace std; int main() { int rank = 15; int classSize = 35; double score1 = 82.45; double score2 = 95.25; double average = (score1 + score2) / 2; char grade = 'C'; rank = 7; grade = 'B'; string name = "Michael"; cout << "Name: " << name << endl; cout << "Average: " << average << endl; cout << "Rank: " << rank << " of "; cout << classSize << endl; cout << "Grade: " << grade << endl; } Grade: B main name Michael grade C B average 88.85 score2 95.25 score1 82.45 classSize 35 rank 15 7 identifier stack

A tool Memory diagrams are only a tool. Usually do them on scratch paper. On an exam, make legible. The next slide is an example of what this example might really look like…