Jumat

ARRAY

Array is a group of similar data that is stored into the variable with the same name, by giving the index in the variable to distinguish from each other.

ARRAY VARIABLES
nama_variabel [index]

provisions arrray same variable name with the name of common variables.
index shows the number of variables.

ARRAY VARIABLE DECLARATION

BU: nama_variabel type [index];

Example: float bil [10];
array variable declaration with the name of the car that will accommodate 10 data type float. 10 shows the variable bil Index consists of 10 elements, where each element will contain a data.

Array index starts from zero (0), while the number usually starts from a single element (1). No element can be made equal to the index numbers to facilitate the making of the program is to give the index one more than the amount of data required, thus becomes:
bil float [11]

Initialization DIMENSIONAL ARRAY 1
Initialization can be done in conjunction with or separate declarations. Initialization of an array is to put the array elements between curly braces {}, among other elements that one with comma separated.
 int bil [2] = {4,1,8}

bil [0] = 4
 bil [1] = 1
bil [2] = 8

AUTOMATIC ARRAY is the array initialization is done in a particular function. Only ANSI-standard C compiler C that can initialize automatic arrays.
How to initialize an array of compilers that do not follow the ANSI standard C:
1. Initialized outside the function as a variable GLOBAL / EXTERNAL ARRAY.
int bil [2] = {0,0,0};
main ()

2. Function as a variable is initialized didlm LOCAL / STATIC ARRAY.
main ()
{
static int bil [2] = {0,0,0};
.........

In automatic array is not initialized, array elements will have a value that does not irregular. If global & static array is not initialized then all array elements are automatically given a value of zero (0).

Example:
main ()
{
int y;
int count = 0;
int x [0];
for (y = 0, y <5; y + +)
{
count + = y;
x [y] = count;
printf ("% 3d -% 3d \ n", y, x [y]);
}
}

OUTPUT:
0-0
1-1
2-3
3-6
40-10

DEFINING THE NUMBER OF ELEMENTS IN ARRAY VARIABLES
The size of the index variable can be determined using
preprocessor directives # define
# Define N 40
main ()
{
int number [N], salary [N], goal [N], status [N], Juman [N];

When Besari index will be changed to 50, just replaced with
# Define N 50

2 DIMENSION ARRAY
nama_variabel [indeks1] [indeks2]

indeks1: number / line number
indeks2: number / column number
Number of elements owned by 2-dimensional array can be determined by multiplying indeks1 * indeks2

eg: array A [2] [3] will have a 2 * 3 = 6 elements.

main ()
{
bil float [5] [5]
.......

can be written with a # define
# Define N 5
main ()
{
bil float [N] [N]
.......

Initialization 2 DIMENSIONAL ARRAY
main ()
{
bil float [2] [3] =
{{1,2,3}, / * line 0 * /
{4,5,6}, / * row 1 * /
}

bil element [0] [0] = 1
bil element [0] [1] = 2
bil element [0] [2] = 3
bil elements [1] [0] = 4
bil elements [1] [1] = 5
bil elements [1] [2] = 6

Example:
main ()
{
int x [3] [5];
int y, z;
int count = 0;
for (y = 0, y <3; y + +)


{
printf ("y =% d \ n", y);
for (z = 0, z <5; z + +)
{
count + = z;
x [y] [z] = count;
printf ("% / t% 3d -% 3d \ n", z, x [y] [z]);
}
}
}

OUTPUT:
y = 0
   0-0
   1-1
   2-2
   3-6
   40-10
y = 1
   00-10
   1-11
   2-13
   3-16
   40-20
y = 2
  00-20
  1-21
  2-23
  3-26
  40-30

STRING and ARRAY
1. In the string there is a null character (\ 0) at the end of the string
2. Strings are definitely an array, the array is not necessarily a string

EXAMPLE - EXAMPLE:
1. filling array with input through keyboard
baca_input ()
{
float value [10];
for (i = 0; i <10; i + +)
scanf ("% d", & value [i]);
}

2. The function that print the contents of an array from end to beginning
cetak_array ()
{
float value [10];
for (i = 9; i> = 0; i -)
scanf ("% 3f", value [i]);
}
3. Counting - average contents of an array of values
rata_rata ()
{
float value [10], num * flat;
for (i = 0, num = 0; i <= 9; i + +)
num + = value [i];
Average = num / i;
}

4. Finding the greatest value
big ()
float temp, value [10];
{
for (temp = value [0], i = 1; i <= 9; i + +)
if (value [i]> temp)
temp = value [i];
}
return (temp)

Tidak ada komentar:

Posting Komentar