Understanding Array
ARRAY is the array variable or a structured type consisting of a number of components that have the same type.
An array has a fixed number of components that number.
The number of components in an array is shown by an index to distinguish one variable with another variable.
• Example: value = (56 42 89 65 48)
• individual data items in the array can be assigned separately to state his position in the array
- Value (1) appoint 56
- Value (2) designate 42
• Numbers are written in parentheses indicates the position of individual items in the array (also called a subscript / index)
• The index in the array starts from 0
Variables can be used as a subscript, eg Value (i).
If i = 2 then points to the value (2) where 42
If i = 4 then points to the value (4) at 65
Individual data items in an array are called elements
Matrix
Array that contains only numbers and no data alfabetisnya
Classification of Array
1-dimensional array
Multi-dimensional arrays
Illustration 1 Dimensional Array char
ARRAY is the array variable or a structured type consisting of a number of components that have the same type.
An array has a fixed number of components that number.
The number of components in an array is shown by an index to distinguish one variable with another variable.
• Example: value = (56 42 89 65 48)
• individual data items in the array can be assigned separately to state his position in the array
- Value (1) appoint 56
- Value (2) designate 42
• Numbers are written in parentheses indicates the position of individual items in the array (also called a subscript / index)
• The index in the array starts from 0
Variables can be used as a subscript, eg Value (i).
If i = 2 then points to the value (2) where 42
If i = 4 then points to the value (4) at 65
Individual data items in an array are called elements
Matrix
Array that contains only numbers and no data alfabetisnya
Classification of Array
1-dimensional array
Multi-dimensional arrays
Illustration 1 Dimensional Array char
Accessing Array Elements
Array elements can be accessed by the program using a particular index in random or sequential
Charging and collection at the specified index value can be done by setting the value, or display the value on the index in question.
In C, there is no error handling to the limit value of the index, if the index is in the array index that is defined or not. It is the responsibility of the programmer. So if the programmer to access the index is wrong, then the resulting value will be different or damaged due to access a memory address that is not appropriate.
Array Handling
• The basic method of handling an array:
- Finding the largest value
- Finding the smallest value
- Calculate the average value
- Calculate the total value
- Count the number of values below the average
• Sorting Array (Sort)
- Buble sort
- Straight selection sort
• Finding / Researching Array (Search)
- Linear search
Handling Array [2]
• Example: A passing student would be read in the array. Then be shown the largest value, smallest value, average value, total value, and the number of values below the average.
• Stages of array handling
- Input the data into the array
- Calculate the value of the largest, smallest, total, and the average
- Calculate the number of values below the average
- Showing the results (output)
ARRAY 1 DIMENSIONS
char letter [9];
int age [10];
int state [2] = {0.1}
arr_dinamis int [] = {1,2,3}
• The sign [] is also called "the elements ...". For example, the condition [0] means the condition array element to zero.
• An array that has been ordered, for example, 10 sites have not filled all of them, may be filled only 5 elements, either in sequence or not. But the condition is not completely filled, place orders remain in memory as much as 10 places, so places will remain unfilled terpesan and left blank.
• We can not declare a dynamic array without initialization!
Example
# Include <iostream>
# Include <conio.h>
void main ()
{Int y [] = {1, 2, 7, 4, 5};
int n, r = 0;
for (n = 0, n <5, n + +)
{
r + = y [n] ;/ / y [n] is replaced y [3] how the results
}
cout << "" << r;
getch ();}
Program
# Include <iostream>
# Include <conio.h>
void main ()
{Int values [5], x; / / no initialization
cout << "Enter the value: \ n \ n";
for (x = 0; x <5; x + +)
{Cout << "The value of number:";
cin >> value [x];}
cout << "\ n \ nOutput value: \ n";
for (x = 0; x <5; x + +)
{Cout << "\ nNilai number:" << value [x];}
getch ();}
Editing array
# Include <iostream>
# Include <conio.h>
void main ()
{Int A [5] = {20,9,1986,200,13} ;/ / with initialization
int n;
clrscr ();
cout << "The data length \ n";
for (n = 0, n <5, n + +)
{Cout << "" << A [n];}
cout << "\ nData a new one: \ n";
A [0] = 4;
A [1] = 2;
A [2] = 1;
A [3] = 3;
A [4] = 5;
for (n = 0, n <5, n + +)
{Cout << "" << A [n];}
getch ();}
remove array element
# Include <iostream>
# Include <conio.h>
void main ()
{Int A [5] = {20,9,1986,200,13};
int n, delete;
clrscr ();
cout << "The data length \ n";
for (n = 0, n <5, n + +)
{Cout << "" << A [n];}
cout << "elements who want to be deleted:";
cin >> delete;
cout << "\ nData a new one: \ n";
for (n = delete-1, n <5-1; n + +)
{A [n] = A [n +1];}
for (n = 0, n <4; n + +)
{Cout << "" << A [n];}
getch ();}
Search Data
# Include <iostream>
# Include <conio.h>
void main ()
{/ / Search data
int A [10] = {1,2,3,4,5,6,7,8,9,10}, bil;
cout << "Enter the number you are looking for:";
cin >> bil;
for (int c = 0; c <10; c + +)
{
if (A [c] == bil)
cout << "The value you are looking at the index" << c;
}
getch ();}
2 DIMENSIONAL ARRAY
• two-dimensional arrays are often described as a matrix, an extension of one-dimensional array.
• If the one-dimensional array consists of only a few rows and columns of elements, then the two-dimensional array consists of multiple rows and multiple columns of the same type elements.
Sorting (sorting)
Sorting is the process set by rule or set of objects a particular arrangement. The order of these objects can
* Ascending (ascending = from small data to the data is greater)
or
* Decreased (descending = from large data to the data is smaller).
Sorting algorithms (sorting):
• Bubble sort (bubble)
• Selection sort (maximum / minimum)
• Insertion sort (insert)
• Heap sort
• Shell sort
• Quick sort
• Merge sort
• Radix sort
• Tree sort
A. Bubble sorting
Bubble sorting method (bubble sort), inspired by soap bubbles in the water. Because the density of the soap bubbles are lighter than the density of water then bubbles will always float.
The principle of flotation is also used in bubble sort. Most valuable elements of a small "float", meaning that lifted up (or to the far left end) through the exchange. Flotation process is carried out N times steps. At step i, array [1 .. N] will consist of two parts:
- The part that was ordered is L [1] .. L [i].
- The part that has not been sorted L [i +1] .. L [n].
source: STMIK AMIKOM YOGYAKARTA
0 komentar
Posting Komentar