Thursday, October 29, 2015

Code in C: Recursion

Today, i will explain about Recursion~ =w=

What is Recursion??
Recursion is name for function that call themselves.

Actually, Function can call themselves. When they call themselves, they are recursive, that is why it called recursion.

Recursion can be use for looping too.
There is many thing that can use recursion, for example, factorial or fibonacci ,etc.

This is code for factorial~~

#include<stdio.h>
int factorial(int n){
if(n==0||n==1)
    return 1;
 else
    return n*factorial(n-1);

}

int main(){
int a;
scanf("%d",&a);
int b=factorial(a);
printf("%d",b);
}



And this for fibonacci

#include<stdio.h>
int fibonacci(int n){
if(n==0||n==1)
    return 1;
else
    return fibonacci(n-1)+fibonacci(n-2);
}

int main(){
int a;
scanf("%d",&a);
int b=fibonacci(a);
printf("%d",b);
}

So the conclusion is Recursion is function that call themselves~~~ =w=

Wednesday, October 28, 2015

Code in C : Function

I will explain about function in C.~~~~ \(^0^)/

What is function? 

Function is the place where every activity occurs in C, actually we have been using function when we use C programming language. Because "int main () {}" that we always use is a function. The different with other function, "int main(){}" is the main "activity" that will be executed.

There is also Procedure in C.... =w=

What is the difference??

Function usually returns value, unlike procedure.
Procedure doesn't have to return a value
Procedure always uses "void" and it doesn't have to have argument.
Function can have any type of argument/parameter or return value, depend on the type..
If you declare the function or procedure first before main(), then you don't have to add declaration for function or procedure. else ~, then you need pre-declaration to the function .~


//Example of Procedure:
#include<stdio.h>
void printline(){
printf("_________");
}

//In this procedure, It will print "______" if you execute it in function main()...

//Example of Function:
int multiply(int a, int b){ // inside () are argument/parameter
int result=(a*b); //or you can write return a*b;
return result; //this is return statement
}

int main(){
int x,y;
scanf("%d %d",&x,&y);
printline();
printf("%d",multiply(x,y));
printline();
}


If you execute this code, you can see that we only need to call procedure and function name to execute them. 
When printf command executed ,x and y will go to function as the argument/parameter (a and b) and  it will give the "result" as return value. 
You will get an integer return value from multiplying x with y because the function type is "Int".


Why do we use function?

We use function to simplify the code that we write, so we don't have to write it again and again.
We only need write the function, and it will be executed. 

Function is very useful, because if you need change something in program that have a lot of code, you only need change something inside the function (if you use function =w=)

You can put any code in function or procedure as long as you follow the structure that is:

Function:
type of function__name of function (type of parameter _any parameter){
body of function : any code
}
Procedure:
void__name of procedure ((type of parameter _any parameter) or none){
body of procedure : any code
}

You can combine function and procedure with anything.
For example, procedure to input array:

#include <stdio.h>
void inputarray(int array[],int limit){//using pointer?? xD
int x;
for(x=0;x<limit;x++){
scanf("%d",&array[x]);
}
}
int main(){
int score[100];
int students;
scanf("%d",&students);
inputarray(score,students);// pointer?? xD
}

And many things~~~ =w=

Thank you for watching~~ ^__^


Tuesday, October 27, 2015

Code in C: Array

Okay then, i will explain about array in C programming language~

What is array?

Array is one of important things in C , and it has connection to pointer. Because you can use array for storing a lot of data orderly without making another variable...
For example: if you want to store similar data, you will store it at one place ( folder,etc). That's how array can be use...

When using array,you must give the array name,and you must input the size/capacity (how many data) that can be contain in that array..

Example:

int array1[5];

In example above.... name of array is "array1" and the data can be stored is 5..and the data type is integer.You must declare the type of array too.


Array stores data orderly, it always store data start from zero until capacity minus 1.
For example: The array is "a[5]", then it stores data from a[0],a[1],until a[5-1] or 1[4]..



When we are talking about how to use array... we should use loop to store data..


Why should we use loop?


Of course, we can use manual way to store data in array but what if there is a lot of data that we must insert... it will be troublesome if we input the data manually, then we can use loop to make it easier than using manual way.

Let us see about manual way vs looping:


Manual way:

#include <stdio.h>
int main(){
int a[100];
scanf("%d %d %d %d......",&a[0],&a[1],&a[2]......)// continue until a[99] -_-
}


Loop:

#include <stdio.h>
int main{}{
int a[100];
int x;
for(x=0;x<100;x++){
scanf("%d",&a[x]);
}
}

As you can see above, loop is easier than manual way... (=w=)b

When do we need use array?

We need to use array when there is a lot of data and we want to store that data.

For example:


#include<stdio.h>
int main(){
int size=10;
int score[size];
int x;
for(x=0;x<size;x++){
scanf("%d",&score[x]);
}
for(x=0;x<size;x++){
printf("%d",score[x]);
}

}
That will show, that array stores a lot of data..
We can apply to this way to:
#include<stdio.h>
int main(){
int size=10;
int score[size];
int x;
for(x=0;x<size;x++){
scanf("%d",&score[x]);
x+=10;
}
for(x=0;x<size;x++){
printf("%d",score[x]);
}

}



In that way, we can add 10 to every data, without adding 10 one by one to data... 



There is also multi-dimension array..
The difference with array are the size / capacity and how it stores data.
The capacity of array depend on the size of each dimension.


For Example:

int a[2][5];

It means the capacity of array "a" is 2 times 5 =10 data.
It can store 10 data, but it stores data similar matrix structure and you should use loop to store it to make it easier. The process of storing data for example above is like this:


#include<stdio.h>
int main(){
int x,y,a[2][5];
for(x=0;x<2;x++){
for(y=0;y<5;y++){
scanf("%d",&a[x][y]);
}
}
}


It will goes from a[0][0]---because array always store from "0". to a[2-1][5-1] or a[1][4]....



if you want to see the matrix form, you only need to use printf command like this:



#include<stdio.h>
int main(){
int x,y,a[2][5];
for(x=0;x<2;x++){
for(y=0;y<5;y++){
scanf("%d",&a[x][y]);
}
}
for(x=0;x<2;x++){
for(y=0;y<5;y++){
printf("%d",a[x][y]);
}
printf("\n");
}
}


Then you can see how multidimension array works.....



Thank you for watching =w=


Saturday, October 24, 2015

Hello~ Welcome to my blog...

Hello~ This is first time i make my blog~
So i hope it'll go well~ =w=
My blog contents will be random~