|1-C Programming Tutorial Part 6 By Crow of The LOC This part will cover arrays and there malipulation. The varaible used so afr have all had a common characteristic, each variable could be used to store only a single value. Example int count; char letter; float price; are of different data types and each variable can store one value of the declared data type. These types of variables are called scaler variables. Frequently we may have a set of values, all of the same type, that form a logical group. Example 10.55 7.12 2.59 0.86 12.39 3.98 A simple list consisting of individual items of the same scaler type is called a single dimensional array. Each item is stored sequentially in an array, and any single item can be accessed by giving the name of the array and the position of the item in the array. The elements position is called the index or subscript value. Array declaration The format is :- type variable[const-exp]; Example char string[20]; int number[MAXNO]; The constant expression gives the length of the array. Elements of the array are always numbered from 0 to length - 1 Example int num[10]; num[0] to num[9] are valid elements of the array. C will allow you to access invalid elements so beware! Array variables can be used anywhere that scaler variables are valid. Example num[i] num[2*i] num[j-i] One of the most important advantages of using integer expressions as subscripts is that it allows sequencing through an array using a for loop. The most common for loop to iterate through n items is :- for (j = 0; j < n; j++) statement; Example To sum all elements in an array int number[20],x,total; total = 0; for (x = 0; x < 20; x++) total += nummber[x]; or int number[20],x,total; for (tot = x = 0; x < 20; tot += number[x++]) ; /* NULL Statement */ Strings There is no 'string' varaible type. In C a string as always held in an array where each element is a character. A string usually has a null character '\0' at the end of the string. Example If we have an array of characters, string[12], containing the string 'apples',string[0] holds the character 'a',string[1] holds 'p', etc and string[6] holds the null terminator '\0'. Because strings are a derived rather than basic data type in C, C has no built in operators for string handling. Thus a programmer either writes his/her own string handling functions or uses the standard run time library declared in string.h. There are many library functions which deal with strings. It is not usually necessary to look at individual characters or access each character in an array. Example To read a string from the keyboard use :- scanf("%s",string); or gets(string); String output To write a string to the screen use :- printf("%s",string); or puts(string); String Copying To copy a string use strcpy(string1,string2); This copies all of the string2 into string1. Example strcpy(string,'bobbins'); Concatenating Strings To join or concatanate two strings use strcat(string1,string2); This concatenates the two strings and puts the result, properly null terminated into string1. Example strcat("shen","anagins"); Result:- "shenanagins" String Comparison When we compare two different strings there are two possibilities :- 1) Each character in the shorter sting is identical to the corresponding character in the longer string. 2) At some position the characters in the string differ. If the first possibility holds then the shorter will precede the shorter eg "cat" precedes "cathouse". If the second possibility holds then the order is determined by the order of the characters at the leftmost position at which the characters differ eg. "rubberband" precedes "rubberglove" as 'b' precedes 'g' at the position at which the characters differ. The function strcmp(string1,string2) is used to compare two strings. It compares them and returns :- zero - if the two strings are equal a negative integer if string1 comes before string2 a positive integer if string1 comes after string2 String Length To find the length of a string :- length = strlen(string); Multi-Dimensional Arrays The number of indexes used to access a particular element in an array is called the dimension of the array. An array with one pair of brackets [] is one dimensional; an array with two pairs of brackets [][] is two dimensional and so on. Arrays of more than one dimension are called multidimensional arrays. The format is :- int number [ROW] [COL]; int number [a][b][c]; A two dimensional array consists of rows and columns. For example the array of integers :- 4 9 10 1 9 3 22 4 8 20 17 2 is called a two dimensional array of integers. The array consist of three rows and four columns. To reserve storage for this array both the number of rows and the number of columns must be included in the arrays declaration. Example int number[3][4]; Similarly double prices[10][5]; char code[6][26]; declare that array prices consists of 10 rows and 5 columns of double precision numbers and the array code consists of 6 rows and 26 columns of characters. In order to locate each element in a two dimensional array an element is identified by its row and column number. Example num[1][3] identifies the element in row 1 column 3 in our array. An example using a two dimensional array val = num[3][2]; num[0][0] = 43; new = 4 * (num[1][0] - 5); sumrow0 = num[0][0] + num[0][1] + num[0][2] + num[0][3] + num[0][4]; In the next part of the this tutorial we will cover functions. Until next time fare thee well CROW of The LOC. End Of Part 6.