{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08-C-Arrays

# 08-C-Arrays - ECE 175 Computer Programming for Engineering...

This preview shows pages 1–6. Sign up to view the full content.

ECE 175: Computer Programming for Engineering Applications Arrays

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ECE 175: Computer Programming for Engineering Applications - Arrays Basics of arrays Allows identical items to be grouped into a composite data structure Example: Scores of students in this class Declaring and referencing arrays Declaration specifies array name and the number of elements in the array The number of elements must be a constant (may use constants through #define ) The elements are numbered from 0, 1, 2, ..., N-1 Every element of the array may be viewed as an independent variable The usage of individual element is exactly the same for any simple variables that we have seen so far C does not check for array boundaries; so be careful in accessing arrays 2 10 x[0] 5 x[1] 2 x[2] -3 x[3] 25 x[4] 1001 x[5] -1 x[6] 23 x[7] Example: int x[8]; double d[10]; char c[255];
ECE 175: Computer Programming for Engineering Applications - Arrays Array initialization Initialization at declaration The numbers declared are sequentially assigned to elements starting from 0 The value of -1 at the end helps in identifying when the array ends Otherwise, count the elements If you do not specify the array limit explicitly, it will be taken based on the initialization set If the array limit explicitly specified is lower, then compiler will provide warnings 3 Examples: int primes_up_to_100[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, -1}; char vowels[] = {'A', 'E', 'I', 'O', 'U'};

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ECE 175: Computer Programming for Engineering Applications - Arrays Accessing arrays Array subscripts Plenty of ways to specify the subscript You can use constants, variables, another array element, arithmetic expression, etc Use of loops to access arrays You can access a sequence of elements using loops The loop control variable may be used to access an element Array elements can be passed as function arguments 4
ECE 175: Computer Programming for Engineering Applications - Arrays Array arguments Can send the whole array of elements as argument to a function Function has the pointer to the first element of the array Other usage Use *<arrayname> instead of <arrayname>[] 5 Syntax: <return type> <function name> (<data type> <array name>[], <other arguments>); Example: int array_max(int a[], int n); Syntax: <return type> <function name> (<data type> *<array name>, <other arguments>); Example: int array_max(int *a, int n);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 18

08-C-Arrays - ECE 175 Computer Programming for Engineering...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online