Friday, October 19, 2018

17/10/2018 - Pointers & Array

Today I learn about pointers and array in C programming. Pointers is a variable that store address of another variable. To make a variable become a pointer, just add * (asterisk symbol) before the variable's name. To indicate the address of a variable, we can use & (ampersand symbol). * means "content of"; & means "address of"
The syntax for Pointer :
<data_type> *<pointer_name>;

To save another address of pointer in another variable, you can add another * (asterisk symbol)
Syntax :
<data_type> **<pointer_name> (double pointer)
<data_type> ***<pointer_name> (triple pointer)
<data_type> ****<pointer_name> (quadple pointer)
etc

bad example of not using pointer:
int angka1 = 3;
int angka2 = angka1;
int angka3 = angka2 = angka1;
int angka4 = angka3 = angka2 = angka1;
printf("%d", angka4); --> print = 3

example on using pointer :
int angka1 = 3;
int *angka2;
int *angka3;
int *angka4;

angka2 = &angka1;
angka3 = &angka1;
angka4 = &angka1;

printf("%d", angka2); --> print = 3

if these variable changed:
angka2 = &angka1;
angka3 = &angka2;
angka4 = &angka3;
it can cause error.
To make the program didn't error, we must add a pointer to pointer :
int *angka2;
int **angka3;
int *** angka4;

Array is data saved in a certain structure that can be used individually or as a group.
Syntax of array :
type array_name [dimensional_value];
sometimes array can also have its own element, the structure becomes:
type array_name [dimensional_value] = {element};

Accessing an array :
example: to access an element i = 2 in an array, can use :
*(A+2) (using a pointer) or A[2] (using an array)

Syntax of array according to its dimension:
One Dimensional Array = type array_name [row];
Two Dimensional Array = type array_name [row][column];
Three Dimensional Array = type array_name [row][column][depth];

Array of pointer = type *array_name[dimensional_value];

Array of character that ended with null character ("\0") also called string.
String always wrote between double quote (" ").
Difference between character and string is character use only single quote (' ') and can only contain one character when string use double quote (" ") and can contain many character.

To manipulate a string in c programming, we need to include <string.h> in Standard  Library Function.

Thursday, October 11, 2018

10/10/2018 - Program Control: Repetition

What I learn from today class are repetition in programming to control the program. Repetition in programming can also be named looping or iteration. Go to function can also use to control the program, but could make the program more messy.

There are 3 type of repetition that we learned, it is:
  1. For
  2. While
  3. Do While

The difference between While and Do While is :
  1. While repetition can only make the statement executed when the condition met. It check the condition first, then execute the instruction and repeat the action.
  2. Do While repetition would still execute the statement at least once even when the condition doesn't met. It execute the instruction first, then check the condition and repeat the action.

For Syntax :
for(exp1; exp2; exp3) statement;
or
for(exp1; exp2; exp3) {
     statement 1;
     statement 2;
     . . . . . . . . . .
}

exp1 = Initialization
exp2 = Condition
exp3 = Increment or Decrement

for example :
1. Reverse String
    . . . . . . . . .
    void reverse (char ss[])
    {
          int c, i, j;
          for(i = 0; j = strlen(ss) - 1; i++, j--) {
                 c = ss[i];
                 ss[i] = ss [j];
                 ss[j] = c;
          }
     }
     . . . . . . . . 

2. Create 10 asterisk (*) symbol
    . . . . . . . . . . 
    for(int i = 10; i > 0; i++) {
          printf("*");
    }
    . . . . . . . . . . .

3. Create x*y asterisk (*) symbol
    . . . . . . . . . . . 
    for (int x = 0; x < a; x++) {
          for (int y = 0; y < a; y++) {
                printf("*");
          }
          printf("\n");
     }
     . . . . . . . . . . . .

break function used to break the infinite loop.

Note : to compile the input repeatedly, While repetition can be added before scanf function, provided that conditions are given.