Berikut adalah Program penjumlahan array 2 dimensi bilangan prima dan Structure dengan menggunakan C++
C++
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int pilih;
void kembalimenu1();
void kembalimenu2();
void kembalimenu3();
void kembalimenu4();
void kembalimenu5();
void exit();
void satu()
{
int j,k,l,m;
system("cls");
cout<<"Matrix A"<<endl;
cout<<"Input Row: ";
cin>>j;
cout<<"Input Coloumn: ";
cin>>k;
int matrik_a[j][k];
for(int bar=0;bar<j;bar++){
for(int kol=0;kol<k;kol++){
cout<<"A["<<bar+1<<"]["<<kol+1<<"] = ";
cin>>matrik_a[bar][kol];
}
}
cout<<endl;
for(int bar=0;bar<j;bar++){
for(int kol=0;kol<k;kol++){
cout<<matrik_a[bar][kol]<<"\t";
}
cout<<endl<<endl;
}
cout<<"Matrix B"<<endl;
cout<<"Input Row: ";
cin>>l;
cout<<"Input Coloumn : ";
cin>>m;
int matrik_b[l][m];
for(int bar=0;bar<l;bar++){
for(int kol=0;kol<m;kol++){
cout<<"B["<<bar+1<<"]["<<kol+1<<"] = ";
cin>>matrik_b[bar][kol];
}
}
cout<<endl;
for(int bar=0;bar<l;bar++){
for(int kol=0;kol<m;kol++){
cout<<matrik_b[bar][kol]<<"\t";
}
cout<<endl<<endl;
}
if(j == l,k == l)
{
cout<<"Amount Between Matrix A & Matrix B"<<endl;
int matrik_c[l][m];
for(int bar=0;bar<l;bar++)
{
for(int kol=0;kol<m;kol++)
{
matrik_c[bar][kol]=matrik_a[bar][kol]+matrik_b[bar][kol];
}
}
cout<<endl;
for(int bar=0;bar<l;bar++)
{
for(int kol=0;kol<m;kol++)
{
cout<<matrik_c[bar][kol]<<"\t";
}
cout<<endl<<endl;
}
}
else
{
cout<<" Baris dan Kolom Tidak Bisa Dioperasikan";
}
kembalimenu1();
}
void dua()
{
int a,b,x,y,z;
system("cls");
cout<<"Masukkan banyak data : ";
cin>>x;
for(a=x;a>=1;a--)
{
y=0;
cout<<endl;
for(b=1;b<=a;b++)
{
cout<<(b*2)-1;
y=y+(b*2)-1;
if(b<a)
{
cout<<" + ";
}
else{
cout<<" = "<<y;
}
}
cout<<endl;
}
for(a=2;a<=x;a++)
{
z=0;
cout<<endl;
for(b=1;b<=a;b++)
{
cout<<(b*2)-1;
z=z+(b*2)-1;
if(a>b)
{
cout<<" + ";
}
else{
cout<<" = "<<z;
}
}
cout<<endl;
}
kembalimenu2();
}
void tiga()
{
int batas,x,y,cek,z;
system("cls");
cout<<"Masukkan batas dari deret bilangan prima ";
cin>>batas;
cout<<"\nDeret bilangan prima sampai angka "<<batas<<" adalah :"<<endl<<endl;
for (x=2;x<=batas;x++)
{
cek=0;
for (y=2;y<x;y++)
{
if(x%y==0)
{
cek=1;
}
}
if(cek==0)
{
cout<<x<<" ";
}
if(x<batas)
{
cout<<" + ";
}
else
{
cout<<" = "<<x+6<<" ";
}
}
kembalimenu3();
}
void empat()
{
system("cls");
struct stud
{
char name_stud[3][10];
int id_stud[5];
char prodi[5][10];
};
stud stud1;
struct scr
{
int mid[5],final[9],assign[10],nilai[10];
char grade[40];
};
scr scr1;
struct lect
{
int id_lect[10];
char name_lect[20][30];
char subject[9][10];
};
lect lect1;
{
int limit;
cout<<"enter the limit =";
cin>>limit ;
for(int x=1;x<=limit;x++)
{
cout<<"\tdata"<<x<<endl;
cout<<"\nid_students=";cin>>stud1.id_stud[x];
cout<<"\nname_student=";cin>>stud1.name_stud[x];
cout<<"\nsubject=";cin>>lect1.subject[x];
cout<<"\ngrade=";cin>>scr1.grade[x];
cout<<"\nid_lect=";cin>>lect1.id_lect[x];
cout<<"\nname_lect=";cin>>lect1.name_lect[x];
}
cout<<"No"<<"\t"<<"id_stud"<<"\t"<<"name_stud"<<"\t"<<"subject"<<"\t"<<"grade"<<"\t"<<"id_lect"<<"\t"<<"name_lect";
cout<<endl;
for(int x=1;x<=limit;x++)
{
cout<<stud1.id_stud[x]<<"\t"<<stud1.name_stud[x]<<"\t"<<lect1.subject[x]<<"\t"<<scr1.grade[x]<<"\t"<<lect1.id_lect[x]<<"\t"<<lect1.name_lect[x];
cout<<endl;
}
}
kembalimenu4();
}
void lima()
{
system("cls");
cout<<"Keluar";
kembalimenu5();
}
main()
{
system("cls");
cout<<"***********Menu Program********"<<endl;
cout<<"1.Penjumlahan Array 2 Dimensi"<<endl;
cout<<"2.Deret"<<endl;
cout<<"3.Bilangan Prima"<<endl;
cout<<"4.Structure"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Masukkan pilihan Anda [1],[2],[3],[4],[5]="<<endl;
cin>>pilih;
if(pilih==1)
{
satu();
}
else if(pilih==2)
{
dua();
}
else if(pilih==3)
{
tiga();
}
else if(pilih==4)
{
empat();
}
else{
cout<<"Terima Kasih Program Anda Selesai";
}
}
void kembalimenu1()
{
char yn;
cout<<endl;
cout<<"Back to Menu[Y/N/T]";
cin>>yn;
if(yn=='Y' || yn=='y'){
main();}
else if(yn=='N' ||yn=='n')
{
}
else if(yn=='T' || yn=='t' )
{
satu();
}
}
void kembalimenu2()
{
char yn;
cout<<endl;
cout<<"Back to Menu[Y/N/T]";
cin>>yn;
if(yn=='Y' || yn=='y'){
main();}
else if(yn=='N' ||yn=='n')
{
}
else if(yn=='T' || yn=='t' )
{
dua();
}
}
void kembalimenu3()
{
char yn;
cout<<endl;
cout<<"Back to Menu[Y/N/T]";
cin>>yn;
if(yn=='Y' || yn=='y'){
main();}
else if(yn=='N' ||yn=='n')
{
}
else if(yn=='T' || yn=='t' )
{
tiga();
}
}
void kembalimenu4()
{
char yn;
cout<<endl;
cout<<"Back to Menu[Y/N/T]";
cin>>yn;
if(yn=='Y' || yn=='y'){
main();}
else if(yn=='N' ||yn=='n')
{
}
else if(yn=='T' || yn=='t' )
{
empat();
}
}
void kembalimenu5()
{
char yn;
cout<<endl;
cout<<"Back to Menu[Y/N/T]";
cin>>yn;
if(yn=='Y' || yn=='y'){
main();}
else if(yn=='N' ||yn=='n')
{
}
else if(yn=='T' || yn=='t' )
{
lima();
}
}
C++Semoga contoh-contoh program di atas dapat membantu Anda dalam memahami penggunaan array 2 dimensi, pengecekan bilangan prima, dan penggunaan struktur dalam bahasa C++. Jika Anda memiliki pertanyaan lebih lanjut, jangan ragu untuk bertanya.