Last active
June 7, 2019 11:46
-
-
Save ammarfaizi2/777834df8d2afcf3cd9560f4177b64d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <stdlib.h> | |
void percabangan(); | |
void perulangan(); | |
void array_satu_dimensi(); | |
void array_dua_dimensi(); | |
void subroutine(); | |
char ib[3]; | |
int main(int argc, char *argv[], char *envp[]) | |
{ | |
char buf[10]; | |
short int opt; | |
while (1) { | |
printf( | |
"\ec\n" | |
"===============================\n" | |
"1. Percabangan\n" | |
"2. Perulangan\n" | |
"3. Array 1 Dimensi\n" | |
"4. Array 2 Dimensi\n" | |
"5. Sub Routine\n" | |
"6. Exit program\n" | |
"===============================\n" | |
"Masukkan pilihan: " | |
); | |
#define a(A,B) case A: printf("\ec"); B(); break; | |
fgets(buf, 8, stdin); | |
opt = atoi(buf); | |
switch (opt) { | |
a(1,percabangan) | |
a(2,perulangan) | |
a(3,array_satu_dimensi) | |
a(4,array_dua_dimensi) | |
a(5,subroutine) | |
case 6: return 0; | |
} | |
#undef a | |
} | |
return 0; | |
} | |
#define PAUSE \ | |
printf("Tekan enter untuk melanjutkan!\n"); \ | |
fgets(ib, 2, stdin); | |
void percabangan() { | |
char buf[10]; | |
short int angka; | |
printf("Masukkan angka: "); | |
fgets(buf, 8, stdin); | |
angka = atoi(buf); | |
if (angka < 5) { | |
printf("Angka kurang dari 5\n"); | |
} else if (angka == 5) { | |
printf("Angka sama dengan 5\n"); | |
} else { | |
printf("Angka lebih dari 5\n"); | |
} | |
PAUSE | |
} | |
void perulangan() { | |
int i; | |
for (i = 0; i < 10; ++i) | |
printf("Perulangan %d\n", i); | |
PAUSE | |
} | |
void array_satu_dimensi() { | |
int i, array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
for (i = 0; i < (sizeof(array)/sizeof(array[0])); i++) | |
printf("array[%d] = %d\n", i, array[i]); | |
PAUSE | |
} | |
void array_dua_dimensi() { | |
int i, j, array[][3] = { | |
{1, 2, 3}, | |
{4, 5, 6}, | |
{7, 8, 9} | |
}; | |
for (i = 0; i < (sizeof(array)/sizeof(array[0])); i++) { | |
for (int j = 0; j < (sizeof(array[0])/sizeof(array[0][0])); ++j) | |
printf("array[%d][%d] = %d\n", i, j, array[i][j]); | |
} | |
PAUSE | |
} | |
void subroutine() { | |
printf("Ini subroutine\n"); | |
PAUSE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment