Intro To Programming in C++ (CS 1A Quick Review)
Intro To Programming in C++ (CS 1A Quick Review)
2-1
Computer Programming
3-2
Named Constant – the symbolic name for a location in memory
referenced by an identifier that contains a data value that does NOT change
during program execution. Named constants must be declared.
When we run programs we need to store values that will be used by the
program. When variables and named constants are declared we are really
requesting memory locations within the computer. We need to specify two
things:
- how much memory,
- and how the contents of these memory locations are to be viewed. We
do this with a data type.
Data Type – a set of valid data values along with the operations that may
be performed on those values. The C++ data types we will use in this class
are as follows:
2-3
int - positive or negative integers with or without a sign (commas are not
allowed)
23 -5 6 -100 0 etc.
3-4
Declarations of Variables and Named Constants
Memory is allocated for named constants and values are placed into the
locations at compilation time. The value of a named constant cannot be
changed during program execution. This means that the identifier for a
named constant may NOT appear on the left side of an assignment
operator or in a cin statement.
Variable Declarations:
int ageOne;
int ageTwo;
float averageAge;
char answer;
char userName[20];
NOTE: The variable answer does not use square brackets ( [ ] ) in its
declaration. This variable will hold one single character such as 'Y' or 'N'
where variable userName may contain up to 20 characters. Values such as
"Sam Spade" or "Sally Smith" may be stored in variable userName.
Begin variable names with a lower case letter and capitalize the first letter of
each new word contained in the identifier. Remember, no blank spaces.
2-5
Variable declarations should contain a data table. This table contains
comments that describe what the variable represents in the program and how
its value was obtained.
Memory is allocated for variables at compilation time and values are placed
into the locations at run time. This is accomplished using an assignment
statement or reading from the keyboard (cin).
3-6
Assignment "Statement"
variable = expression;
ageOne = 15;
ageTwo = 23;
averageAge = (ageOne + ageTwo) / 2.0;
answer = 'y';
cin Examples:
Note: While only the cin statement actually places a value into a memory
location, the cout placed on the line before the cin statement provides a user
prompt so the user knows what data to enter. Each input shown on a
flowchart will have a corresponding cout/cin pair in the program.
input ageOne
2-7
Basic Structure of a C++ Program
- main function - {
named constant declarations
variable declarations
executable statements
}
3-8
// This program outlines the basic parts of a C++ program. "//" indicates a comment
// and causes the compiler to ignore text appearing on that line.
// Following are examples of preprocessor directives
#include <iostream> // needed in all programs for basic input/output
using namespace std;
void main(void)
{
// Declaration of named constants
const char MY_NAME[25] = "Your name here"; // programmer name
const char SECTION[25] = "Your 1A section here"; // class section
const char DATE[10] = "Month dd, yyyy"; // current date
// Declaration of variables
// Note the data table to the right of variable names
int num1; // first value to average - INPUT
int num2; // second value to average - INPUT
float average; // average of the two values - CALC & OUTPUT
// Get numbers to average from user - prompt using cout and input using cin
cout<< "Enter first integer to average: ";
cin>> num1;
cout<< "Enter second integer to average: ";
cin>> num2;
2-9
Review
3-10
Declaration Section Exercise
Write the necessary declaration section for a program requiring the
following variables and named constants. Use the proper style and be sure
to include the data table. Also, remember that identifiers must be
descriptive.
2-11