10 CONTOH PROGRAM ARRAY PADA C++





/* ---------------------------- */
/* Program Array Satu Dimensi */
/* ---------------------------- */
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
main()
{
int i;
char nama[5][20];
float nilai1[5];
float nilai2[5];
float hasil[5];
clrscr();
for(i=1;i<=2;i++)
{
cout<<"Data Ke - "<<i<<endl;
cout<<"Nama Siswa : "; gets(nama[i]);
cout<<"Nilai MidTest : "; cin>>nilai1[i];
cout<<"Nilai Final : "; cin>>nilai2[i];
hasil[i] = (nilai1[i] * 0.40)+ (nilai2[i] * 0.60);
cout<<endl;
}
cout<<"------------------------------------------";
cout<<"-------"<<endl;
cout<<"No. Nama Siswa Nilai Nilai ";
cout<<"Hasil"<<endl;
cout<<" MidTest Final ";
cout<<"Ujian"<<endl;
cout<<"------------------------------------------";
cout<<"-------"<<endl;
for(i=1;i<=2;i++)
{
cout<<setiosflags(ios::left)<<setw(4)<<i;
cout<<setiosflags(ios::left)<<setw(20)<<nama[i];
cout<<setprecision(2)<<" "<<nilai1[i];
cout<<setprecision(2)<<" "<<nilai2[i];
cout<<setprecision(2)<<" "<<hasil[i]<<endl;
}
cout<<"------------------------------------------";
cout<<"-------"<<endl;
getch();
}



/* ---------------- */
/* Array Dimensi 2 */
/* ---------------- */
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
main()
{
int i, j;
int data_jual[4][4];
clrscr();
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
cout<<"Data Ke - "<<i<<" "<<j<<endl;
cout<<"Jumlah Penjulan : ";
cin>>data_jual[i][j];
}
}
cout<<"Data Penjualan Pertahun"<<endl;
cout<<"-----------------------"<<endl;
cout<<"NO 2001 2002 2003"<<endl;
cout<<"-----------------------"<<endl;
for(i=1;i<=3;i++)
{
cout<<setiosflags(ios::left)<<setw(5)<<i;
for(j=1;j<=3;j++)
{
cout<<setiosflags(ios::right)<<setw(4);
cout<<data_jual[i][j];
cout<<" ";
}
cout<<endl;
}
cout<<"-----------------------"<<endl;
getch();
}




/* ---------------- */
/* Array Dimensi 3 */
/* ---------------- */
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
main()
{
int i, j, k;
int data_jual[2][3][2];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<2;k++)
{
cout<<"Data Tahun Ke - "<<i+1<<endl;
cout<<"Data Ke - "<<j+1<<" "<<k+1<<endl;
cout<<"Jumlah Penjulan : ";
cin>>data_jual[i][j][k];
}
cout<<endl;
}
cout<<endl;
}
cout<<endl;
cout<<"Data Penjualan Pertahun"<<endl;
cout<<"--------------------------------------";
cout<<endl;
cout<<"Tahun Hasil Tahun Penjualan Ke. ";
cout<<endl;
cout<<"Ke. Ke. --------------------";
cout<<endl;
cout<<" 1 2 ";
cout<<endl;
cout<<"--------------------------------------";
cout<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<setiosflags(ios::left)<<setw(11)<<i+1;
cout<<setiosflags(ios::left)<<setw(9)<<j+1;
for(k=0;k<2;k++)
{
cout<<setiosflags(ios::right)<<setw(5);
cout<<data_jual[i][j][k];
cout<<" ";
}
cout<<endl;
}
cout<<endl;
}
cout<<"--------------------------------------";
cout<<endl;
getch();
}




#include<constream.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
long double input();
void main()
{
clrscr();
int a,p,e,y;
cout<<"Masukan Jumlah Mahasiswa : ";
a=input();
char nama[50][25],ulang,cari[25],m,n,*swap;
long double nilai[50],tmp;
for(int b=0;b<a;b++)
  {
  cout<<"Masukan Nama Ke ["<<b+1<<"] \t: ";gets(nama[b]);
  cout<<"Masukan Nilai "<<nama[b]<<" \t: ";nilai[b]=input();
  }
clrscr();
cout<<"Data Nilai (Sebelum di Sorting)\n";
cout<<"NO.  NAMA \t\tNILAI\n\n";
for(int c=0;c<a;c++)
  {
  cout<<c+1<<"   "<<nama[c];
  gotoxy(25,4+c);cout<<nilai[c]<<endl;
  }
for(int f=0;f<a-1;f++)
  {
  for(int g=f+1;g<a;g++)
    {
    if(nilai[f]<nilai[g])
      {
      tmp=nilai[f];
      nilai[f]=nilai[g];
      nilai[g]=tmp;
      strcpy(swap,nama[f]);
      strcpy(nama[f],nama[g]);
      strcpy(nama[g],swap);
      }
    }
  }
gotoxy(40,1);cout<<"Data Nilai (Setelah di Sorting)\n";
gotoxy(40,2);cout<<"NO.  NAMA \t\tNILAI\n\n";
for(int h=0;h<a;h++)
  {
  gotoxy(40,4+h);cout<<h+1<<"   "<<nama[h];
  gotoxy(65,4+h);cout<<nilai[h];
  cout<<endl;
  }
reset:
{
cout<<"\nCari Nilai Berdasarkan Nama : ";
gets(cari);
//n=strlen(cari);
p=0;
for(int d=0;d<a;d++)
  {
 // m=strlen(nama[d]);
 // if(m==n)
  y=strcmp(cari,nama[d]);
  if(y==0)
    {
    e=d;
    p=p+1;
    }
  }
if(p!=0)
{
cout<<"Data Yang Anda Cari\n";
cout<<"Nama\t: "<<nama[e];
cout<<"\nNilai\t: "<<nilai[e];
}
else
{
cout<<"Maaf, Nama yang Anda Masukan Tidak Terdaftar";
}
cout<<"\nCari Lagi (Y/T)? :";
cin>>ulang;
}
if(ulang=='y'||ulang=='Y')
{
goto reset;
}
else
{
exit(1);
}
getch();
}
long double input()
{
long double ab;
cin>>ab;
return ab;
}




#include<constream.h>

void main()
{

int a[5];
int x,y;
y=1;
clrscr();

 for (x=0;x<5;x++)
 {
 cout<<” Input Angka “<<” : “;cin>>x;
 }
 for (x=0;x<5;x++)
 {
 cout<<”\n Hasil : “;cout<<x;
 }
getch();
}



#include<stdio.h>
#include<conio.h>
#include<iostream.h>

main()
{
char a[3][5]={"ABCDE","FGHIJ","KLMNO"};
char b[3][5];
int i,j;

cout<<"\3 \3 \3 \3 \3 \3 \3 \3 \3"<<endl;
cout<<"Array Dua Dimensi"<<endl;
cout<<"\3 \3 \3 \3 \3 \3 \3 \3 \3"<<endl;
for(i=0;i<=2;i++)
{
 cout<<endl;
for(j=0;j<=4;j++)
 {
  b[i][j]=a[i][j];
  cout<<" "<<b[i][j];
 }
}
getch();
}





#include <iostream.h>
#include <conio.h>
void main()
{
clrscr ();
 int A[5];
 int i;

 for(i=0;i<5;i++)
 {
 cout<<"Masukkan Data "<<i<<"="; cin>>A[i];
 }
   for(i=0;i<5;i++)
  {
  cout<<endl<<"array ke-"<<i<<" -> "<<A[i];
  }     getch () ;
}




#include
#include
void main()
{
int data[5] = {4, 1, 0, -9, 8};
int elemen;
clrscr();
// tampilkan data
for (elemen=0;elemen <= 4;elemen++)
{
cout << “Data ke – “ << elemen << “: “ << data[elemen];
}
}



  #include <iostream.h>
#include <conio.h>

int main ()
{
   char c;
   do
   {
      int a=0, b=1, n, deret;


   cout<<"========================================================"<<endl;
   cout<<"           BILANGAN FIBONACCI\n";
   cout<<"========================================================"<<endl;
   cout<<"\n\n";
   cout<<"Masukan Jumlah Bilangan Fibonacci Yang Diinginkan : ";
   cin>>deret;
   cout<<"\n";
   cout<<"========================================================"<<endl;
      cout<<"Hasil Bilangan Fibonacci : ";

      for(int i=0;i<=deret;i++)
      {
            if(i==1)
                  cout<<b<<" ";
            else if(i==0)
                  cout<<a<<" ";
            else
            {
                  n = b + a;
                  a = b;
                  b = n;
                  cout<<n<<" ";
            }
      }

      cout<<endl;
      cout<<"\n";
   cout<<" Ulangi lagi ?? Tekan y untuk iya ";
   cin>>c;
   clrscr();
   }
   while (c=='y' || c=='Y');
      getch();

}





//array dua dimensi
#include<iostream.h>
#include<conio.h>
main()
{
int matrix[3][3];
int i,j;

for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<"Masukkan angka pada baris ke "<<i<<" kolom ke "<<j<<" : ";
cin>>matrix[i][j];
}
cout<<endl;
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
getch();
}
Previous
Next Post »

1 komentar:

Click here for komentar
April 27, 2020 at 11:14 PM ×

infonya sangat bermanfaat bagi ane
isolasi double tape

Congrats bro Shikamaru Nara you got PERTAMAX...! hehehehe...
Reply
avatar