Pertemuan IX

Array Dimensi 1
Contoh kode array dimensi 1 :

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{
int umur[] = {18, 19, 20, 21, 22};

cout<<umur[0]<<endl;
cout<<umur[1]<<endl;   
cout<<umur[4]<<endl;
getch();


}


Array Dimensi 1 dengan for hasl ke bawah :

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{
int umur[] = {18, 19, 20, 21, 22};
int i;

for(i=0;i<=4;i++)
{
cout<<umur[i]<<endl;

}
getch();
}

Array Dimensi 1 Hasil Kesamping

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{
int umur[] = {18, 19, 20, 21, 22};
int i;

for(i=0;i<=4;i++)
{
cout<<umur[i]<<" ";

}
getch();
}

Array Dimensi 1 dengan Input

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{
int umur[5];
int i;

for(i=0;i<=4;i++)
{
cout<<"Masukkan umur ke - "<<i+1<<": ";
cin>>umur[i];

}
getch();
cout<<endl;
cout<<"Hasilnya adalah sebagai berikut:"<<endl;
cout<<endl;
for(i=0;i<=4;i++)
{
cout<<umur[i]<<" ";
}
getch();

}


Array 2 Dimensi
Contoh Array Dimensi 2 sebagai berikut :

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{
int umur[2][3]={{0,1,2},
					{4,5,6}};

cout<<umur[0][0]<<" ";
cout<<umur[0][1]<<" ";
cout<<umur[0][2]<<" ";
cout<<endl;
cout<<umur[1][0]<<" ";
cout<<umur[1][1]<<" ";
cout<<umur[1][2]<<" ";
getch();
}

Array Dimensi 2 dengan For

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{
int umur[2][3]={{0,1,2},
					{4,5,6}};
int i,j;
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
cout<<umur[i][j]<<" ";
}
if(i<=1)
{
cout<<endl;
}


}


getch();
}

Array Dimensi 2 Dengan Input

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{
int umur[2][3];

int i,j;
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
cout<<"umur ke "<<i<<" "<<j<<" :";
cin>>umur[i][j];
}
}




for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
cout<<umur[i][j]<<" ";
}
if(i<=1)
{
cout<<endl;
}


}


getch();
}

Penjumlahan Matrik A dan Matrik B

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{
int A[2][4]={
               {1,2,3,4},
               {5,6,7,8}
					};
int B[2][4]={{9,10,11,12},
               {13,14,15,16}
					};

cout<<"Matrik A"<<endl;
cout<<A[0][0]<<" ";
cout<<A[0][1]<<" ";
cout<<A[0][2]<<" ";
cout<<A[0][3]<<" ";
cout<<endl;
cout<<A[1][0]<<" ";
cout<<A[1][1]<<" ";
cout<<A[1][2]<<" ";
cout<<A[1][3]<<" ";
cout<<endl;
cout<<endl;

cout<<"Matrik B"<<endl;
cout<<B[0][0]<<" ";
cout<<B[0][1]<<" ";
cout<<B[0][2]<<" ";
cout<<B[0][3]<<" ";
cout<<endl;
cout<<B[1][0]<<" ";
cout<<B[1][1]<<" ";
cout<<B[1][2]<<" ";
cout<<B[1][3]<<" ";
cout<<endl;
cout<<endl;

cout<<"Matrik A + Matrik B hasilnya sebagai berikut :"<<endl;
cout<<A[0][0]+B[0][0]<<" ";
cout<<A[0][1]+B[0][1]<<" ";
cout<<A[0][2]+B[0][2]<<" ";
cout<<A[0][3]+B[0][3]<<" ";
cout<<endl;
cout<<A[1][0]+B[1][0]<<" ";
cout<<A[1][1]+B[1][1]<<" ";
cout<<A[1][2]+B[1][2]<<" ";
cout<<A[1][3]+B[1][3]<<" ";
getch();
}

Penjumlahan Matrik A dan Matrik B dengan For

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
main()
{
int A[2][4]={
               {1,2,3,4},
               {5,6,7,8}
					};
int B[2][4]={{9,10,11,12},
               {13,14,15,16}
					};

int i,j;
cout<<"Matrik A"<<endl;
for(i=0;i<=1;i++)
{
	for(j=0;j<=3;j++)
   {
   	cout<<A[i][j]<<" ";
   }
   if(i<=1)
   {
   	cout<<endl;


   }

}
cout<<endl;


cout<<"Matrik B"<<endl;
for(i=0;i<=1;i++)
{
	for(j=0;j<=3;j++)
   {
   	cout<<B[i][j]<<" ";
   }
   if(i<=1)
   {
   	cout<<endl;
   }

}
cout<<endl;

cout<<"Matrik A + Matrik B hasilnya sebagai berikut :"<<endl;
for(i=0;i<=1;i++)
{
	for(j=0;j<=3;j++)
   {
   	cout<<B[i][j]+B[i][j]<<" ";
   }
   if(i<=1)
   {
   	cout<<endl;
   }

}
getch();
}

 

Leave a Reply