C Programming – Part 3 Arrays and Strings. Collection of variables of the same type Individual...

16
TEC 284 C Programming – Part 3 Arrays and Strings

Transcript of C Programming – Part 3 Arrays and Strings. Collection of variables of the same type Individual...

Page 1: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

TEC 284

C Programming – Part 3Arrays and Strings

Page 2: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

What are arrays?

Collection of variables of the same type

Individual array elements are identified by an integer index

Array index always begins at 0The index is written in square

brackets

//Example of declaring a single dimension array

int values[10];

Page 3: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Declaring arrays

Normally when an array is declared, the type and the number of elements are specified

The compiler will then know how much memory to allocate for the array variable

It is possible to declare an array with dimensions that are not fixed immediately, but space can be allocated as required

This is done with pointersint *values;

Page 4: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Referencing array elements

In C, like most other languages, array indices start at o and end at one less than the array size

Eg int itemList [5];//Contains the following elementsitemList[0]itemList[1] itemList[2] itemList[3] itemList[4]

Page 5: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Getting the length of an array

No one function call or property which directly gives the length of an array in c as in other programming languages

To find the length of an array, you can do the following

int arrayItems[10];int numberOfItems = sizeof(arrayItems) /

sizeof(int);

Page 6: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Multi-Dimension Arrays

They are declared as follows:int results[20][5];

This ranges from results[0][0], results[0][1], results[0][3], results[0][4], results[1][0], results[1][1]…results[19][4]

Arrays can have even more dimensionsint results[20][10][5];

For further dimensions, simply add more []

Page 7: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Initializing arrays

int amounts[3];amounts[0] = 10;amounts[1] = 25;amounts[2] = 35;

This is normal initializationRemember here that the size of the

array is 3Only indices 0, 1 and 2 can be

referenced

Page 8: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Initializing arrays

int numbers[] = {1, 2, 3}; //sets up an array with size 3

int numbers[5] = {3, 4,8}; //sets up an array with size 5, but only sets the first three values

This is equivalent to the following: int numbers[5];

numbers[0] = 3;numbers[1]=4;numbers[2]=8;

Page 9: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Initializing arrays

You can use designated initializers to skip over elements of an array

int number[3]= { [0]=5, [2]=7};

Page 10: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Initialization of character arrays

You can initialize a one dimensional character array by specifying A brace-enclosed comma-separated list

of constants A string constant (with optional

surrounding braces) Initializing a string constant places

the null character(\0) at the end of the string if there is room or if the array dimensions are not specified

Page 11: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Initialization of character arrays

char name1[ ] = { 'J', 'a', 'n' };char name2[ ] = { "Jan" };char name3[4] = "Jan";

Page 12: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Initialization of multi-dimension arrays

int month_days [2][12];month_days[0][0] = 1;month_days[0][1] = 2;

int month_days[2][12] ={

31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,

31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31

};

Page 13: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Strings

In C, strings are defined as arrays of characters char name[50];

C has no string handling facilities, so the following is illegal

char firstName[50], lastName[50], fullName[100];firstName = “John”; //illegallastName= “Doe”; //illegalfullName = firstName + lastName; //illegal

Page 14: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Strings

To print a string output, printf is used with the %s control character

printf(“%s”, name); In order to allow variable length strings

the \0 character is used to indicate the end of a string

So if we have a string char NAME[50]; and the name “DAVE” is stored in it, the contents will look like:

Page 15: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Exercise

Write a program which given a string and a character returns a count of the number of occurrences of that character

char find = ‘l’;char testString [] = “Hello World”;

Page 16: C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index

Exercise

Write a program which loops over a character array and displays the vowels and a count of each of the vowels that are present in the array//Starting pointchar sentence[] = “I am enjoying learning C programming in TEC 284”;