membuat menu di c++

Membuat Menu Dengan C++
Diposting oleh: suta32 at 7:30 AM

Menu dalam setiap program sangat penting keberadaannya. Dengan menu memungkinkan orang untuk memilih beberapa fungsi yang ada dalam satu pprogram. Berikut ini adalah contoh penggunaan menu dalam C++. Program berikut ini digunakan untuk menghitung luas bangun datar.

User dapat memilih menu untuk menghitung luas bangun datar dengan memilih menu yang sesuai, diantaranya:

1. Menghitung Luas Segitiga
2. Menghitung Luas Persegi
3. Menghitung Luas Lingkaran

Berikut ini adalah tampilan dari programnya:


Berikut ini adalah kode programnya:


//Contoh Membuat Menu
#include
#include

//Fungsi untuk menghitung luas Segitiga
float luasSegiTiga(){
clrscr();
float a, t, L;
cout<<"Menghitung Luas Segitiga\n";
cout<<"============================\n";
cout<<"Masukkan Alas : ";cin>>a;
cout<<"Masukkan Tinggi : ";cin>>t;
cout<L=(a * t)/2;
cout<<"Luas : "<getch();
}

//Fungsi untuk menghitung luas Persegi
int luasPersegi(){
clrscr();
int s, L;
cout<<"Menghitung Luas Persegi\n";
cout<<"============================\n";
cout<<"Masukkan Sisi : ";cin>>s;
cout<L= s * s;
cout<<"Luas : "<getch();
}

//Fungsi untuk menghitung luas Lingkaran
float luasLingkaran(){
clrscr();
float r, L;
const float phi=22/7;
cout<<"Menghitung Luas Lingkaran\n";
cout<<"============================\n";
cout<<"Masukkan Jari-Jari : ";cin>>r;
cout<L=phi * r * r;
cout<<"Luas : "<getch();
}

//Fungsi Utama
void main(){
int pil;
do {
//Membuat Menu
clrscr();
cout<<"Menghitung Luas Bangun Datar\n";
cout<<"============================\n\n";
cout<<"1. Menghitung Luas Segitiga\n";
cout<<"2. Menghitung Luas Persegi\n";
cout<<"3. Menghitung Luas Lingkaran\n";
cout<<"4. Keluar\n\n";
cout<<"============================\n";
cout<<"Masukkan Pilihan Anda : ";cin>>pil;

//Mengeksekusi pilihan user
switch (pil){
case 1: //jika pilihan=1
luasSegiTiga();
break;
case 2: //jika pilihan=2
luasPersegi();
break;
case 3: //jika pilihan=3
luasLingkaran();
break;
case 4: //jika pilihan=4
cout<<"\n\nProgram Selesai. Terima Kasih";
break;
default : //jika pilihan selain 1,2,3,4
cout<<"\n\nMaaf, Pilihan Tidak Tersedia !!!!";
break;
}
getch();
}while (pil!=4);//looping sampai Pilihan User = 4
}


tugas c++

Saturday, December 26, 2009

Multidimensional arrays in C++

Multidimensional arrays can be described as "arrays of arrays". For example, a bidimensional array can be imagined as a bidimensional table made of elements, all of them of a same uniform data type.



jimmy represents a bidimensional array of 3 per 5 elements of type int. The way to declare this array in C++ would be:

int jimmy [3][5];

and, for example, the way to reference the second element vertically and fourth horizontally in an expression would be:
jimmy[1][3]



(remember that array indices always begin by zero).

Multidimensional arrays are not limited to two indices (i.e., two dimensions). They can contain as many indices as needed. But be careful! The amount of memory needed for an array rapidly increases with each dimension. For example:

char century [100][365][24][60][60];

declares an array with a char element for each second in a century, that is more than 3 billion chars. So this declaration would consume more than 3 gigabytes of memory!

Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a simple array just by putting a factor between its indices:
int jimmy [3][5];   // is equivalent to

int jimmy [15]; // (3 * 5 = 15)


With the only difference that with multidimensional arrays the compiler remembers the depth of each imaginary dimension for us. Take as example these two pieces of code, with both exactly the same result. One uses a bidimensional array and the other one uses a simple array:


#define WIDTH 5
#define HEIGHT 3

int jimmy [HEIGHT][WIDTH];
int n,m;

int main ()
{
for (n=0;n for (m=0;m {
jimmy[n][m]=(n+1)*(m+1);
}
return 0;
}

None of the two source codes above produce any output on the screen, but both assign values to the memory block called jimmy in the following way:



We have used "defined constants" (#define) to simplify possible future modifications of the program. For example, in case that we decided to enlarge the array to a height of 4 instead of 3 it could be done simply by changing the line:
#define HEIGHT 3 

to:
#define HEIGHT 4