For example:
| Input | Output |
|---|---|
| A positive real number | Its square root |
The program structure for a straight forward program is, therefore
/* Name and Project
Algorithm
Status
*/
#include ...
using namespace std;
declare global constants like PI.
main()
{
Declare variables
Get values for input variables
Do some calculations
Send some output to the user
}
#include <cmath>
using namespace std;
const double PI = 4 * atan(1.0);
g++ -lm -o pythagoras pythagoras.cpp(the "-lm" adds the compiled 'm' library to the program). However, Q understands this and does it for you.
double nth_power_of_x = pow(x, n);
int v;(for counting)
double v;(for measurements)
cin >> v >> v...;
cout << e << e ... ;
cout << "string" << endl ;
v = e;where v is any variable name and e is any expression.
{
steps
}
For example, if you see this bit of code:
int i;you will need to record a column like this
| int i | i |
|---|---|
| ? |
After these two steps:
int i;
i = 10;you will need to record a column like this
| int i | i |
|---|---|
| ? | |
| i=10 | 10 |
int i;
i = 10;
i = 2 * i;you will need to record a column like this
| int i | i |
|---|---|
| ? | |
| i=10 | 10 |
| i=2* | 20 |
For this class just pages 26-42.
I don't have any questions today can I ask one one on Thursday
You need to ask a question every class to get credit... even if you think
you know the answer.
Which is the most important part of the reading.
Please look at the web page for the "***" ratings.
Note: this question is too general to prove you did the reading.... next time no credit.
How can I remember all the special words and rules
Write them down on a single sheet of paper.
You can bring a single such sheet into all quizzes and the final.
Who figured out how to write expressions
Jim Backus in FORTRAN I...... which lead to the Algols,....B,...C,...C++.
Who figures out what to write in expressions
Programmers.
What is the precedence for oprators in C++
It is close to "Please Excuse My Dear Aunt Sally" or
the British "BODMAS" mnemonic.
Are there any confusing expressions
Warning: look out for "^" it is not exponentiation.
Warning: Look out for expressions like
2/3because the computer sees two integers (2) and (3) and so does integer division giving the answer
0To get "two-thirds" you need to force the compile to use double length calclations like this
2.0/3.0See [ 03ex.cpp ] for a demo.
What are real numbers
Talk to the math department!
Can you have a double constant and how do you do it
Yes.
const double NAME = value;Example
const double PI = 4*atan(1);
Note -- you can have constants of any type you want. When ever you learn about a new type you'll find you can construct constants for that type of object.
Does C++ have sigle length vaariables like BASIC
Yes. They are called 'float' vaariables and declared like this
float x;
float CONST = value;
In this class don't use them -- our computers are fast and big enough to handle full precision double length calculations.
What do double and const double do
These reserve space for a double-length number -- ready to do
calculations. Good for holding measurements. Vital
when you want to do work with fractions.
They also give the space
a name.
How do you use double in a program
It's used to declare variables that hold measurements and fractions:
double name;
double name=initial value;
You should always declare a variable before you use it.
Is writing the declaration first like backwards
Possibly -- but it is needed for the compiler to know what each
symbol means the first time it sees it.
Is there a limit to the number of variable you can have
No.
What is the int main()
In some books (and my old examples) the main function is
always written like this:
int main()rather than Jan's
main()Both are ok. The first is very precise. The second use a rule that, by default, functions return 'int's.
Why is the final return not required by the standard
Because if you had to have it then thousands of C and old
C++ programs would by non-standard and not compile. The standards
people (and most compilers) left a loop hole.
Why aren't complicated math functions included automatically
The take up time to compile and make the compiled program bigger.
When C++ was C it was used mainly for "system programming" and there was no need for most program to do complex math.
This is even more true about non-elementary functions that can be found on the Internet... but are not part of the standard. Most programs don't need them.
How is input entered
The program starts to run and stops. The user types in the input data
and taps the "Enter" or "Return" key. The program takes what the
user typed and makes it fit (if possible) what is in the cin statement.
Can you solve and math problem in C++
You can solve any solvable problem in C++.
One of the big discoveries made in the beginning of computer science was the discovery that some problems can not be solved by a computer. These are called unsolvable problems and Turing was able to show that no computer would be able to solve them. This is a fascinating and tricky topic that is covered in the CSci500 level theory classes.
What we can say is that if any machine can do it then C++ can program it.
Can we make the the input and output look nice like Visual Basic
Yes -- if you get a copy and #include the right library.
Sadly I don't think we'll have time to do this in CSCI201.
On the other hand it is very easy to do once you've got the library.
For example both our KDE linux and MS Windows have nice user interfaces
that let you draw forms and generate code for them.
I didnt fully understand ++ and -- on page 39
(1) It is a subtle bit of C++.
(2) The book seems to have a misprint.
C++ lets you put ++ and -- operations inside expressions. They always add (subtract) one from a variable, but they also return a value. Suppose we have
| i | j | k |
|---|---|---|
| 2 | 4 | 17 |
k = ++ i;we have
| i | j | k |
|---|---|---|
| 3 | 4 | 3 |
If we again start with
| i | j | k |
|---|---|---|
| 2 | 4 | 17 |
k = i ++;we have
| i | j | k |
|---|---|---|
| 3 | 4 | 2 |
Summary of the rules
| Expression | Effect on i | Returned value |
|---|---|---|
| i+1 | None | old i + 1 |
| i++ | add 1 to i | old i |
| ++i | add 1 to i | old i + 1 |
Why is tracing a program helpful
No other method gives you an understanding of what the computer is going to
do.
It shows that you understand the program and so can earn you points in a quiz.
What does cout mean
This variable is pronounced "C out" and it means: the place where
C output is sent.
Quiz 1
I will give you some straight forward programs
and you work out what they output.
I will also ask you to describe a straight forward program
that you'd like to do as a project: inputs and output. See
[ projects.html#P1 ]
Lab 02 -- Straight forward programs
Note
The first project is also a straight forward program. But not one
in the labs or in class!
Next -- Selections