Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program...
-
Upload
amie-gordon -
Category
Documents
-
view
216 -
download
0
Transcript of Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program...
Overview of c++
Objectives
1. Understanding the use of the following elements in a c++ program
variables
constants
assignment
input
output
2. Writing a simple c++ program
Variables - How Do We Know If We Need Them?
Consider a variation of the previous problem.
INPUT -> PROCESS -> OUTPUTdim in feet calc perimeter total costCostPYard SqYards
calc cost$1.00/ft edging
+ SqYards * CostPYard
PROCESS1. input length and width2. calc perimeter3. calc square feet4. calc square yards5. input CostPYard6. calc cost of edging7. calc cost of unedged carpet8. calc total cost -> cost of carpet + cost of edging9. output total cost
Extreme -> Minimum Variables
#include <iostream.h>
void main()
{ const float PerFtEdging = 1.0;
float length, width, CostPYard;
cin >> length >> width >> CostPYard;
cout << 2*(length+width) * PerFtEdging +
( (length*width)/9.0) * CostPYard;
}
Once again you have a tradeoff:Less Variables
More difficult to follow
Extreme -> Maximum Variables
#include <iostream.h>
void main()
{ const float PerFtEdging = 1.0;
float length, width, Perimeter, CostPYard, CostEdging, CostCarpet, SqFeet, SqYards, TotalCost;
cin >> length >> width;
Perimeter = 2 * (length + width);
SqFeet = length * width;
SqYards = SqFeet/9;
cin >> CostPYard;
CostEdging = Perimeter * PerFtEdging;
CostCarpet = SqYards * CostPYard;
TotalCost = CostEdging + CostCarpet;
cout << TotalCost;
}
More VariablesIt's somewhat subjective.
Avoid UNDERuse of variables!
I have avoided comments and labelling for space on the overhead
HOW Do We Tell C++ We Need Variables?
In declarations of the following form!
type variable-name;
examples:
float length;
float width;
or
float length, width;
NAMES->
begin with letter
have letters, digits, underscores
avoid c++ names (cout, cin, float)
try to use appropriate names
TYPES->
float int (strings)
(more later)
float
Can be fractional
1.0 12.7 12e+6 12e-1
Are APPROXIMATE when stored in a computer
MOST Base 10 numbers CAN NOT be represented in a finite number of Base 2 digits. -> They would require an infinite amount of memory to be exact!
Understand scientific notation.
Can be very large or small
1e39 1e-39
Limited PRECISION -> 7 base 10 digits
123.456789 would be stored as
123.456779232163523127123
int
Whole numbers ONLY
Are EXACT, not approximate
Limited size :
-2 billion < int < +2 billion
(roughly)
A number of kinds of integers exist in c++.
They vary by the range of size of values (amount of memory)
whether they are
signed
+3 -12 +4 -123
or unsigned
2 5 454 34 (no negatives)
For Now Use "int" ONLYBe careful when dividing integers!
Strings
Strings as we will use them initially are NOT variable types.
For example, we can NOT do the following:#include <iostream.h>
void main()
{ int first; // this is OK
string second; // do not try this yet
.....
}
Strings are used for output whether it is on the output device printer file screen
Strings are underlined in this example. Note no variables!..
first = 10;
cout >> "The value of first is " >> first >> "\n"
Output is
The value of first is 10
Inputting Data
What role does the input play in your programs?
ANSWER:
To transfer values from devices such as the keyboard, disk or mouse to the main memory of the machine.
In C++
"cin"
Consider the following simple program:
int age; // step 1
age = 10; // step 2
cin >> age; // step 3
STEP what happens value of age
1 memory allocated ???2 age value set to 10 103a program executes cin 103b user types value 23 23
Whatever value is typed by the user is placed in age.
More cin
READING MULTIPLE VALUES:
Suppose you need to read 3 values.
cin >> a >> b >> c; // separate variables with >>
OR cin >> a;
cin >> b;
cin >> c;
Either. In most situations, they function the same. c++ will continue reading until it finds the data you ask for no matter how many lines it takes. Try running cinEx1.C and cinEx2.C from the public account and run both by entering the data on the same line and different lines (four times altogether).
You will see they work the same way!
1. Rules for reading char and strings require special consideration.
2. Special cases are best ignored at this point.
Output in c++
Lets you output to the screen any of the following:
int float string and others
The values which are printed can be either variables, constants, or (in general) expression values.
Separate each value (expression) with a <<
EXAMPLES:
cout << "\n"; // says position output on next line
cout << ‘\n’; // same effect but really different
cout << endl; // does the same thing
x = 10;
y = 7;
// outputs only the value 10
cout << x;
// outputs The value of x is 10 and a newline
cout << "The value of x is " << x << "\n";
// outputs 107 ... forgot to output spacing
cout << x;
cout << y;
Expressions
Don't overwhelm yourself with rules.
They'll come with practice!
Rules: (Fig. 1.11, p.28)
1. () parentheses are evaluated first
2. * / % are equal and evaluated second
+ - are equal and evaluated last
3. when operations are equal LEFT->RIGHT
Terms: X + 12
operand operator operand
Division and Mod for integers:
Think of grade-school division (w/o remainder)
EXAMPLE:
(7 / 2) * 2 + (7 % 2)
3 * 2 + 1
6 + 1
7
When writing expressions, use parentheses as you learn to use the rules!
Mixed Mode Expressions
Mixing types within a c++ program generally is a problem.
Numerical expressions are an exception.
Logic dictates that a float plus an int is a float.
As you evaluate an expression, remember TYPE at each stage.
int m;
m = 5.5 + 5 / 2
5.5 + 2 // integer division
5.5 + 2.0 // convert to float
7.5 // float addition
7 // convert to int
BE SURE TO REVIEW TEXT EXAMPLES
Expression Example 1
P2 - P1
t2 - t1
V =The formula for the average velocity, v, of a particle traveling on a line between points p1 and p2 in time t1 and t2 is
The formula can be written and evaluated in c++ as follows:
V = (P2 - P1) / (t2 - t1)
-
/
-
V
1
3
24.5 9.0 0.0 60.0
P1 P2 t1 t2
V = (P2 - P1) / (t2 - t1) 9.0 4.5 60.0 0.0
4.5 60.0
0.075
Fig. 2.13, Problem Solving and Design in C, Addison-Wesley, by Jeri R. Hanly, et. al
Expression Example 2
Another expression:
The formula can be written and evaluated in c++ as follows:
z - (a + b / 2) + w * -y
z - (a + b / 2) + w * -y
DONE
1 -3
6
*4
/
2 +
5 -
+
Fig. 2.13, Problem Solving and Design in C, Addison-Wesley, by Jeri R. Hanly, et. al
8
z a b w y
8 3 9 2 -5
4 5
11
3 9 2 -5
z - (a + b / 2) + w * -y
7 10
1 . Unary operators first!
Assuming all values are integer!
Another expression:
The formula can be written and evaluated in c++ as follows:
z - (a + b / 2) + w * -y
z - (a + b / 2) + w * -y
DONE
1 -3
6
*4
/
2 +
5 -
+
Fig. 2.13, Problem Solving and Design in C, Addison-Wesley, by Jeri R. Hanly, et. al
8
z a b w y
8 3.0 9 2 -5
4 5
11.0
3.0 9 2 -5
z - (a + b / 2) + w * -y
7.0 10
1.0 . Unary operators first!
Assuming ONLY a is float!
Expression Example 3 - MIXED mode
Redirection
Your Lab will show you how to do this!
PROBLEM:
How do you make your program read a file?
helps rerun the program without having to type the data over
helps to substitute new data setsno changes to program, only how you RUN it!
1. build a data file with an editor (TESTDATA)
2. instead of running the program as
a.out
use
a.out < TESTDATA
When you run the program, you WON'T have to type the input data. It will be read from the file instead.
NO PROGRAM CHANGE
A similar method exists for writing output to a file
Standard Input&
Standard Output
CIN -> Reads from the standard input
COUT -> Writes to the standard output
REDIRECTION simply redefines the standard input and/or standard output
to be a file
What if you wanted to read (write) 3 files at the same time?
ANSWER:
Redirection won't work.
You need another method which we'll learn later.
Program Structure
// Any comments // David Game
// typically AUTHOR, filename // Assignment 1
// date, assignment, etc.
#include directives #include <iostream.h>
void main ( ) void main ()
{ {
constant declarations const float x=1.2;
variable declarations int y;
executable statements y = x + 3;
(cin,cout, assignment) cout << y;
} }
Always read “Good Programming Practices” at the end of each chapter