Minggu, 06 Desember 2015

Quiz W13 Blog

I want to show about previous W13 Quiz answers.

1. When is the right time to use: 
a. struct
b. pointer/reference
c. function
d. array

2. Write an example that uses combination all of items above, for example:
void bla( Cat * x ) { // x is pointer to struct Cat
x -> .. // this how to access data member using pointer to struct
}

ANSWERS

1. a. We use struct when we want to work with data that has some items. for example, there is an ID Card that contains data like a name, address, sex, and age. But it is different with array, because array is only can has some elements with the same type. (In Indonesia: Ketika kita ingin bekerja dengan data yang mempunyai sejumlah item. contohnya, sebuah kartu identitas yang mengandung data seperti nama, alamat, jenis kelamin, dan usia. tetapi struct berbeda dengan array, karena array hanya dapat mengandung beberapa elemen dengan tipe sama.).

    b. We use pointer/reference when we want to make an object data that contains an address that point the location of memory where a score is stored. (In Indonesia: Ketika kita ingin membuat sebuah objek data berisi alamat yang menunjukkan lokasi memori dimana suatu nilai tersimpan.).
   
    c. We use function when we want to make codes that is for do a task and that codes will be executed if it's names is called. (In Indonesia: Ketika kita ingin membuat sekumpulan kode yang ditujukan untuk melaksanakan suatu tugas tertentu dan kode-kode terbsebut akan dijalankan bila namanya dipanggil.).
  
    d. We use array when we want to make a data with a scores that has same type in certain order that uses the same name. (In Indonesia: Ketika kita ingin membuat sebuah data dengan nilai-nilai bertipe sama dalam urutan tertentu yang menggunakan sebuah nama yang sama.).


2. Here is an example about pointer that can be used to point the array




Here is the explanation about the example:

After ptr is set to point the array score via:
Setelah ptr diatur agar menunjuk ke array score melalui:

ptr = score;

Then accessing array data can be do via pointer ptr. As always, *ptr will be declare the first element in array score. So, if there is a statement like this:
Maka pengaksesan data array dapat dilakukan melalui pointer ptr. Seperti biasa, *ptr akan menyatakan isi elemen pertama di array nilai. Jadi, jika kemudian terdapat pernyataan seperti berikut:


ptr++;

ptr will be point to the next element in array ptr. Absolutely, to show all the element of array score via ptr, we will do via:
ptr akan menunjuk ke elemen berikutnya di array ptr. Tentu saja, untuk menampilkan semua elemen array nilai melalui ptr, perlu dilakukan melalui:

for(x=0;x<sizeof(score)/sizeof(int);x++)

In this case,
Dalam hal ini,

sizeof(score)/sizeof(int)

declares the code for get the amount of array element.
menyatakan kode untuk mendapatkan jumlah elemen array.


THANK YOU! ^.^






    

Rabu, 02 Desember 2015

stdarg.h in C

Hello readers!

i want to show you about stdarg.h in C.

stdarg.h



The stdarg.h header defines a variable type va_list and three macros which can be used to get the arguments in a function when the number of arguments are not known i.e. variable number of arguments.A function of variable arguments is defined with the ellipsis (,...) at the end of the parameter list.

stdarg.h types:
- va_list (type for iterating arguments)

stdarg.h macros:
- va_start (start iterating arguments with a va_list)
- va_arg (retrieve an argument)
- va_end (free a va_list)
- va_copy (copy contents of one va_list to another)

Library macros:
- void va_start(va_list ap, last_arg)
  This macro initializes ap variable to be used with the va_arg and va_end macros. The last_arg is the last known fixed argument being passed to the function i.e. the argument before the ellipsis.

- type va_arg(va_list ap, type)
  This macro retrieves the next argument in the parameter list of the function with type type.

- void va_end(va_list ap)
  This macro allows a function with variable arguments which used the va_start macro to return. If va_end is not called before returning from the function, the result is undefined.

Example:



That's the stdarg.h in C. Hope this will help you. Thanks! :D
nb: Some source that really helps me very much (recommended):
http://www.tutorialspoint.com/c_standard_library/stdarg_h.htm https://en.wikipedia.org/wiki/Stdarg.h




Jumat, 27 November 2015

File in C

Hello readers!

i want to show you about File in C.

File

  • Place to save the collection of data record.
  • Represents a sequence of bytes, regardless of it being a text file or a binary file.

C programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices.C programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices.

Opening Files

We use fopen() to create a new file or open a file.

FILE *fopen( const char * filename, const char * mode );

filename: string liberal, used to name our file
mode: 
ModeDescription
rOpens an existing text file for reading purpose.
wOpens a text file for writing. If it does not exist, then a new file is created. 
aOpens a text file for writing in appending mode. If it does not exist, then a new file is created.
r+Opens a text file for both reading and writing.
w+Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.
a+Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.

Closing Files

We use fclose() function to close a file.

int fclose( FILE *fp );

fclose(-) function returns zero on success, or EOF if there is an error in closing the file.

Writing Files

int fputc( int c, FILE *fp );

The function fputc() writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error.

Reading Files

int fgetc( FILE * fp );

The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error, it returns EOFThe following function allows to read a string from a stream:

char *fgets( char *buf, int n, FILE *fp );

The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a nullcharacter to terminate the string.
You can also use int fscanf(FILE *fp, const char *format, ...) function to read strings from a file, but it stops reading after encountering the first space character.

Example:



That's the Structs in C. Hope this will help you. Thanks! :D

nb: Some source that really helps me very much (recommended):
http://www.tutorialspoint.com/cprogramming/c_file_io.htm




Kamis, 26 November 2015

Structs in C

Hello readers!

i want to show you about Struct in C.

Struct

  • Another user defined data type available in C that allows to combine data items of different kinds.


Structure is the collection of variables of different types under a single name for better handling. For example: You want to store the information about peoples name, age and their citizenship. You can create these information separately but, better approach will be collection of these information under single name because all these information are related to person.

Syntax of structure:

struct structure_name 
{
    data_type member1;
    data_type member2;
    .
    .
    data_type member;

};


We can create the structure for a person as mentioned above as:

struct person
{
    char name[5];
    int cit_no, age ;
    

};

This declaration above creates the derived data type struct person.


Relation between Struct and Array

Sometimes, we will do a task with data that has some item, but it differs from array. For example, identity card has data like name, adress, sex, and age. That is not suitable if we use array, because array only can has some element with the same type. That example can be used in a struct.

Example:




Relation between Struct and Function

You can pass a structure as a function argument in the same way as you pass any other variable or pointer.

Example:



That's the Structs in C. Hope this will help you. Thanks! :D
nb: Some source that really helps me very much (recommended):
http://www.programiz.com/c-programming/c-structures
http://stackoverflow.com/questions/10370047/passing-struct-to-functio
http://www.tutorialspoint.com/cprogramming/c_structures.htm



Selasa, 24 November 2015

Pointer & Reference in C

Hello readers!

i want to show you about Pointer&Reference in C.

Pointer

  • A variable that contains an adress from another variable

Pointer, based on it's name, used to point. What is appointed? the answer is a data. Pointer is can be imagined as a normal variable, but it doesn't save data, it saves an adress of data. That's why Pointer is used to point.

Example of Pointer: 



Relation between Pointer and Array

An array name is a constant pointer to the first element of the array.

Example of Array Pointer:



Relation between Pointer and Function

C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.

There is an example where we pass an unsigned long pointer to a function and change the value inside the function which reflects back in the calling function 




Reference

Actually, a reference is not really like a pointer.
A compiler keeps "references" to variables, associating a name with a memory address; that's its job to translate any variable name to a memory address when compiling.
When you create a reference, you only tell the compiler that you assign another name to the pointer variable; that's why references cannot "point to null", because a variable cannot be, and not be.
Pointers are variables, they contain the address of some other variable, or can be null. The important thing is that a pointer has a value, while a reference only has a variable that it is referencing.
Now some explanation of real code:
int x = 0;
int& y = x;
That's the Pointer & Reference in C. Hope this will help you. Thanks! :D

nb: Some source that really helps me very much (recommended):
http://www.tutorialspoint.com/cprogramming/c_pointers.htm
http://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htm
http://www.cprogramming.com/tutorial/function-pointers.html
http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in


Minggu, 18 Oktober 2015

Arrays in C

Hello readers!

i want to show you about Arrays in C.

Arrays

  • Group of element that has the same data
  • Rows and Columns
  • Has the same name and the same element
Kinds of array:

  • Single Array 


  • Multidimentional Array (more than one dimention)



That's the Arrays in C. Hope this will help you. Thanks! :D

nb: You can find it more from this link: http://www.tutorialspoint.com/cprogramming/c_arrays.htm





            

Sabtu, 17 Oktober 2015

How Recursion Works in C

Hello readers!

i want to show you about How Recursion Works in C.

Recursion:

  • Function that calls itself as recursive function
  • Process of repeating the items in a similar way

Advantage and Disadvantage of Recursion:

Advantage: More elegant and makes some problem to be one problem
Disadvantage: Hard to find the logic


I will give you an example (you can see the pict):


That's the How Recursion Works in C. Hope this will help you. Thanks! :D

nb: for source/additional knowledge: 
  • http://www.programiz.com/c-programming/c-recursion
  • http://www.tutorialspoint.com/cprogramming/c_recursion.htm
  • http://www.cprogramming.com/tutorial/c/lesson16.html