Download Metodyka i Techniki Programowania

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Catuṣkoṭi wikipedia , lookup

Transcript
07
Programming Methods and Techniques
C: Arrays
mgr inż. Jacek Wszołek
Arrays – introduction
An array is a collection of same type items, to which access is possible via the same identifier (the
name of an array) using an index number. Declaration of an array is as follows:
type name[SIZE];
Where type is the type of variable stored in an array (eg int), and SIZE determines the number of
elements that can be stored in declared array. Number of elements (SIZE) must be provided during
the declaration.
In an example below a 10-element array of integers is declared, and assigned to the first two
elements of value. Arrays are 0-indexed, so the first array element is at index = 0, second index = 1
and the last is size of an array minus 1
int tab[10];
tab[0] = 4;
tab[1] = 10;
Setting the size of the array is not necessary when we perform an initialization at the time of
declaration. In this case, the compiler will automatically calculate the size of the array based on the
number of elements to be stored, for example:
int tab[] = {1, 2, 3};
Array elements can accessed via an index, which is presented in the example above and using
pointers, which will be thoroughly discussed at the eighth laboratory.
Array with a string
It is very important to know the difference between character arrays and string arrays. Please note
that there is a little difference between them. It includes a null character '\0', which mark the end of
a string. If the character '\0' is present at the end of an array of characters (type char) then we
have to deal with a string (string).
Example below defines an array of characters:
char char_array[] = {'a', 'b', 'c'};
and string:
char string1 [] = {'a', 'b', 'c', '\0'};
or second method, which does not require to writing a null ('\0') char:
char string2 [] = ”abc”;
The use of standard library functions (<string.h> header file) on a regular array of characters
(without '\ 0') can lead to errors. For example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int ww = 10;
char char_array[] = {'a', 'b', 'c'};
printf("a string: %s\n", char_array);
return 0;
}
Multi-dimensional Arrays
In C there is no concept of "multidimensional array", but it is possible to create an array of arrays,
which means an array whose elements are other arrays. Such an array can be (and in practice is)
treated as a multidimensional array. An example of a "multidimensional" array:
float multidimensional_array [3][5];
In fact, it is a 3-element array of five-element arrays. Accessing "multidimensional" array's elements
is the same as with traditional arrays, for example:
multidimensional_array [0][2] = 4.2;
Of course, it is possible to declare 2, 3 or even 10-dimensional arrays. For example:
float multidimensional_array2 [3][5][7];
The vast majority of computer RAM is one-dimensional. Thus, a multidimensional array
must ultimately be converted to one-dimensional. As a result, any reference to a
multidimensional array requires additional arithmetical calculations which slows down the
process of reading / writing.
Arrays index bounds checking
C compilers never check the index value against the bounds of the array. Officially, this is motivated
by maximizing the speed of the program. As a result of this approach the programmer must
carefully use the index because its going outside of the array range can lead to serious errors. The
following example illustrates such a case.
#include <stdio.h>
int main(void) {
int var = 10;
int tab[3];
tab[0] = 0;
tab[1] = 1;
tab[2] = 2;
printf("var = %d \n", var);
tab[-1] = 3;
printf("var = %d \n", var);
}
return 0;
Exercises:
1. Write a program that sorts numbers in the 10-element int array. Let the initial values be
randomly generated in a range from 0 to 9.
2. Write a program that calculates the determinant of a matrix. Use as a two-dimensional array
as a matrix.