IP

Rabu, 02 Mei 2012

CONTOH PROGRAM C++ FUNCTION

Program Di bawah ini saya compile dengan compiler MICROSOFT VISUAL C++ 2008

# include <iostream>
using namespace std ;
void input_matriks(int matriks[5][5])
{             
                int o , p , q , r , matriks1 [5] [5] ; 
                cout << endl ;
                cout << " Masukkan Jumlah Baris  = " ;
                cin >> o ; 
                cout << " Masukkan Jumlah Kolom  = " ; 
                cin >> p ; 
                cout << endl ;
                cout << " Inputkan Data ? " ;
                cout << endl ;
                for ( q = 0 ; q < o ; q++ )       
                                for ( r = 0 ; r < p ; r++ ) {
                                                cout << " Data [ " << ( q + 1 ) << " , " << ( r + 1 ) << " ] = " ; 
                                                cin >> matriks1 [q] [r] ;
                                }
                                cout << endl ; 
                                cout << endl ;
                                for ( q = 0 ; q < o ; q++ ) {
                                                for ( r = 0 ; r < p ; r++ ) {
                                                                cout << matriks1 [q] [r] << " " ; }
                                                cout << endl ;
                                                cout << endl ;
                                }
}
void perkalian_matriks( const int matriks1 [5] [5] , const int matriks2 [5] [5] , int matriks3 [10] [10])
{
                int o = 2 , p = 2 , q , r , s = 2 , t ;
                for ( q = 0 ; q < p ; q++ ) {
                                for ( r = 0 ; r < s ; r++ ) {
                                                matriks3 [q] [r] = 0 ;
                                                for ( t = 0 ; t < o ; t++ ) {
                               matriks3 [q] [r] = matriks3 [q] [r] + ( matriks1 [q] [t] * matriks2 [t] [r] ) ; }
                                }
                }
}
void output_matriks (  const int matriks3 [10][10] )
{
                int p = 2 , q , r , s = 2 ;
                cout << endl ;
                cout << " Hasil Kali Matriks Ke-1 Dengan Matriks Ke-2 " ;
                cout << endl ;
                cout << endl ;
                for ( q = 0 ; q < p ; q++ ) {
                                for ( r = 0 ; r < s ; r++ ) {
                                                cout << matriks3 [q] [r] << " " ;
                                }
                                cout << endl ;
                                cout << endl ;
                }
}
int main ()
{
                int matriks1 [5] [5] , matriks2 [5] [5] , hasil [10] [10] ;
                cout << " Program Perkalian Matriks \n" ; cout << endl ;
                cout << " Masukkan Matriks 1 " ; cout << endl ;
                input_matriks(matriks1) ; cout << endl ;
                cout << " Masukkan Matriks 2 " ; cout << endl ;
                input_matriks(matriks2) ; cout << endl ;
                perkalian_matriks(matriks1,matriks2,hasil) ; cout << endl ;
                output_matriks(hasil) ; cout << endl ;
                return 0 ;
}


               




               



                                                                               

0 komentar:

Posting Komentar

free counters