Selasa, 23 Oktober 2018

Pointers and Arrays

Pointers and Arrays

Pointer

Pointer is a variable that store the address of another variable
Example of Pointer :
int number1, *number2;
char word1, *word2;

Pointer to Pointer

Pointer to pointer is variabel that saves another address of a pointer
Example :
int number1, *number2, **number3;
char word1, *word2, **word3, ***word4;


So, what will happen when we add another pointer on a pointer ?
Example :

    number1   =   7;
    number1   =   *number2;
  *number2   =   **number3;
**number3   =   ***number4;

when this happens then any number (1/2/3/4) will become 7 (they will follow the number 1) since number 2 is following number 1 ,and number 3 is following number 2 and so on.



Array

Array is a data saved in a certain structure to be accessed as a group or individually. Some variables saved using the same name distinguish by their index.

Example of Array :
int number[10];

Array can be used to make elements
Example :
int A[   ]={5, 2, -1, 9};

there are two ways to apply "i=2;"
*(A+2) or A[2]

String

String is an array of character that ended with null character ( ‘\0’ or in ASCII = 0)
A Constant String can be linked at compile-time:
  "Hello," " world"
  Similar to:
  "Hello, world"
Example string initialization:
  char s[ ] = "BINUS";  
  Similar to :
  char s[ ] = {'B','I','N','U','S','\0'};

What is the difference between Char and String ?
Character in c written between single quote. Each uses one byte of computer memory
String written in between double quote.






- Thanks for Reading -




Kamis, 11 Oktober 2018

Algorithm and Programming

Repetition

What is Repetition ?

Repetition is one or more instruction which is repeated for a certain amount of time

There's 3 repetition/looping operation :
- for
- while
- do-while


Repetition: FOR

Example Repetition : for
           for (i=1; i<=10; i++) printf("%d\n", i);
                   i1       i2       i3

Explanation :
i1 : initialization
i2 : conditional
i3 : increment or decrement


2 Kinds of Loop :

Infinite Loop : Loop with no stop condition can use "for-loop" by removing all parameters (i1.i2.i3). to end it you need to use break.

Note : break is used to force finish a loop

Nested Loop : Loop in a loop. the repetition operation will start from the inner side loop.


Repetition: WHILE

Example Repetition : while

int i = 1;
while (i<=10) {
           printf("%d\n", i);
           x++; 
       }
}



Repetition: DO-WHILE

Example Repetition: do-while

int counter =0;
do {
           printf("%d", counter);
           ++counter;
} while (counter <=10);



That concludes everything


Thank You.