c unit programming arrays in detail 3.pptx

anithaviyyapu237 20 views 50 slides May 26, 2024
Slide 1
Slide 1 of 50
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50

About This Presentation

c unit programming arrays in detail 3


Slide Content

C Programming unit - III

Index Arrays Strings Concept of arrays String Concepts Using arrays in C C Strings Array Applications String Input / Output Functions Two Dimensional Arrays Arrays of Strings Multi Dimensional Arrays String Manipulation Functions Programming Examples- Calculate Averages

Arrays Concept of arrays: When we need to solve a problem of reading, processing, and printing a set of n integers. General procedure to declare such set of n integers is to assign values declaring that n number of variables. Declaring, processing and printing values accessing n variables is ofcourse a complicated task. Although such process is acceptable for 10 variables it is definitely not acceptable for 100 ‘s or 1000’s of variables. To process such large amounts of data, we need a data structure to access .

An Array is a collection of elements of the same data type. Since an array is sequenced collection, we can refer to the elements in the array as the first element , the second element and so forth until we get last element, When we put 10 integers into an array , the address of first element is 0 and sequence flows. Example: scores , scores 1 ,scores 2 ………… scores n-1 Rather than using subscripts , we will place the subscript value in brackets. This format is called indexing Using indexing notation, we would refer to scores as scores[0]. The data that is stored in an array is called elements of array.

using arrays in c C provides for two different array types : fixed length array and variable length array. In a fixed length array, the size of array is known when the program is written. In a variable length array, the size of array is not known when the program is run. Syntax: Fixed length array : data type arrayname [size of array]; Variable length array : datatype arrayname [ ] ;

Defining and Declaring arrays: An array must be declared and defined before it can be used. Array declaration and definition tells the compiler the name of an array, the type of each array element and the size or number of elements in the array. In a fixed length array, the size of the array is a constant and must have a value at the time of compilation.

Accessing elements in Array: C uses an index to access individual elements of an array. The index must be an integral value or an expression that evaluates to an integral value. The simplest form for accessing an element is a numeric constant. The index is an expression , typically the value of a variable , to process all the elements in an array. element address= array address + ( sizeof (element)*index)

Storing values in arrays: To store values in the array, we must either initialize the elements , read values from the keyboard or assign values to each individual element. Initialization : Just as with variables, initialization of the elements in a fixed length array can be done when it is defined. Variable length arrays cannot be initialized when they are defined. Only fixed length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning the values.

Inputting values to an array: Another way to fill array is to read the values from keyboard . This method of inputting values can be done using a loop. The most appropriate loop is for loop because number of elements can be fixed and known. Assigning values to array: We can assign values to individual elements using the assignment operator. Any value that can be reduced to proper type can be assigned to an individual array element.

Arrays in detail Multi-dimensional arrays C supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. Passing arrays to functions You can pass to the function a pointer to an array by specifying the array's name without an index. Return array from a function C allows a function to return an array. Pointer to an array You can generate a pointer to the first element of an array by simply specifying the array name, without any index.

Initializing two dimensional array A two-dimensional array can be initialized during its declaration. Two dimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns. A  two-dimensional array  in C can be thought of as a matrix with rows and columns. A two-dimensional array is an array of two one-dimensional arrays. 

Accessing two dimensional array elements: An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. Syntax: datatype variablename [number of rows][number of columns]; Example : int my_array [5][3]; The code above declares a two-dimensional array that has five elements. Each of these five elements is themselves, an array of three integers.

Just like one-dimensional arrays, two-dimensional arrays also require indices to access the required elements. A row and a column index are needed to access a particular element; for nested loops, two indices (one to traverse the rows and the other to traverse the columns in each row) are required to print a two-dimensional array.

Multi dimensional array in c Multi dimensional arrays can have three or more dimensions. Every dimension of a multi dimensional array is called a plane. Every plane consists of rows and columns. Syntax to declare a three dimensional array: datatype array_name [number of planes][number of rows in each plane][number of columns in each plane];

Declaring Multi Dimensional Arrays: Multi dimensional arrays must be declared before being used. Declaration tells the compiler the name of the array , the type of each element, and the size of each dimension. The size of the fixed length array is a constant and must have a value at compilation time .

Initializing a multi dimensional array: A three dimensional array has to be initialized before it is used If we want to store values into array, we must either initialize the elements , read values form key board or assign values to each individual element. While initializing a multi dimensional array we nest each plane in a set of brackets. For each plane we bracket the rows as we did for two dimensional arrays. Example: int a[2][2][4 ]= { { { 1,2,3,4 } , { 2,4,5,6 } },{ { 8,9,10,11 } , { 5,6,8,9 } } } Explanation of flower braces: { } Red braces for opening planes { } Black braces to indicate rows of each plane { } Pink braces to open columns of each plane

Calculate averages

Strings in c Strings concepts: The string can be defined as the one-dimensional array of characters terminated by a null ('\0’). The character array or the string is used to manipulate text such as word or sentences. Each character in the array occupies one byte of memory, and the last character must always be 0. The termination character ('\0') is important in a string since it is the only way to identify where the string ends. When we define a string as char s[10], the character s[10] is implicitly initialized with the null in the memory.

Declaring arrays: There are two ways to declare a string in c language. 1. By char array 2. By string literal Declaring a string is similar as declaring a one dimensional array. Syntax: char variable_name [size of string]; In above syntax variable_name is the name given to the string and size is used to define the length of the string. Despite of normal array a string array will have extra terminating character Null character .(‘\0’) to indicate the termination of string array.

Declaring strings by char array: Declaring a string array variable by character array can be done in the way we normally declare a integer array. But in strings, it has to be terminated by null. Example: char str[10]={‘H’,’E’,’L’,’L’,’O’,’\0’}; The memory representation of above string array shall be like : INDEX: 0 1 2 3 4 5 H E L L O \0

While declaring a string it is not mandatory to mention size. Hence a string can also be declared as: char str[ 10] = {‘H’,’E’,’E’,’L’,’L’,’O’,’\0’}; In such case, null character will terminate the space allocation of character array.

Declaring a string by string literal shall be done by declaring a character array variable. Syntax: char str[10]={“hello”};

There are two main differences between char array and string literal. 1. We need to add the null character ‘\0’ at the end of the array by ourself in char array where as , it is appended internally by the compiler in case of string literal. 2. The string literal can not be reassigned to another set of characters where as we can reassign the characters of the array.

Traversing String: Traversing a string is one of the most important aspects . There may be a need to manipulate a very large text which can be done by traversing the text. Traversing string is different form traversing ana integer array. To traverse an integer array we need to know the length of array. In case of strings, we can use null character to identify the end of the string . Hence, traversing of a string can be done in two ways: 1. By using the length of the string 2. By using the null character .

Traversing string using length of array

Traversing string using null pointer

STRING INPUT/OUTPUT FUNCTIONS C provides two basic ways to read and write strings. First, we can read and write strings with the formatted input/output functions: scanf / sscanf and printf / fprintf . Second way is we can use a special set of string only functions , get string (gets/ fgets ) and (puts/ fputs ).

C gets() and puts() functions: C gets() function : This function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows the string to enter space- separated strings. It returns the string entered by the user.

Reading string using gets() function: The gets() function is risky to use because it does not perform any a rray bound checking and keep reading characters until the new line(enter is encountered.). It suffers from buffer overflow and it can be avoided using fgets () function. fgets () makes sure that not more than maximum limit characters are read.

C puts() function: The puts() function is verymuch similar to printf () function. The puts() is used to print the string on the console which us previously read by using gets() or scanf () function. The puts() function returns an integer value representing no.of characters being printed on console.

Sscanf and sprintffunctions : The sprintf () function works just like printf () function. The difference is it returns the formatted string. The first argument to the sprintf () function points to the target string. The rest of arguments are same as printf () function. The sprintf () function stores the whole content to be displayed into a single string and displays it.

The sscanf () function is similar to scanf () . Sscanf () allows us to read formatted datat from a string rather than standard input/output functions. The rest of arguments are similar to scanf (). It returns no.of items read from the string and returns -1 if an error occurs.

String manipulation functions C String Length: strlen () function The strlen () function returns the length of the given string. It does not count null character ‘\0’. C Copy String: The strcpy () function copies the source string in the destination string. strcpy ( destination,source )

C String Concatenation: strcat () The strcat ( firststring , secondstring ) function concatenates two strings and result is returned to first string. C Compare String : strcmp () The strcmp ( firststring , secondstring ) function compares two strings and returns 0 if both strings are equal.

C String Lower case strlwr (): The strlwr (string) function returns string characters in lower case. C String Upper case strupr () : The strupr (string) function returns string characters in upper case.

C String Reverse function: strrev () The strrev (string) reverses the string and returns the reversed string value. C String strstr () : The strstr () function returns the pointer to the first occurrence of matched string in the given string. It is used to return substring from the first match till the last character. String represents the full string from where sub string can be searched. Match represents the substring to be searched in full string.
Tags