Showing posts with label double. Show all posts
Showing posts with label double. Show all posts

Wednesday, March 22, 2017

C++ Part 41

Review Part IV

Function Call
A function call is an expression consisting of the function name followed
by arguments enclosed in parentheses. If there is more than one argument,
the arguments are separated by commas. A function call is an expression
that can be used like any other expression of the type specified for the
value returned by the function.
SYNTAX
Function_Name(Argument_List)
where the Argument_List is a comma-separated list of arguments:
Argument_1, Argument_2, . . . , Argument_Last
EXAMPLES
side = sqrt(area);
cout << "2.5 to the power 3.0 is " << pow(2.5, 3.0);

A Function to Convert from int to double
The notation static_cast can be used as a predefined function
and will convert a value of some other type to a value of type double. For
example, static_cast(2) returns 2.0. This is called type casting.
(Type casting can be done with types other than double, but until later in
this book, we will do type casting only with the type double.)
SYNTAX
static_cast(Expression_of_Type_int)
EXAMPLE
int total_pot, number_of_winners;
double your_winnings;
. . .
your_winnings =
static_cast(total_pot)/number_of_winners;

Function Declaration
A function declaration tells you all you need to know to write a call to the
function. A function declaration is required to appear in your code prior to
a call to a function whose definition has not yet appeared. Function
declarations are normally placed before the main part of your program.
SYNTAX
Type_Returned Function_Name(Parameter_List);
Function_Declaration_Comment
where the Parameter_List is a comma-separated list of parameters:
Type_1 Formal_Parameter_1, Type_2 Formal_Parameter_2,...
..., Type_Last Formal_Parameter_Last
Example
double total_weight(int number, double weight_of_one);
//Returns the total weight of number items that
//each weigh weight_of_one.

A Function Is Like a Small Program
To understand functions, keep the following three points in mind:
■ A function definition is like a small program and calling the function is the
same thing as running this “small program.”
■ A function uses formal parameters, rather than cin, for input. The arguments to
the function are the input and they are plugged in for the formal parameters.
■ A function (of the kind discussed in this chapter) does not normally send any output
to the screen, but it does send a kind of “output” back to the program. The
function returns a value, which is like the “output” for the function. The function
uses a return statement instead of a cout statement for this “output.”

Procedural Abstraction
When applied to a function definition, the principle of procedural
abstraction means that your function should be written so that it can be
used like a black box. This means that the programmer who uses the
function should not need to look at the body of the function definition to
see how the function works. The function declaration and the accompanying
comment should be all the programmer needs to know in order to use
the function. To ensure that your function definitions have this important
property, you should strictly adhere to the following rules:
HOW TO WRITE A BLACK-BOX FUNCTION DEFINITION (THAT RETURNS
A VALUE)
■ The function declaration comment should tell the programmer any and all conditions
that are required of the arguments to the function and should describe
the value that is returned by the function when called with these arguments.
■ All variables used in the function body should be declared in the function body.
(The formal parameters do not need to be declared, because they are listed in
the function declaration.)

Make a Loop Body a Function Call
Whenever you have a loop nested within a loop, or any other complex
computation included in a loop body, make the loop body a function call.
This way you can separate the design of the loop body from the design of
the rest of the program. This divides your programming task into two
smaller subtasks.

C++ Part 39

Review Part II
Starting New Lines in Output
To start a new output line, you can include \n in a quoted string, as in the
following example:
cout << "You have definitely won\n" << "one of the following prizes:\n"; Recall that \n is typed as two symbols with no space in-between the two symbols. Alternatively, you can start a new line by outputting endl. An equivalent way to write the above cout statement is as follows: cout << "You have definitely won" << endl << "one of the following prizes:" << endl;

Outputting Values of Type double
If you insert the following “magic formula” in your program, then all
numbers of type double (or any other type that allows for digits after the
decimal point) will be output in ordinary everyday notation with two digits
after the decimal point:
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
You can use any other nonnegative whole number in place of the 2 to
specify a different number of digits after the decimal point. You can even
use a variable of type int in place of the 2

cin Statements
A cin statement sets variables equal to values typed in at the keyboard.
SYNTAX cin >> Variable_1 >> Variable_2 >> . . . ;
EXAMPLE cin >> number >> size;
cin >> time_to_go
>> points_needed;

The “and” Operator &&
You can form a more elaborate Boolean expression by combining two
simple tests using the “and” operator &&.
SYNTAX (FOR A BOOLEAN EXPRESSION USING &&)
(Comparison_1) && (Comparison_2)
EXAMPLE
(WITHIN AN if-else STATEMENT)
if ( (score > 0) && (score < 10) ) cout << "score is between 0 and 10\n"; else cout << "score is not between 0 and 10.\n"; If the value of score is greater than 0 and the value of score is also less than 10, then the first cout statement will be executed; otherwise, the second cout statement will be executed.

The “or” Operator ||
You can form a more elaborate Boolean expression by combining two
simple tests using the “or” operator ||.
SYNTAX (FOR A BOOLEAN EXPRESSION USING ||)
(Comparison_1) || (Comparison_2)
EXAMPLE
(WITHIN AN if-else STATEMENT)
if ( (x == 1) || (x == y) )
cout << "x is 1 or x equals y.\n";
else
cout << "x is neither 1 nor equal to y.\n";
If the value of x is equal to 1 or the value of x is equal to the value of y (or
both), then the first cout statement will be executed; otherwise, the
second cout statement will be executed.

Sunday, March 12, 2017

C++ Part 16

Review to C++ Basics

C++ is case sensitive. For example, count and COUNT are two different identifiers.
Use meaningful names for variables.
Variables must be declared before they are used. Other than following this rule, a
variable declaration may appear anyplace.

Be sure that variables are initialized before the program attempts to use their value.
This can be done when the variable is declared or with an 
assignment statement before
the variable is first used.

You can assign a value of an integer type, like int, to a variable of a floating-point
type, like 
double, but not vice versa.

Almost all number constants in a program should be given meaningful names that
can be used in place of the numbers. This can be done by using the modifier 
constin a variable declaration.

Use enough parentheses in arithmetic expressions to make the order of operations clear.
The object cout is used for console output.
A \n in a quoted string or an endl sent to console output starts a new line of output.
The object cerr is used for error messages. In a typical environment, cerr behaves
the same as 
cout.The object cin is used for console input.

In order to use cin, cout, or cerr, you should place the following directives near
the beginning of the file with your program:
#include <iostream>using namespace std;

There are two forms of comments in C++: Everything following // on the same line
is a comment
, and anything enclosed in/* and */ is a comment.

Do not over comment. 

__________________
Source: Absolute C++ by Walter Savitch , Kenrick Mock

Intro to C++ Part 12

  • How Do I Start a Program? 


  1.  Identify inputs and outputs.
  2. Understand what you asked for ( Ask Questions ).
  3. Break the program down. // Functions
  4. into steps, writing each step in pseudocode English.
  5.  Replace each pseudocode step with C++ codes.

Saturday, March 11, 2017

Intro to C++ Part 8

Variable

  • Every variable in a C++ program must be declared before it is used.  

Digital Design Part 3

4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...