Computer Programming Array and String PDF

Summary

This document provides an overview of arrays and strings in Computer Programming. It details the declaration, initialization, and advantages/disadvantages of using arrays, and explains the fundamentals of working with strings, including functions and applications.

Full Transcript

Department of FOT 01CE0101 - Computer Programming Unit - 4 Array and String  An array is a collection of data items, all of the same type, accessed using a common name.  Array is a data structure that hold finite sequential...

Department of FOT 01CE0101 - Computer Programming Unit - 4 Array and String  An array is a collection of data items, all of the same type, accessed using a common name.  Array is a data structure that hold finite sequential collection of homogeneous data. Array is a collection - Array is a container that can hold a collection of data. Arrays Array is finite - The collection of data in array is always finite, which is determined prior to its use. Array is sequential - Array stores collection of data sequentially in memory. Array contains homogeneous data - The collection of data in array must share a same data type.  Examples: - List of customers and their phone numbers -Table of daily rainfall data - List of employees in an organization - Test scores of a class of students and so Arrays on….  Structure of array:  Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array.  Syntax: type arrayName [ arraySize ]; Declaratio  Example: n of Arrays int group; float height; char name;  After an array is declared, its elements must be initialized.  There are two ways to initialize an array. Initializati on of 1.Static array initialization - Initializes all elements of array during its declaration. Arrays 2.Dynamic array initialization - The declared array is initialized some time later during execution of program.  We define value of all array elements within a pair of curly braces { and } during its declaration. Static  Values are separated using comma , and must be initializati of same type. on of array  Example int marks = {90, 86, 89, 76, 91};  You can assign values to an array element dynamically during execution of program. First Dynamic declare array with a fixed size.  Then use the following syntax to assign values to initializati an element dynamically. on of array Syntax: array_name[index] = some_value; EX: scanf("%d",&arr[i]);  Advantages: 1.Use of less line of code as it creates a single array of multiple elements. 2.Random access of elements using array index. 3.Easy access to all the elements. 4.Traversal through the array becomes easy using Advantage a single loop. s and 5.Sorting becomes easy as it can be accomplished Disadvant by writing less line of code.  Disadvantages: ages 1.Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation. 2.Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic.  One-dimensional Types of arrays(single) Arrays  Two-dimensional arrays  Multidimensional arrays  A one-dimensional array as a row, where elements are stored one after another. One – Dimension  Syntax: datatype array name[size]; al Array Where …. datatype: It denotes the type of the elements in the array. array name: Name of the array. It must be a valid identifier. size: Number of elements an array can hold. here are some example of array  The elements of an array can be accessed by specifying array name followed by subscript or index inside square brackets (i.e []). Accessing  Array subscript or index starts at 0. If the elements size of an array is 10 then the first element is of an array at index 0, while the last element is at index 9.  The first valid subscript (i.e 0) is known as the lower bound, while last valid subscript is known as the upper bound. Example: int a; then elements of this array are; Accessing elements First element – a of an array Second element – a Third element – a Fourth element – a Fifth element – a Array subscript or index can be any expression that yields an integer value. For example: Accessing int a; int i = 0, j = 2; elements of an array a[i]; // 1st element a[i+1]; // 2nd element a[i+j]; // 3rd element In the array a, the last element is at a, #include int main() { int a, i; for(i = 0; i < 5; i++) { printf("Enter a[%d]: ", i); Example scanf("%d", &a[i]); Program } printf("\nPrinting elements of the array: \n\n"); for(i = 0; i < 5; i++) { printf("%d ", a[i]); } return 0; }  1. Write down a program to enter 20 elements in an array and print the elements in reverse order.  2. Write a program to add all the elements in an array. Example  3.Write a program to copy one Program array to another array.  4. Write a program to find out all the even and odd numbers present in an array.  5. Write a program to swap two arrays.  Two – dimensional array is the simplest form of a multidimensional array. Two  The two-dimensional array can be Dimension defined as an array of arrays. The 2D al Array in array is organized as matrices which C can be represented as the collection of rows and columns.  It provides ease of holding the bulk of data at once. Syntax: Declaratio datatype array_name[rows][columns]; Where n of two dimension datatype: It denotes the type of the elements in al Array in the array. C array name: Name of the array. It must be a valid identifier. Row – size of rows Column – size of column There are two ways in which a Two-Dimensional array can be initialized. First Method: int x = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11} Initializing The above array have 3 rows and 4 columns. The Two – elements in the braces from left to right are stored in the table also from left to right. The elements will Dimension be filled in the array in the order, first 4 elements al Arrays from the left in first row, next 4 elements in second row and so on. Better Method: int x = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}}; Accessing the 2- D arr = 1 arr = 2 Arrays arr = 3 arr = 2 arr = 3 arr = 4 arr = 3 arr = 4 arr = 5 #include int main () { int a,i,j; for (i=0;i

Use Quizgecko on...
Browser
Browser