Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index "— Presentation transcript:

1 C Programming – Part 3 Arrays and Strings

2  Collection of variables of the same type  Individual array elements are identified by an integer index  Array index always begins at 0  The index is written in square brackets  //Example of declaring a single dimension array int values[10];

3  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 pointers int *values;

4  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 elements itemList[0] itemList[1] itemList[2] itemList[3] itemList[4]

5  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);

6  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 dimensions int results[20][10][5];  For further dimensions, simply add more []

7  int amounts[3]; amounts[0] = 10; amounts[1] = 25; amounts[2] = 35;  This is normal initialization  Remember here that the size of the array is 3  Only indices 0, 1 and 2 can be referenced

8  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;

9  You can use designated initializers to skip over elements of an array  int number[3]= { [0]=5, [2]=7};

10  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

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

12 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 };

13  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”; //illegal lastName= “Doe”; //illegal fullName = firstName + lastName; //illegal

14  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:

15  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”;

16  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 point char sentence[] = “I am enjoying learning C programming in TEC 284”;


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

Similar presentations


Ads by Google