One-dimensional array is an array that is used to display data in the form of straight oronly use one index alone while the two-dimensional array of display data in a box or withtwo indices.
In today's program I will combine these two methods. 2-dimensional array used to store data and one-dimensional array to display the results from the sum and the average of the data.
For more details see the program and the results of running the program below :
#include <cstdlib>
#include <iostream>
using namespace std;
class penjualan{
public:
friend ostream& operator<<(ostream&, penjualan&);
void cetak_nama();
void masuk_data();
void hitung();
penjualan();
private:
char a[50],b[50],c[50],d[50],e[50];
int data[100][100];
int total[50],jumlah[50];
double rata[50];
};
penjualan::penjualan(){
cout<<"\tPROGRAM MENGHITUNG PENJUALAN KOMPUTER"<<endl;
cout<<"\t================OLEH================="<<endl;
cout<<"\t============ARIF BUDIARTI============"<<endl;
cout<<"\t===============10018021=============="<<endl;
cout<<"\t================2011================="<<endl;
cout<<"\t====================================="<<endl;
}
void penjualan::cetak_nama(){
int k=1;
cout<<"Masukkan Nama "<<k<<"=";cin>>a;
cout<<"Masukkan Nama "<<k+1<<"=";cin>>b;
cout<<"Masukkan Nama "<<k+2<<"=";cin>>c;
cout<<"Masukkan Nama "<<k+3<<"=";cin>>d;
cout<<"Masukkan Nama "<<k+4<<"=";cin>>e;
}
void penjualan::masuk_data(){
for(int i=0; i<5; i++)
for(int j=0; j<5; j++){
cout<<"Masukkan data ke ["<<i+1<<"] ["<<j+1<<"] :";
cin>>data[i][j];
}
}
void penjualan::hitung(){
for(int i=0; i<5; i++){
total[i]=0;
jumlah[i]=0;
for(int j=0; j<5; j++){
total[i]=total[i]+data[i][j];
jumlah[i]=jumlah[i]+data[j][i];
}}
rata[0]=0;
jumlah[5]=0;
for(int i=0; i<5; i++){
rata[i]=(rata[i]+(jumlah[i]/5));
jumlah[5]=jumlah[5]+jumlah[i];
}
}
ostream& operator<<(ostream& out,penjualan& m){
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| No\t|| Nama\t\t|| Jan\t|| Feb\t|| Mar\t|| Apr\t|| Mei\t|| Total ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| 1."<<"\t|| ";
for(int i=0; i<strlen(m.a); i++){
out<<m.a[i];
}
out<<"\t\t|| ";
for(int i=0; i<5; i++){
out<<m.data[0][i]<<"\t|| ";
}
out<<m.total[0]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| 2."<<"\t|| ";
for(int i=0; i<strlen(m.b); i++){
out<<m.b[i];
}
out<<"\t|| ";
for(int i=0; i<5; i++){
out<<m.data[1][i]<<"\t|| ";
}
out<<m.total[1]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| 3."<<"\t|| ";
for(int i=0; i<strlen(m.c); i++){
out<<m.c[i];
}
out<<"\t|| ";
for(int i=0; i<5; i++){
out<<m.data[2][i]<<"\t|| ";
}
out<<m.total[2]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| 4."<<"\t|| ";
for(int i=0; i<strlen(m.d); i++){
out<<m.d[i];
}
out<<"\t\t|| ";
for(int i=0; i<5; i++){
out<<m.data[3][i]<<"\t|| ";
}
out<<m.total[3]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| 5."<<"\t|| ";
for(int i=0; i<strlen(m.e); i++){
out<<m.e[i];
}
out<<"\t\t|| ";
for(int i=0; i<5; i++){
out<<m.data[4][i]<<"\t|| ";
}
out<<m.total[4]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| \t|| total\t|| ";
for(int i=0; i<5; i++){
out<<m.jumlah[i]<<"\t|| ";
}
out<<m.jumlah[5]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| \t|| rata2\t|| ";
for(int i=0; i<5; i++){
out<<m.rata[i]<<"\t|| ";
}
out<<"Total ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
}
Running Result :
In today's program I will combine these two methods. 2-dimensional array used to store data and one-dimensional array to display the results from the sum and the average of the data.
For more details see the program and the results of running the program below :
#include <cstdlib>
#include <iostream>
using namespace std;
class penjualan{
public:
friend ostream& operator<<(ostream&, penjualan&);
void cetak_nama();
void masuk_data();
void hitung();
penjualan();
private:
char a[50],b[50],c[50],d[50],e[50];
int data[100][100];
int total[50],jumlah[50];
double rata[50];
};
penjualan::penjualan(){
cout<<"\tPROGRAM MENGHITUNG PENJUALAN KOMPUTER"<<endl;
cout<<"\t================OLEH================="<<endl;
cout<<"\t============ARIF BUDIARTI============"<<endl;
cout<<"\t===============10018021=============="<<endl;
cout<<"\t================2011================="<<endl;
cout<<"\t====================================="<<endl;
}
void penjualan::cetak_nama(){
int k=1;
cout<<"Masukkan Nama "<<k<<"=";cin>>a;
cout<<"Masukkan Nama "<<k+1<<"=";cin>>b;
cout<<"Masukkan Nama "<<k+2<<"=";cin>>c;
cout<<"Masukkan Nama "<<k+3<<"=";cin>>d;
cout<<"Masukkan Nama "<<k+4<<"=";cin>>e;
}
void penjualan::masuk_data(){
for(int i=0; i<5; i++)
for(int j=0; j<5; j++){
cout<<"Masukkan data ke ["<<i+1<<"] ["<<j+1<<"] :";
cin>>data[i][j];
}
}
void penjualan::hitung(){
for(int i=0; i<5; i++){
total[i]=0;
jumlah[i]=0;
for(int j=0; j<5; j++){
total[i]=total[i]+data[i][j];
jumlah[i]=jumlah[i]+data[j][i];
}}
rata[0]=0;
jumlah[5]=0;
for(int i=0; i<5; i++){
rata[i]=(rata[i]+(jumlah[i]/5));
jumlah[5]=jumlah[5]+jumlah[i];
}
}
ostream& operator<<(ostream& out,penjualan& m){
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| No\t|| Nama\t\t|| Jan\t|| Feb\t|| Mar\t|| Apr\t|| Mei\t|| Total ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| 1."<<"\t|| ";
for(int i=0; i<strlen(m.a); i++){
out<<m.a[i];
}
out<<"\t\t|| ";
for(int i=0; i<5; i++){
out<<m.data[0][i]<<"\t|| ";
}
out<<m.total[0]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| 2."<<"\t|| ";
for(int i=0; i<strlen(m.b); i++){
out<<m.b[i];
}
out<<"\t|| ";
for(int i=0; i<5; i++){
out<<m.data[1][i]<<"\t|| ";
}
out<<m.total[1]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| 3."<<"\t|| ";
for(int i=0; i<strlen(m.c); i++){
out<<m.c[i];
}
out<<"\t|| ";
for(int i=0; i<5; i++){
out<<m.data[2][i]<<"\t|| ";
}
out<<m.total[2]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| 4."<<"\t|| ";
for(int i=0; i<strlen(m.d); i++){
out<<m.d[i];
}
out<<"\t\t|| ";
for(int i=0; i<5; i++){
out<<m.data[3][i]<<"\t|| ";
}
out<<m.total[3]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| 5."<<"\t|| ";
for(int i=0; i<strlen(m.e); i++){
out<<m.e[i];
}
out<<"\t\t|| ";
for(int i=0; i<5; i++){
out<<m.data[4][i]<<"\t|| ";
}
out<<m.total[4]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| \t|| total\t|| ";
for(int i=0; i<5; i++){
out<<m.jumlah[i]<<"\t|| ";
}
out<<m.jumlah[5]<<"\t ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
out<<"|| \t|| rata2\t|| ";
for(int i=0; i<5; i++){
out<<m.rata[i]<<"\t|| ";
}
out<<"Total ||"<<endl;
out<<"----------------------------------------------------------------------------"<<endl;
}
Running Result :