Posts

Sorting and Searching Sorting Sorting is used to speed up searching in a list of objects. There are two types of sorting : Ascending and Descending. Methods of sorting : Bubble Sort Bubble sort compares a variable with their neighbour and swap them if necessary. Example : void bubbleSort(int arr[], int n){    int i, j, temp;      for(i = 1; i < n; i++){         for(j = n - 1; j >= i; j--){               if(arr[j-1] > arr[j]){                   temp = arr[j-1];                   arr[j-1] = arr[j];                   arr[j] = arr[j-1];               }          }    } Selection Sort for(i = 0; i < n - 1; i++){    temp = i;    for(j = i+1; j < n; j++...
Structure and file processing Structure Structure is a data type used to store a group of data. It is user defined and the group of data can be varied. Structure declaration : struct name_structure{    data_type name    ... }; Example : struct mahasiswa{    int NIM;    char nama[21];    char jurusan[51]; }agus; To call an element of a structure, use dot operator : agus.NIM Structure can also be used in conjunction with array. To declare structrure with array, just declare it as usual. struct mahasiswa{    int NIM;    char nama[21];    char jurusan[51]; }data[20]; Example of storing structured array: void storeStruct(){    struct mahasiswa{       int NIM;       char nama[21];       char jurusan[51];    }data[20];        int i;    for(i = 0; i < 20; i++){     ...
Function and Recursion Function Function in C is related to modular programming. To implement modules in C, we use function to call sub-program of a group of statements when necessary. In C, there are two types of function. First one is library function(printf, scanf) and the second one is user-defined function(self-defined). The construct of user-defined function is : return-value-type function name(parameter-list){           statements}; Example: int even(int x){    if(x % 2 == 0){           return 1;    }    else{           return 0;    } }; int main(){    #include<stdio.h>    int x;      scanf("%d", &x);    even(x);    if(even(x) == 1){         printf("It's even\n");    }    else{         printf("It's not even\n); ...
Array & Pointer In this session, I learned about array & pointer. Array Array is used to store the same type of data in a group. It means that all elements in the array have the same data type. To differentiate the elements in an array, they have an identifier called index. In C, index starts from 0. For example, if I want to make 3 integer variables: n1, n2, and n3; I can put those variables into an array n[index]. Here is an example of inputting elements in array : int n[5] = {1,2,3,4,5}; -> this means that I can store 5 elements in an array called n. To access the elements in an array, I just need to type the name of the array followed by the index, like n[0] = 1, n[1] = 2 and so on. I can also use pointer to access the elements(*(A+1) = 2). The example above is what we called 1D array. There are also different dimensional array like 2D and 3D. For 2D, it is the same like 1D except that 2D array have rows(array[row][col]). Example of 2D array : int n[3][4] :...
FOR Function in c Last session, i have learned how to use for loop function. For loop function can be used to repeat statements under given conditions. For example, if i want to print 12345 i don't need to use printf 5 times. Instead, i can use for loop function to print 12345. Here's the code for using the function : for( int i=1; i <= 5; i++){   printf("%d", i); } The code above will print 12345 because the condition stated to print i if i is 5 or below. For loop makes coding more efficient and you can also use it to print patterns like triangle or pyramid.