Skikkings: Inisialisering

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Skikkings

Hoofstuk 6: (lesing 14-17)

Def: Strukture wat verwante data items van dieselfde datatipe stoor
‘n Groep opeenvolgende geheue-posisies met dieselfde naam en tipe
Vaste grootte vir hele uitvoering van program
Formaat: naam[posisie no]

 Verklaring en Inisialisering
 Formaat: tipe naam[number of elements]
 Bv: 1. int c[10];
2. float b[3] = {1,2,3};
3. double c[] = {1,2,3,4,5}
 In c[10]: Begin by c[0] en eindig by c[9]
 As van 1-10 = for lus
for(index = 0; index < 10; index
array[index] = index + 1;
 Als selfde = stel net array[index] = getal
 Stringe
 Fibonacci series
// Storing the first 10 elements of the Fibonacci series // in an array.

- int fibonacci(int n);


- int main ()
{int array[10]; // An array of 10 integers
int counter;
// Store the fibonacci series in the array.
for(counter = 0; counter < 10; counter ++){ array[counter] = fibonacci(counter + 1);
}
// Print out the array.
for(counter = 0; counter < 10; counter ++)
{
printf("Element at index %d = %d\n", counter, array[counter]);
}
}
- // Definition of the fibonacci function used in main.
int fibonacci(int n)
{ int answer;
if(n <= 1)
{ // Base case: f(1) = 0
answer = 0;
}
else if(n == 2)
{ // Base case: f(2) = 1
answer = 1;
}
else
{ // Recursive Step: f(n) = f(n-1) + f(n-2)
answer = fibonacci(n-1) + fibonacci(n-2);
}
return answer; }

 Simboliese konstantes

 Aanstuur van skikkings na funksies


- Prototipe
void function_A(int my_array[], int size);
void function_B(float my_array[], int size, int a);
- Definisie
void function_A(int my_array[], int size){
int function_B(float my_array[], int size, int a){
return some_integer;
- Main
int my_integer_array [10];
float my_float_array = {1.0, 1.3, 4.1, 4.5, 10.0};
int my_integer = 20;
function_A(my_integer_array , 10);
my_integer = function_B(my_float_array , 5, my_integer);

 Lineêre en binêre soektogte van skikkings


- Linear Search: Appropriate for small unsorted arrays.
o Soek deur array in sekwensiele orde totdat waarde gevind is
o Array – array waardeur ons wil search
Size – size van array
Value – value vir wat ons soek
Returns the index of the first instance of the value in the array andersens sal dt -1
terugstuur
 int linear_search(int array[], int size, int value)
{ int index = -1; // The index we want to return
int counter;
for(counter = 0; counter < size; counter++)
{ if(array[counter] == value){ // Found the value
index = counter; // The counter == index of the // value we are looking
for.
counter = size; // Stops the for-loop.
// If the value was not found, index will still have // a value of -1.
return index;
- Binary Search: Appropriate for larger arrays, but arrays have to be sorted. (low, middle and
high)
o Searches through the array by continously splitting the search space into 2 equal parts
o //  array - The array to search through.
//  search_key - The value (key) to look for.
//  low - The lower index from which to start looking.
//  high - The upper index from which to start looking.
//returns the index of the value if found, otherwise -1
int binary_search(int array[], int search_key ,int low , int high )
{ int index = -1;
while(low <= high)
{ int middle = (high + low) / 2;
int key = array[middle];
if(key == search_key){ // Found the key.
index = middle;
low = high + 1; // Forces the while to exit. }
else if(key < search_key){ // Look in upper half.
low = middle + 1; }
else{ // Look in lower half.
high = middle - 1; }
return index;

 Sortering van skikkings (borrelsortering)


- Bubblesortering
o Algorithm used to sort arrays in either an ascending or descending order.
o Two elements inspected at the same time and if necessary swapped
o void bubble_sort(int array[], int size){
int i, j; // Loop counters
int temp; // Temporary storage variable
for(j = 0; j < size; j++){// Need to scan through array (size - 1) times // to make sure the
array is completely sorted
for(i = 0; i < size - 1; i++){
// Use (size - 1) limit to avoid reference error with array[i + 1].
if(array[i + 1] < array[i]){
// Create temporary storage for array[i]
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;

 Veelvoudige-onderskrif-skikkings
Ghf

You might also like