Input Statement on C++

a. CIN( )
In C + +, cin command is used to input a value from a input device (keyboard) for further processed by the program.
Syntax is cin>> variable;
example:

court <<"Enter a number:";
cin>> bil;
court <<"You entered number" <<bil "\ n";
b. GETCH ( )
Command getch () function just like cin (command inputs), but getch () specifically for input in the form of characters. Besides, getch () to read input form a space or a tab, whereas cin can not.
syntax is: Variable = getch ();
This function can also be used if the suppression of unwanted ENTER when character data input.

Getche command () uses the same as getch (), the difference is:
- getch () does not display characters diiinput
- getche () displays the inputted character
function getch () and getche () are both needed header files conio.h
Here are examples of the use of getch () and getche ()

char character;
court <<"enter a character:";
character = getch ();
court <<"You typed characters:" <<character;
court <<"enter a character:";
character = getche ();
court <<"You typed characters:" <<character;

Operator and Statements

Operator is a symbol commonly involved in the program for perform an operation or manipulation, for example to:
- add two values
- assign value to a variable (assignment)
- comparing the similarity of two values​​.
a. Arithmetic Operator
This operator is used for basic arithmetic calculations. These operators include:
- Multiplication (*)
- Distribution (/) - Divide
- Addition (+)
- Reduction (-)
- Modulus (%)
b. Relational Operator 
This operator is used to compare two values​​. Here's many operator of this type.
- Equal (==)
- Not equal (!=)
- Smaller (<)
- Bigger (>)
The result of this operator is true or false statements. Look this sample program:
value_1 = 3 > 2;
value_2 = 15 == 16;
cout << “Result of value 1 = ”<< value_1 << “\n”;
cout << “Result of value 2 = ”<< value_2 << “\n”;
c. Logical Operator
This operator is used to connect 2 or more statements. Usually statement is linked is a relational operation. Logical operators as well logic value true or false.
- AND (&&)
- OR (||)
- NOT (!)


Look this sample program.
number1 = (3 > 2) && (4 < 10);
number2 = !(15 == 15);
cout << “Result of number1 = ”<< number1 << “\n”;
cout << “Result of number2 = ”<< number2 << “\n”;

C++ Elements

a. Identifier
Identifier is a name commonly used in programming for declare variables, constants, data type, and function. Rules for writing the identifier together with the rules in pascals, among others:
- It should not be initiated by non-letter characters
- There should be no spaces
- Not allowed to use the characters  ~! @ # $% ^ & * () + `- = {} []:"; '<>?,. /
- Not allowed to use reserved words on c++

b. Data type
Data types used in c + + programming include:
Rounded number data type:
- char
- int (integer)
- short (short integer)
- long (long integer) 
Real number data type:
- float (real)
- double (real double)
- long double
 
Unsigned data type:
- unsigned char
- unsigned int (integer)
- unsigned short (short integer)
- unsigned long (long integer)

Unsigned data types which are not similar to unsigned. The difference is the data type unsigned does not recognize negative numbers (its value is always positive).
c. AssignmentAssignment process is the process of giving value to a variable that has been declared.
Here are examples of assignments:
Number = 10;
Price_unit= 23 456;
James= 'B';
Dean= '2 ';

 
Here's an example program that illustrates the declaration of variables and assignment.
#include <iostream.h>
#include <conio.h>
void main()
{
int var1, var2, var3;
char karakter;
var1 = 10;
var2 = 5;
var3 = var1 + var2;
karakter = ‘D’;
cout << “Value of var3 = ” << var3 << “\n”;
cout << “Value of character  = ” << karakter;
getch();
}
For next example, we can create program with precision statement
#include "conio.h";
#include "iostream.h";
#include "iomanip.h";
void main()
{
clrscr();
double real;
real = 182.2182713674821746;
cout << setprecision(12);
cout << "Value of real = " << real;
getch();
}



Declaration of Constanta (const)

It is simple in concept, variables declared with ‘const’ added become constants and cannot be altered by the program, but, in the way is has to be used to bodge in a substitute for one of the missing features of C++, it gets horridly complicated and frustratingly restrictive. The following attempts to explain how 'const' is used and why it exists.

Const gives you the ability to document your program more clearly and actually enforce that documentation. By enforcing your documentation, the const keyword provides guarantees to your users that allow you to make performance optimizations without the threat of damaging their data. For instance, const references allow you to specify that the data referred to won't be changed; this means that you can use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data. Even if it does, the compiler will prevent the code from compiling and alert you to the problem. On the other hand, if you didn't use const references, you'd have no easy way to ensure that your data wasn't modified.

Such constants are useful for parameters which are used in the program but are do not need to be changed after the program is compiled. It has an advantage for programmers over the C preprocessor ‘#define’ command in that it is understood & used by the compiler itself, not just substituted into the program text by the preprocessor before reaching the main compiler, so error messages are much more helpful.

It also works with pointers but one has to be careful where ‘const’ to determine whether the pointer
or what it points to is constant or both. For example look statement below:
 
const int myconstant = 10       //An int which can't change value. Similar to #define in C but better.
const int * myconstant            //Variable pointer to a constant integer.
int const * myconstant            //Same as above.
int * const myconstant            //Constant pointer to a variable integer.
int const * const myconstant   //Constant pointer to a constant integer.

Creating Variabel

A variable in C++ is a name for a piece of memory that can be used to store information. You can think of a variable as a mailbox, or a cubbyhole, where we can put and retrieve information. All computers have memory, called RAM (random access memory), that is available for programs to use. When a variable is declared, a piece of that memory is set aside for that variable.

In order to declare a variable, we generally use a declaration statement. Here’s an example of declaring variable x as an integer variable (one that can hold integer values):

char name_unit [20];

In C++, variables are also known as l-values (pronounced ell-values). An l-value is a value that has an address (in memory). Since all variables have addresses, all variables are l-values. They were originally named l-values because they are the only values that can be on the left side of an assignment statement. When we do an assignment, the left hand side of the assignment operator must be an l-value. Consequently, a statement like 5 = 6; will cause a compile error, because 5 is not an l-value. The value of 5 has no memory, and thus nothing can be assigned to it. 5 means 5, and it’s value can not be reassigned. When an l-value has a value assigned to it, the current value is overwritten. The opposite of l-values are r-values. An r-value refers to any value that can be assigned to an l-value. r-values are always evaluated to produce a single value. Examples of r-values are single numbers (such as 5, which evaluates to 5), variables (such as x, which evaluates to whatever number was last assigned to it), or expressions (such as 2+x, which evaluates to the last value of x plus 2).

Here is an example of some assignment statements, showing how the r-values evaluate:
int y;      // declare y as an integer variable
y = 4;      // 4 evaluates to 4, which is then assigned to y
y = 2 + 5;  // 2 + 5 evaluates to 7, which is then assigned to y

int x;      // declare x as an integer variable
x = y;      // y evaluates to 7, which is then assigned to x.
x = x;      // x evaluates to 7, which is then assigned to x (useless!)
x = x + 1;  // x + 1 evaluates to 8, which is then assigned to x.

Typing a Comment

Typically, comments should be used for three things. At the library, program, or function level, comments should be used to describe what the library, program, or function, does. For example:

All of these comments give the reader a good idea of what the program is trying to accomplish without having to look at the actual code. The user (possibly someone else, or you if you’re trying to reuse code you’ve already written in the future) can tell at a glance whether the code is relevant to what he or she is trying to accomplish. This is particularly important when working as part of a team, where not everybody will be familiar with all of the code.

Second, within the library, program, or function described above, comments should be used to describe how the code is going to accomplish it’s goal.

/* To calculate the final grade, we sum all the weighted midterm and homework scores and then divide by the number of scores to assign a percentage.  This percentage is used to calculate a letter grade. */

Typically, comments should be used for three things. At the library, program, or function level, comments should be used to describe what the library, program, or function, does. For example:

// To generate a random item, we're going to do the following:
// 1) Put all of the items of the desired rarity on a list
// 2) Calculate a probability for each item based on level and weight factor
// 3) Choose a random number
// 4) Figure out which item that random number corresponds to
// 5) Return the appropriate item

These comments give the user an idea of how the code is going to accomplish it’s goal without going into too much detail. At the statement level, comments should be used to describe why the code is doing something. A bad statement comment explains what the code is doing. If you ever write code that is so complex that needs a comment to explain what a statement is doing, you probably need to rewrite your code, not comment it.

My first program

Now, we will create simple program and we can compile that. But we should know about structure program in C++.Let's started, becouse I can hold anymore.

First, although our programs will be written inside .cpp files, the .cpp files themselves will be added to a project. Some IDEs call projects “workspaces”, or “solutions”. The project stores the names of all the code files we want to compile, and also saves various IDE settings. Every time we reopen the project, it will restore the state of the IDE to where we left off. When we choose to compile our program, the project tells the compiler and linker which files to compile and link. It is worth noting that project files for one IDE will not work in another IDE.

Second, there are different kinds of projects. When you create a new project, you will have to pick a project type. All of the projects that we will create in this tutorial will be console projects. A console project means that we are going to create programs that can be run from the dos or linux command-line. By default, console applications have no graphical user interface (GUI) and are compiled into stand-alone executable files. This is perfect for learning C++, because it keeps the complexity to a minimum.

Traditionally, the first program programmers write in a new language is the infamous hello world program, and we aren’t going to deprive you of that experience! You’ll thank us later. Maybe.

A quick note about examples containing code

Starting with this lesson, you will see many examples of C++ code presented. Most of these examples will look something like this:

#include <iostream>

int main()

{
        using namespace std;
        cout << "Hello world!" << endl;
        return 0;

}


Now we have a simple program to display a text like "Hello World", but we have compile thats first. If you select the code from these examples with your mouse and then copy/past it into your compiler, you will also get the line numbers, which you will have to strip out manually. Instead, click the “copy to clipboard” link at the top of the example. This will copy the code to your clipboard without the line numbers, which you can then paste into your compiler without any editing required.

Define of Structure and Statement

In this posting I want explain about structure and statement in programming. The most common type of instruction in a program is the statement. A statement in C++ is the smallest independent unit in the language. In human language, it is analogous to a sentence. We write sentences in order to convey an idea. In C++, we write statements in order to convey to the compiler that we want to perform a task. Statements in C++ are terminated by a semicolon.

There are many different kinds of statements in C++. The following are some of the most common types of simple statements:


#include <iostream>
int main()
{
        using namespace std;
        cout << "Hello world!" << endl;
        return 0;
}

int x is a declaration statement. It tells the compiler that x is a variable. All variables in a program must be declared before they are used. We will talk more about variables shortly.
x = 5 is an assignment statement. It assigns a value (5) to a variable (x).
cout << x; is an output statement. It outputs the value of x (which we set to 5 in the previous statement) to the screen.

The compiler is also capable of resolving expressions. An expression is an mathematical entity that evaluates to a value. For example, in math, the expression 2+3 evaluates to the value 5. Expressions can involve values (such as 2), variables (such as x), operators (such as +) and functions (which return an output value based on some input value). They can be singular (such as 2, or x), or compound (such as 2+3, 2+x, x+y, or (2+x)*(y-3)).

For example, the statement x = 2 + 3; is a valid assignment statement. The expression 2+3 evaluates to the value of 5. This value of 5 is then assigned to x.

What is the difference between a statement and an expression? A statement is a “complete sentence” that tells the compiler to perform a particular task. An expression is a mathematical entity that evaluates to a value. Expressions are often used inside of statements. What symbol do statements in C++ end with? The semicolon (;)

Structure program in C++ language

Examples of programs on the following material can be run with Borland C + + (Turbo C + + is fine). In general the structure of the program in C + + are as follows:
declaration header file / preprocessor
constant declaration
global var declaration
function declaration
class declaration
The main program (main function)
main () {
... ...
... ...
}

Description:
The declaration begins with the header file # include or # include "........." <.......> Filled dots with the name of the header file you wish to use. Header files is a function that supports the execution of certain instructions in C+ +

Declaration of constants
Declaration of global variables
Declaration of function can not be there. The function also can be placed seletah function main ().
Class declaration
The main function, must exist, beginning with {and end with}. In it contains instructions that will be executed following the declaration of variables (local variables) are needed.

Some header files that are often used:
iostream.h
For instructions:
<<"....................."; court cin>> nama_var;

conio.h
For instructions:
getch ();
clrscr ();

Define of Algorithm

The History of Algorithm

Historians of mathematics in the world have agreed that the word "algorithm"is derived from the word "algorithm / algorism" from the author of the famous Arab named Abu Jafar Muhammad ibn Musa al-Khuwarizmi (read by Westerners become algorism). He wrote a book called Kitab al jabar wal-muqobala which means "The book about the restoration and reduction" (The book of restoration and reduction). From this book also obtained the word "algebra / algebra", later adapted in the Indonesian language, the word algorithm is absorbed into the algorithm.

In 1950, said algorithms are often associated with "Euclidean algorithm" (Euclid's algorithm), which is a process to find the greatest common divisor of two integers. C + + more to answer the questions above are as follows:

The algorithm is a sequence of logical steps in solving problems systematically arranged. While definitions algorithm according to Big Indonesian Dictionary (1988), is a sequence of decision-making for solving problems

For example below is given Algorithm for Rate-making vessel contents.

Given two vessels A and B, A vessel containing a red liquid while the vessel B contains a blue liquid. Interchange the contents of the two vessels was such that the vessel A contains a blue liquid and liquid-filled vessel B into the red. The concept of such exchanges needed additional vessel is the vessel C which is used as a temporary storage place for the exchange process occurs.

Description:

1. Pour the liquid from the vessel A into the vessel C, so A becomes an empty vessel

2. Pour the liquid from the vessel B into the vessel A, so B becomes an empty vessel

3. Pour the liquid from the vessel C into the vessel B, so that the exchange has occurred and the vessel returned empty C

Problem:

Create a C + + program to determine whether a number is the user input is odd or even number.

Analysis:

To create C + + program that determines the odd or even number, we must first know how to distinguish between odd and even numbers. Examples of odd numbers are 1, 3, 5, 7, 9 and so on. Examples of even number is 2, 4, 6, 8 and so on. Before you can determine the odd and even numbers, you should know the rest of the division operator (MOD) / modulus

Division remainder operator yields the remainder quotient, for more details see the following example:

4 mod 2 0 (clear)
5 mod 2 1 (odd)
12 mod 4 0 (clear)
15 mod 2 1 (odd)


Program :
If created in C + + program, then the following is a C + + program to determine odd or even numbers.

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
main()
{
clrscr();
int bil, sisa;
cout<<"Insert some value : "; cin>>bil;
sisa = bil % 2;
if(sisa == 0)
cout<<<" even number"< else cout<<<" odd number"< getch();

Program c++ for selection (1)

Here is an example of C + + program that uses the IF statement. The problem is as follows: By using the IF statement, create a program to determine whether a student is "Passed" or "No Pass" on the basis of Value Theory and Practice Value entered by the user.

     Terms of students would graduate if the average value of at least 60 and minimum value of Practice 55. Eg = 100 Value Theory, Practice Value = 50 then the result = Disqualified. For example, Value Theory = 40, Value Practice = 90 then the result is Passed . Seeing the above question, then we know that there are two inputs namely Value Theory and Practice Value. Students will be based on two things: the average value and value theory. So first we must find the average value of first is by adding up the value of theory and practice and then divided by two. Program C + + more to answer the questions above are as follows:
 

# include <stdio.h>
# include <conio.h>
#include <iostream.h>

void main ()
{
clrscr ();
float the theory, practice, rata2;
court <<"Enter the value of theory:";
cin>> theory;
court <<"Enter the value of practice:", cin>> practice;

rata2 = (theory + practice) / 2;
if (rata2> = 60 & & practices> = 55)
{court <<"Passed";}
else
{court <<"No Pass";}
getch ();
}

Structure of Algorithm

The algorithm contains a step-by-step solution of a problem. These steps may include the contents of runs (sequences), the action selection (branching / selection) and repetition of actions (loops). The three types of step is the major construction that make up an algorithm. In other words that the algorithm is prepared or can be built from three basic structures, namely:

1. Of runs (sequences)

2. Elections (branching / selection)

3. Repetition (loops)

Description of each basic structure will be described below.


Ssequential

The runs consist of one or more instructions. Each instruction is done sequentially in the order written. Instruction sequence determines the final state of the algorithm. If the order is changed, the end result will also change. As an example consider the sequence of instructions denoted by A1, A2, A3, A4 and A5.

The process begins with the process in A1 only then do instruction A2, A3 selanjutna A2 will be done after the instruction is completed and so on and will stop after instruction A5 completed
.

History of Programming C++

Programming Languages C and C + +
C programming language is the language of the predecessor of C + + programming language. Creator of the C programming language is Brian W. Kernighan and Dennis M. Ritchie at around 1972. C is structured programming language that divides the program in the form of a number of blocks. The aim is to facilitate the creation and development program. C + + programming language was created a decade after C by Bjarne Stroustrup in 1983. At first the language C + + called "a better C". The name C + + is then given by Rick Mascitti in the summer of 1983. As for the + + in C + + comes from the name of the service enhancement in the C language Privileges that are very meaningful in C + + is because C + + language supports Object Oriented Programming (PBO) or Object Oriented Programming (OOP).
C + + Program Editor
Program C + + can be written using any text editor, like EDIT (owned by DOS), WordStar or congenital editor of the compiler. Examples of programs C + +:

Program C + + is usually written with the extension name. CPP (from the C plus plus). For this program can be run (executed), the program must be compiled in advance using C + + compiler.At the time of compilation, program source (. CPP) with header files (extension. H or. HPP) will be translated by a compiler C + + into object code (. OBJ). These object files include files in binary format (coded 0 and 1).
Furthermore, these object files together with other object files and library files (. LIB) linked together by a linker. The result is a file that is executable. This file can be run directly from the operating system directly.
C + + compiler
Today many C + + compilers on the market. For example, Microsoft Corporation, a software maker that is very popular, Microsoft issued a C + + compiler products in the form of Microsoft C / C + + and Visual C + +. The Borland International, which is very famous with its Turbo Pascal, Turbo C + + release and Borland C.



Recent Entries