Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 9 Data Structures Arrays.

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 9 Data Structures Arrays."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 9 Data Structures Arrays

2 Arrays in C have a very similar look and feel to arrays in other languages such as Java, although in terms of implementation they are very different. An array is a structured data type An array is: A collection of data objects (all of the same type) referred to collectively by a specific name An array is of a fixed size

3 The objects within the collection can be accessed independently by an index since they are consecutively stored in memory under a single name. e.g. int myarray[60]; myarray[10]=6; char code[4]; code[3]=’t’;

4 Array Subscripts i.e. An item can be accessed by its position number (subscript) within the array. The array name is a constant pointer – it is the name given to the address of the first item in the array (more about pointers and how they are used with arrays in lectures 6 and 7).

5 Order of Arrays Single-Dimensional a.k.a Vector Two-Dimensional a.k.a. Matrix or Table 123450 123450 0 1 2 marks[0][2] int smarks[3][6] marks[2] int marks[6]

6 Multi-Dimensional Array 2 or more Dimensions 2D case referred to as matrix In theory limitless dimensions Hard to visualise beyond 3-dimensions

7 Single-dimensional Arrays or Vector A one dimension array (i.e. an array only requiring one subscript to specify any element) is declared in C as follows: int example[10]; This declares and allocates the memory space for an array called example, to contain 10 elements. Array subscripts always start at zero and so the example array consists of the elements example[0] to example[9] inclusive. e.g. example[0]=12; sum=example[0]+example[3];

8 Subscript Range Checks Consider the following code fragment: int a[10], i; scanf(“%d”, &i); printf(“%d\n”, a[i]); impossible for the compiler to safety check the array subscript NO range checking to ensure that any given subscripts are within the declared bounds of the array. Consequently, the following code will compile and run without even a warning on most systems: int b[10]; b[13]=1234; Result of the above code. Address=base+(subscript*element_size) This formula is used irrespective of the subscript supplied.

9 Initialising Arrays usually done at the start of a program or function Can be done with a set of explicit statements e.g. example[0]=0; example[1]=0; etc.. Simpler notation is available e.g. int x[4]={1, 2, 3, 4};

10 Array Size What happens if the initialising list is smaller than the size of the array? e.g. int a[3]={1, 0, 0}; int a[3]={1}; are equivalent statements What happens if the initialising list is larger than the size of the array? Error A useful extra rule in C allows you to omit the array size specifier when declaring an array with an initialising list. e.g. int d[]={1, 2, 3, 4, 5, 6}; d[0] – d[5] double price[]={9.5, 10.2, 11.6};

11 Character Strings char codes[]={‘a’, ‘b’, ‘c’}; char codes=’abc’; == char codes[]={‘a’, ‘b’, ‘c’, ‘\0’}; The codes array in this example actually has 4 elements: where \0 i.e. null character is added as a String terminator (more about Strings and String terminators in lectures 6 and 7). ‘a’‘b’‘c’‘\0’

12 Example 1 Passing an array as a parameter to a function. #include char code[4]; /*entervalues function If the array size were omitted in the array parameter declaration, it would have to be passed in as an extra function parameter */ entervalues(char myarray[4]) { int i; char dummy; for(i=0;i<4;i++) { printf(“Enter the grade: “); scanf(“%c%c”, &myarray[i], &dummy); } int main(void) { entervalues(code); /* The address of the first location of the array is passed as an array parameter to the function entervalues */ return 0; }

13 Manipulating Arrays In functions that manipulate arrays,size information is not always included (more on this in lectures 6 and 7). Consider a function which totals all of the numbers in an array of type int and returns the result Second parameter will have to be passed to the function to say how big the array is: int total(int array[], int arraySize); Consider the following usage of the total() function: int a[5], tot1, tot2, tot3; tot1=total(a, 5); tot2=total(a, 2); tot3=total(a, 200); Advantage: This makes total(), and indeed the C programming language, more versatile Disadvantage: As it is possible to lie about the array size, it is also possible to make a mistake i.e. C is more error prone.

14 Multi-dimensional Arrays Arrays with more than one dimension are also available in C. Individual elements in a multi-dimensional array are accessed using as many subscripts as there are dimensions i.e. 2 for a 2- dimensional array, n for an n-dimensional array. e.g. int twoDim[3][4]; int twoDim[3][4]= { (1, 2, 3, 4), ( 5, 6, 7, 8), (9, 10, 11, 12) }; int twoDim[3][4]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; int twoDim[][4]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

15 Example 2 #include void display(int nums[3][4]) /* display function; prints the contents of the array */ { int row, col; for (row=0; row<3;row++) { for (col=0;col<4;col++) printf(“%4d\n”, nums[row][col]); printf(“\n”); } int main(void) { int val[3][4] = { 8, 6, 4, 5, 3, 15, 5, 7, 12, 5, 6, 7}; display(val) return 0; }

16 Example 3 Problem: Given the following function prototype, write a function which swaps two elements in an array of type int; void swap(int array[], int, int, int);

17 Code: #include int swap(int array[], int, int, int); /* prototype */ void main(void) { int arr[5]={1, 2, 3, 4, 5}, element, result, el1, el2; printf(“Which two elements would you like to swap?\n”); scanf(“%d%d”, &el1, &el2); result=swap(arr, 5, el1, el2); if (result==0) for(element=0;element<5;element++) printf(“%d “, arr[element]); else printf(“Error!”); } /* swap function declaration */ int swap(int arr[], int size, int x, int y) { int temp; if (x>size||y>size||x<1||y<1) return 1; else { temp=arr[x-1]; arr[x-1]=arr[y-1]; arr[y-1]=temp; return 0; }


Download ppt "Introduction to C Programming CE00312-1 Lecture 9 Data Structures Arrays."

Similar presentations


Ads by Google