Saturday, January 12, 2013

VARIABLES, OPERATORS and DATA TYPES
 
       I.           I.   Summary Of The Theory
 
Variables, data types and operators
A Variable is an identifier (identifier) is used to represent a certain value in the process of the program. While the value of the variable can be changed according to the needs of the Fox. The name of a variable can be specified by programming to the rules as follows:

Consists of a combination of letters and numbers with the first character must be a letter. Because it is Case Sensitive so that between the writing of nim, NIM and Nim in the think different.
Must not contain spaces.
Must not contain symbol symbol is reserved as: $,?,%, #,!, &, (,),-, =, etc., but the bottom line (underscore)
Free length, but only the first 32 characters are used.

Examples of planting the correct variables:

NIM, a, x, nama_mahasiswa, f3087, nilai, dsba
Examples of planting the wrong variable:
% nilai_mahasiswa, 80mahasiswa, rata-rata, ada spasi ,penting!, etc.

The Operator is a token which carry out basic identifies the for a data object. As for example an example of operator like:

1. ARITHMETIC OPERATORS
There are five arithmetic operators that are available in C++, namely:
* Function to operating Multiplication
/function for operating Division
%  operating function for the rest of the Division (modulus)
+ Function to operating Sum
-Function for Operating reduction
2. COMPARISON OPERATORS
< less than
< = less than equal to
> greater than
== is equal to
! = Not equal to
3. LOGIC OPERATORS
& & Logical AND (and)
|| Logical OR (Or)
! Logical NOT (EVENT)
4. DATA TYPES
The Data type is a part of the most important program because data types affect any instruction which will be funded by the computer. For example only 5 in for with 2 could have different calculations produce results depending on the data type. If 5 and 2 of type integer it will result in a value of 2, but if his second of type float and will result in a value of 2,500000. The selection of the proper data type will make the process carry out data become more efficient and effective. As for the example data types used in the C++ language, namely:


Type
Size
Range
Description
char
1 byte
Signed: -128 … 127
Unsigned: 0 … 225
Character
Short int
(short)
2 bytes
Signed: -32768 … 32767
Unsigned: 0 … 65535
Short integer
int
4 bytes
Signed: -2147483648 … 2147483647
Unsigned: 0 … 4294967295
Integer/ bilangan bulat
Long int (long)
4 bytes
Signed: -2147483648 … 2147483647
Unsigned: 0 … 4294967295
Long integer
bool
1 byte
True atau flase
Boolean
float
4 bytes
+ 4.3e-38 … +3.4e38
- 4.3e-38 … -3.4e38
Float / bilangan
pecahan
double
8 bytes
1.7 – 308 … 1,7e308
-1.7 – 308 … -1,7e308
Pecahan presisi
Ganda

1. DETERMINING the FORMAT CODE
% c: Read a character
% s: read a string
% i,% d: read an integer (integer)
% f,% e: reading a fractional number (Float)
% g: Read a number of fractions in the notation% or% e f
% o: reading a number of the oxtal
% x: reading a number of the heksdesimal
% u: Read a number of unmarked

The function gets and scanf
The second function is in use to take input from the keyboard and entering them into a variable, it's just two different parameters.
Function of the gats can only take input string brtipe, whereas the scanf can take input of type string or others, e.g. float and double.





III. DISCUSSION of the experiment and STEP
The Program fills in the input string to a variable

*      Program II.1


#include <stdio.h>
main ()
{
     char nama [20] ;
     printf ("Masukan mamamu: ") ;
     gets ("mustofa") ;
     printf ("Hallo ") ;
     puts ("mustofa") ;
     printf ("Betul kan, namamu %s?\n", "mustofa") ;
     return 0 ;
}
 
 





*      Hasil Program II.1
(1) the meaning of the number 8 in the name [20] are: the setting for the maximum nominal amount of characters to be entered by typing on the on-screen keyboard when the program is run.
The Result Of The Function gets(nama) ;
be scanf (“%”,nama) ;
(1) the usefulness of the function "gets" IE: in use to take input of type strings from keyboard and memasukkanya into the variable.
There are also examples of running his program:


(1) the usefulness of the function "scanf" IE: same thing with the function "gets" that is to take input from the keyboard and entering them into variables as for the type of input that can be taken by this function in the form of the type string, float, double, etc.
And at the time of the program in the compile output display characters not fully in view.




Integer math programs & divisions: real

*     
#include <stdio.h>
main ()
{
     int a = 10, c;
     float b = 3.5, d;

     c = a/b; d = a/b;
     printf ("a = %d\n", a);
     printf ("b = %f\n", b);
     printf ("c = %d\n", c);
     printf ("d = %f\n", d);
     return 0 ;
}
 
Program II.2






*      Hasil dari Program II.2
(1) the value of the variable c & d is
The value of C = 2, while for values of D = 2.857143

(2) the value of C and D are different because the input C programming code using the code defining the format can only read an integer (integer).
While the input code programming D using the code defining the format that can be read through a fractional number (float).

The Program input to the variable number: calculate the root

*     
#include <stdio.h>

main ()
{
     int a ;
     float b ;
     printf ("masukan nilai a = ") ;
     scanf ("%d",a) ;
     b = sqrt (a) ;
     printf ("Akar dari a = %f", b) ;
     return 0 ;
}
 
Program II.3



Results the Program II 3.

Runtime-error occurred when the above program compiled.
(1) because the header files that are in use is not in accordance with the program files that are on the run, at the time of program compiling so that the program could not create the output file that is on the run.

After added # include <math.h> at the top, and then compiled again.

(2) error appears-Runtime: (Unable to create output file)

Replace the scanf ("%d", a); with scanf ("% f", & a);
Scanf is Parameter pass by reference, so it should be given the address of the variable. The notation "&" is used to indicate the address variable "a" or a pointer to the variable "a"
Examples of input value a = 100.5
The Results Of Running The Program:


(1) thus, the result of "a" is of type integer so that the resulting value of a = 10.000000

(2) such event occurs because setting the data type program that we write at the input can only read an integer (integer), while the nominal input bilangn is broken so the response output only shows the roots of the integers in the input that is apparent with the nominal character one hundred (100).

Replace the scanf ("%d", & a); with scanf ("% f", a);

(3) the Program is not able to process the data we input, and the result as in the picture below:

(1) the Program must be%d%f instead because the input data types we write on this program only serves to read an integer/Integer, similarly with the deciding his format code we should equalize/adjust function, otherwise the program can't read/process data we enter at the time of the program is on the run,
example:
The data type "int" can only be read/process data in the form of an integer (integer), while deciding his format code we write% f which enable just to read fractions (float), here there is no function to simultaneously between data type in write code by defining the format will then impact that occurs when a program on the run, the program is not able to process the data we input.

*      Change  printf ("Akar dari a = %f", b) ; be
printf ("Akar dari a = %d", b) ;

1). The Program cannot process the data that we input, and the result as in the picture below:

The results of running the Program:

(1) a Program should%f instead of%d because the output data type which we write on this program only serves to read fractions/float, as well as determining its format code we should equalize/adjust function, otherwise the program can't read/process data we put on when the program is run.

example:
The data type "float" it can only read/process data in the form of fractions (float), while deciding his format code we write% d a disable just for reading whole numbers (integers), here there is no function to simultaneously between data type in write code by defining the format will then impact that occurs when a program on the run, the program is not able to process the data we input.
Programs with some input: calculate the average




*      Program II.4 


#include <stdio.h>
#include <math.h>
main()
{
     int a, b, c, rerata;
     float Rerata ;
     printf ("Masukan nilai a, b, dan c = ") ;
     scanf ("%d %d %d",&a, &b, &c) ;
     Rerata = (a+b+c)/3 ;
     printf ("Rerata = %f", Rerata) ;
}
 
 








(1) after making the running without a modified will an error occurs, then it should be modified so that error does not occur are as follows:

Add # include <math.h> at the bottom of the # include <stdio.h>
Float a, b, c, average; replaced int a, b, c, average;
Add instruction data type float Average;
These ellipses by their defining code format from% to% f d
On the average, R the letter-shaped beams.

As for the results of running the program II. 4 as follows:
a. With interspersed in a space:

Program menghitung invers
#include <stdio.h>

main ()
{
     int a;
     float b,c;

     printf     ("Masukan nilai a= ");
     scanf      ("%d",&a);
     b = 1/a;
     printf     ("b = 1/%d= %f\n", a,b);
     c = 1.0/a;
     printf     ("c = 1/%d= %f\n", a,c);
     return 0;
}
 
Progam II.5

     
      Hasil Runing:




*      Hasil running program
(1) the cause of the difference is due to b and c, the amount of the nominal number divisor from each different operation.
Example: nominal 80 if we share with 1 then the result of the Division will be apparent number of 0.000000 (zero),
And if the nominal 80 we share with his Division then 1.0 will result with the number of 0,012500. It is here that caused the value of b and c are different although the same oprasinya.

       I.            the task

1.      the level of precision of real numbers


#include <stdio.h>
main ()
{
     float a=3, b;
     double c;
     b = 1000 /a;
     c = 1000 /a;
     printf     ("a = %f\n",a) ;
     printf     ("b = %f\n",b) ;
     printf     ("c = %f\n",c) ;
}
 
 





*      results Runing:

The cause of the difference in values between b and c is:

because of differences in data types on each operation, carry out at b using the data type float (fractions)
carry out while in c using the double data type (double precision fractions), this is where that led to differences in the value of b and c are different.

2.      Pengaturan Tampilan


#include <stdio.h>
main ()
{
     double a=3.0, b;
     b = 1000.0/a;
     printf     ("a = %f\t\t b = %f\n",a,b) ;
     printf     ("a = %7.2f\t\t b = %7.2f\n",a,b) ;
     printf     ("a = %+7.4f\t\t b = %+7.4f\n",a,b) ;
     printf     ("a = %g\t\t b= %g\n",a,b) ;
     printf     ("a = %e\t b = %e\n",a,b) ;
     printf     ("a = %5.2e\t\t b = %5.2e\n",a,b) ;
     }
 
 







*      Hasil Runing:

Format
Keterangan
%f
calling a variable of type integer fractions
%7.2f
calling a variable of type integer fractions with a width of 7-digit display and accuracy (precision) 2 digits
%+7.4
calling a variable of type integer fractions with a width of 7-digit display and accuracy (precision) 2 digits
%g
For the notation "a" shaped units and rounded, while the "b" form of fractions
%e
Read the number of ranking with a maximum of 2 characters behind the + sign
%5.2e
There is a max 5 characters and 2 characters behind the + sign



          
Conclusion

t program because data types affect any instruction which will be funded by the computer.
v initial Key to be able to run a C++ program that is located on the main components of C++ i.e. # include and main ()

v the appearance of error message on the screen that C++ caused an error in writing the program, this resulted in the system the program is not running so we should allow it to be read.

v Any punctuation entered in C++ has the codes certain
.If we want to display text as we want then we should follow procedures, such as adding text, signs, or form letters, and all it has to be true.

v is an Operator tokens that carry out basic identifies the for a data object.

v. Data type is a part of the most important
v at the time of the calculation of average on the number we need to add # include <math.h>





 III.            Referensi
 
ü  Modul Praktikum Dasar Pemrograman Kartika Firdausy, S.T.,M.T





K






  

Author: Unknown