Kumpulan Program Sederhana

Minggu, 20 Maret 2011

PROGRAM

Multidimensional Arrays

Multidimensional array is an array consisting of multiple subscript arrays. for example, 2-dimensional array is an array which has 2 subscript array, while the 3-dimensional array has 3 subscripts array. Such arrays are often used for processing the matrix. in this section we kana learn how to c + + can process an array consisting of two or more array subscripts.

2 Dimensional Arrays 
Two-dimensional array is an array which has 2 subscripts, namely row and column. A common form of 2-dimensional array is as follows

data_type
array_name[number of_element_rows][number of_element-column];

For example we will perform 2 pieces of the matrix sum of the order 3 X 2, then the sample adalh program as follows:

 
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 / / define the type of data in the form of 2-dimensional array  
typedef int MATRIK32 [3][2];

/ / declare array A as a 2-dimensional array
MATRIK32 A,B,C;

int a,b;/ / declare variables
          / / for loop index

/ / fill values ​​into the elements of array A
for(a=0;a<3;a++){
for(b=0;b<2;b++){
cout <<"A["<<a<<"]["<<b<<"]=";
cin>> A [a][b]; }
}
cout<< endl;

/ / fill values ​​into the elements of array B
for(a=0;a<3;a++){
for(b=0;b<2;b++){
cout <<"B["<<a<<"]["<<b<<"]=";
cin>> B[a][b]; }
}
cout<< endl;

/ / summation of A and B
/ / and save the results into an array C
for(a=0;a<3;a++){
for(b=0;b<2;b++){
C[a][b]=A[a][b]*B[a][b];}
}

/ / displays the sum of
for(a=0;a<3;a++){
for(b=0;b<2;b++){
cout <<"C["<<a<<"]["<<b<<"]="<< C[a][b]<<endl; }
}

    system("PAUSE");
    return EXIT_SUCCESS;
}

please try this program................
RECURSION

Recursion is when pendefinisiannya function calls itself to perform the process in it. The simplest example to show the process of recursion is when we create a program to calculate factorial of an integer, the example program as follows:

#include <cstdlib>
#include <iostream>

using namespace std;

/ / define the factorial function
int Faktorial (int X){
     if (X==1) return 1;
     return X*Faktorial(X-1);}
    
/ / main function
     int main (){
         int bilangan, HASIL;
         cout <<"masukan bilangan :";
         cin>>bilangan;
        
/ / call the factorial function
         HASIL = Faktorial (bilangan);
        
/ / show results
         cout<<bilangan<<"! ="<<HASIL;
         
    system("PAUSE");
    return EXIT_SUCCESS;
}

OPERATORS
This time we will introduce you with the existing operators in c + + language and its use in the program. C + + language itself offers many operators to make solving certain problems in the program. Operators will be grouped into 4 parts, the assignment operator, unary, binary and ternary. 

Binary Operators

Binary operator is an operator that is used in operations involving two operands. in the study c + +, binary operators grouped into four types, arithmetic operators, logic, relational and bitwise .

Bitwise Operators

Bitwise operators are used to perform operations associated with the manipulation of bits.
 
  
Operator <<

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a,b;
    cout<<"masukan nilai a :"<<endl;
    cin>>a;
    cout<<"masukan nilai b :"<<endl;
    cin>>b;
    cout<<"nilai biner a:"<< (1 << a) <<endl;
    cout<<"nilai biner b:"<< (1 << b) <<endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


catatan kaki:

pemograman c++; budi raharjo;

Tidak ada komentar:

Posting Komentar