Wednesday, November 28, 2018

Structures and Unions & Memory Allocation

Here is what i got from this week's session:

Structure is much likely an array, but it runs on a logic called "record". It is used to make an array (two dimensional usually) in order.

Here's an example of how it's done:

struct data{
   char name[100];
   char NIM[10];
   int age;
   char campus[100];
}student[100];

Once we pick a certain index, the data structure will make it easier to find the data connected by each other data inside it.

Example: input 1, it will print out

a student's name, NIM, age, and campus "accordingly".


Function and Recursion

Basically in this session, we are taking about how we separate a program into different modules.
The main module is the one which we usually make as "int main" in C / C++, and there are many which are called "functions".

1. Function

In short programs, this would pretty much be useless. But, in long term and big-shot programs like applications, games, etc., this is so useful. A name of a function represents all of its "functions" which is already made in the initialization before we call the main module / int main.

Example:

int strlen(char s[100]){
     int i = 0;
     while(s[i]!=NULL){
          i++;
     }
     return i;    //this means the result value which will be returned after it is run by the function is i.
}


2. Recursion

Ever heard of loop and repetition right? This is just an implementation of loopings and repetitions inside a function, calling the function itself again and again until it fulfills some certain condition.