Iswansyah (Array)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Nama : Iswansyah

NIM : 5181011007
Tugas : 2

Program
#1. Array Satu dimensi (statis)
#include <iostream>

using namespace std;


main() {
int my_array[6] = {1,23,17,4,5,100};
cout<<"Data ke-1 = "<<my_array[0]<<"n";
cout<<"\nData ke-2 = "<<my_array[1]<<"n";
cout<<"\nData ke-3 = "<<my_array[2]<<"n";
cout<<"\nData ke-4 = "<<my_array[3]<<"n";
cout<<"\nData ke-5 = "<<my_array[4]<<"n";
cout<<"\nData ke-6 = "<<my_array[5]<<"n";
}

Hasil
#2. Mengakses element Array secara dinamis (dinamis)
#include <iostream>

using namespace std;


int main () {
int x [5];
cout<<"Masukkan nilai 1 :";cin>>x[0];
cout<<"Masukkan nilai 2 :";cin>>x[1];
cout<<"Masukkan nilai 3 :";cin>>x[2];
cout<<"Masukkan nilai 4 :";cin>>x[3];
cout<<"Masukkan nilai 5 :";cin>>x[4];

return 0 ;
}

Hasil
#3. Menampilkan element array secara (dinamis)
#include <iostream>
using namespace std;
int main (){
int x [5];
cout<<"Masukkan nilai 1 :";cin>>x[0];
cout<<"Masukkan nilai 2 :";cin>>x[1];
cout<<"Masukkan nilai 3 :";cin>>x[2];
cout<<"Masukkan nilai 4 :";cin>>x[3];
cout<<"Masukkan nilai 5 :";cin>>x[4];
cout<<endl;

cout<<"Nilai ke-1 ="<<x[0]<<endl;


cout<<"Nilai ke-2 ="<<x[1]<<endl;
cout<<"Nilai ke-3 ="<<x[2]<<endl;
cout<<"Nilai ke-4 ="<<x[3]<<endl;
cout<<"Nilai ke-5 ="<<x[4]<<endl;
system("PAUSE");
}

Hasil
#4. Array dengan perulangan (for)
#include <iostream>
using namespace std;
int main () {
int x[5],i;
for(i=0;i<=4;i++)

{
cout<<"Masukkan nilai = "<<i+1<<" : ";
cin>>x[i];
}
return 0;

}Hasil
#5. Menampilkan Array dengan perulanagan
#include <iostream>
using namespace std;
int main () {
int x[5],i;
for(i=0;i<=4;i++)
{
cout<<"Masukkan nilai "<<i+1<<" : "; cin>>x[i];
}
cout<<endl;
for(i=0;i<=4;i++)
{
cout<<"Nilai ke-"<<i+1<<" = "<<x[i]<<endl;
}
cout<<endl;
return 0;
}

Hasil
#6. Mengisi array dengan karakter ( @, %, &)
#include <iostream>
using namespace std;
int main(){
int i;
char nama[5]={'A','d','@','m'};
char nama2[5]="Ad@m";
cout<<"Array per Karakter= ";
for(i=0;i<=4;i++)
{
cout<<nama[i];
}
cout<<endl;
cout<<"Array string ="<<nama2<<endl<<endl;
return 0;
}

Hasil
#7. Array berdimensi 1
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main (){
float nilai[10];
int x,y;
cout<<"Mau berapa data [1..10] : ";cin>>y;

for(x=0;x<y;x++)
{
cout<<"Nilai ke- "<<x+1<<" : ";cin>>nilai[x];
}
cout<<endl;
cout<<setiosflags(ios::fixed);
for(x=0;x<y;x++)
{
cout<<"Nilai ke- "<<setprecision(2)<<x+1<<" :"<<nilai[x]<<endl;
}
cout<<endl;
return 0;
}
Hasil
#8. Array Bertipe data String
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc,char *argv[]) {
char nama[2][2][10];
int i,j;
for(i=0;i<2;i++) {
for(j=0;j<2;j++)
{
cout<<"nama["<<i+1<<"] ["<<j+1<<"] : ";cin>>nama[i][j];
}
cout<<endl;
}
for(i=0;i<2;i++) {
for(j=0;j<2;j++)
{
cout<<"nama["<<i+1<<"]["<<j+1<<"] : ";cin>>nama[i][j];
}
}
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Hasil
#9. Menampilkan array
#include <iostream>
using namespace std;
int main(void) {
int bilangan[2][2];
bilangan[0][0] = 100;
bilangan[0][1] = 101;
bilangan[1][0] = 110;
bilangan[1][1] = 111;
cout<< ("Isi array bilangan : n");
cout<< ("n",bilangan[0][0],bilangan[0][1]);
cout<< ("n",bilangan[1][0],bilangan[1][1]);
return 0;
}

Hasil
#10. Penjumlahan matrix dua dimensi dengan array
#include<iostream>
#include<conio.h>
using namespace std;
int i,x;
int matriks_A[2][2],
matriks_B[2][2],
matriks_C[2][2];
char ulang;
main()
{
cout<< "nPenjumlahan 2 MatriksOrdo 2x2 nn"; do {
//Input data matriks A
cout<< "nData matriks A n";
for(i=0; i<2; i++) {
for(x=0; x<2; x++) {
cout<< "["<<i<<"]["<<x<<"] :"; cin>> matriks_A[i][x];
}
}

cout<<"n.__________________________________.n";
//Input data matriks B
cout<< "nData matriks B n";
for(i=0; i<2; i++) {
for(x=0; x<2; x++) {
cout<< "["<<i<<"]["<<x<<"] : "; cin>> matriks_B[i][x]; }}
}
}

cout<<"n.__________________________________.n";
//Output hasil penjumlahan
Matriks A + Matriks B
cout<< "nMatriks A + Matriks B:"<<endl; for(i=0; i<2; i++) {
for(x=0; x<2; x++) {
matriks_C[i][x]=matriks_A[i][x] +matriks_B[i][x];
cout<<"t"<<matriks_C[i][x];
}
cout<<endl;
cout<<"n.__________________________________.n";
cout<< "nHitung lagi ? [y/t] : ";cin>>ulang;
}
while(ulang!='t');
cout<< "nnTerima Kasih nn"<<endl;
getch();
}

Hasil
#11. Penjumlahan Array dua Dimensi
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
int a[2][2];
int b[2][2];
int c[2][2];
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++) {
cout<<"a["<<i+1<<"]["<<j+1<<"] : ";cin>>a[i][j];
}
}

cout<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++) {
cout<<"b["<<i+1<<"]["<<j+1<<"] : ";cin>>b[i][j];
}
}

cout<<endl;
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
cout<<"c["<<i+1<<"]["<<j+1<<"] : " <<c[i][j]<<endl;
}
cout<<endl;
}

cout<<endl;
system("PAUSE");
}

Hasil
#12. Pengurangan matriix dua dimensi dengan array
#include <iostream>
#include <conio.h>
using namespace std;
int main(void)
{
double a[10][10],b[10][10],c[10][10];
int CC,i,j,k;
cout<<endl<<" Elemenmatriks A : "<<endl;
for (i=1; i<=2;i++) {
for (j=1;j<=2;j++){
cout<<" Elemen matrik Abaris ke-"<<i<<" kolom ke- "<<j<<":
";cin>>a[i][j];
}
}
cout<<endl<<" Elemenmatriks B : "<<endl;
for (i=1; i<=2;i++){
for (j=1;j<=2;j++){
cout<<"baris ke-"<<i<<" kolom ke-"<<j<<": ";cin>>b[i][j];
}

}
cout<<endl;
for (i=1;i<=2;i++){
for (j=1;j<=2;j++){
c[i][j]=0;
for (k=1;k<=2;k++){
CC=a[i][k]*b[k][j];
c[i][j]=c[i][j]+CC;
}
}
}
cout << "Hasil pengurangan matriks: \n";
for (i=1;i<=2;i++){
for (j=1;j<=2;j++){
cout<<c[i][j]<<"\t";
}
cout << endl;
}
getch();
}

Hasil
#13. Perkalian matrix dua dimensi dengan array
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char
*argv[])
{
int a[2][2],b[2][2],m,n;
for(m=0;m<2;m++)
{
for(n=0;n<2;n++) {
cout<<"masukkan nilaimatriks a("<<m<<","<<n<<"):";cin>>a[m][n];
}
}
cout<<"\n=====================================================\n";
for(m=0;m<2;m++)
{
for(n=0;n<2;n++) {
cout<<"masukkan nilai matriks b("<<m<<","<<n<<"):";cin>>b[m][n];
}
}
cout<<"C:n";
for(m=0;m<2;m++)
{
for(n=0;n<2;n++) {
cout<<setw(3)<<(a[m][0]*b[0][n]+ a[m][1]*b[1][n]);
cout<<'n';
cout<<"\n";
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

Hasil
#14. Matrix transpose dengan array
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
main(){
int A[10][10],b,c,baris,kolom;
char pil;
do {
cout<<" ==========PROGRAM TRANSPOSE
MATRIKS========="<<endl;
cout<<"Jumlah Baris : ";cin>>baris;
cout<<"Jumlah Kolom : ";cin>>kolom;
cout<<endl;
for (b=0;b<baris;b++) {
for (c=0;c<kolom;c++) {
cout<<"Matriks ["<<b+1<<","<<c+1<<"] = ";cin>>A[b][c];
}
}
cout<<endl<<endl;
cout<<"Matriks Pertama :"<<endl<<endl;
for (b=0;b<baris;b++) {
for (c=0;c<kolom;c++) {
cout<<" "<<A[b][c]<<" ";
}
cout<<endl<<endl;
}
cout<<" Matriks Transpose :"<<endl<<endl;
for (b=0;b<kolom;b++) {
for (c=0;c<baris;c++) {
cout<<" "<<A[c][b]<<" ";
}
cout<<endl;
cout<<endl;
}
getch();
cout<<" Ingin Mengulang Program? (Y/N) : ";
cin>>pil;
}
while(pil=='Y'||pil=='y');
}

Hasil
#15. Array dengan Parameter fungsi
#include <cstdlib>
#include <iostream>
using namespace std;
void input_array(int bil[], int
banyak_data){
int i;
for(i=0;i<banyak_data;i++) {
cout<<"Nilai bil ["<<i+1<<"] : ";cin>>bil[i];
}
}
int jumlah_elemen(int bil[],int banyak_data) {
int i, jumlah=0;
for(i=0;i<banyak_data;i++) {
jumlah=jumlah+bil[i];
}
return jumlah;
}
int main() {
int bil[100]; //maksimalelemen
int banyak_data;
int jumlah;
cout<<" Masukkan banyak elemen yang di inginkan : ";cin>>banyak_data;
cout<<endl;

//memanggil void
input_array;
input_array(bil,banyak_data);

//memanggl fungsijumlah_elemen
jumlah=jumlah_elemen(bil,banyak_data);
cout<<endl;
cout<<"Hasil penjumlahan elemen = "<<jumlah<<endl<<endl;
return 0;
}

Hasil

#16. Array 3 dimensi


#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc,char *argv[]) {
char nama [2][2][10];
int i,j;
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
cout<<"nama["<<i+1<<"]["<<j+1<<"] : ";cin>>nama[i][j];
}
cout<<endl;
}
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
cout<<"nama["<<i+1<<"]["<<j+1<<"] : ";cin>>nama[i][j];
}
}

cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Hasil

You might also like