Skip to content

Instantly share code, notes, and snippets.

@mfrahmat1
mfrahmat1 / case2
Created December 19, 2025 10:07
fix code
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_DATA 1000
// =======================
// STRUCT DATA
// =======================
@mfrahmat1
mfrahmat1 / case.c
Created December 7, 2025 05:29
case jawaban tampil semua nilai siswa, hitung min, max, rata"
#include <stdio.h>
int main() {
// Data nilai siswa
int nilai[5] = {80, 85, 70, 75, 90};
int jumlahSiswa = 5;
int min = nilai[0];
int max = nilai[0];
int total = 0;
// Contoh: Pencarian Biner menggunakan rekursi
#include <iostream>
using namespace std;
int binarySearch(int array[], int low, int high, int x) {
if (high >= low) {
int mid = low + (high - low) / 2;
if (array[mid]
return mid;
if (array[mid] > x)
// Contoh: Deret Fibonacci menggunakan rekursi
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
// Contoh: Faktorial menggunakan iterasi
#include <iostream>
using namespace std;
int faktorialIteratif(int n) {
int hasil = 1;
for (int i = 1; i <= n; ++i) {
hasil *= i;
}
return hasil;
}
// Contoh: Faktorial menggunakan rekursi
#include <iostream>
using namespace std;
int faktorial(int n) {
if (n <= 1) // Kasus dasar
return 1;
else
return n * faktorial(n - 1); // Kasus rekursif
}
int main() {
// Contoh: Fungsi rekursif untuk menampilkan "Hello World" se
#include <iostream>
using namespace std;
void cetakHelloWorld(int n) {
if (n <= 0)
return;
cout << "Hello World" << endl;
cetakHelloWorld(n - 1);
}
@mfrahmat1
mfrahmat1 / selectsort.c
Created November 17, 2025 04:31
Selectsort
// C program for implementation of selection sort
#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
// Assume the current position holds
// the minimum element
int min_idx = i;
@mfrahmat1
mfrahmat1 / merge.c
Created November 17, 2025 04:29
mergesort in c
#include <stdio.h>
#include <stdlib.h>
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r){
int i, j, k;
int n1 = m - l + 1;
@mfrahmat1
mfrahmat1 / quicksort.c
Created November 17, 2025 04:27
Quick sort in C
// C Program to sort an array using qsort() function in C
#include <stdio.h>
#include <stdlib.h>
// If a should be placed before b, compare function should
// return positive value, if it should be placed after b,
// it should return negative value. Returns 0 otherwise
int compare(const void* a, const void* b) {
return (*(int*)a - *(int*)b);
}