O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Carregando em…3
×

Confira estes a seguir

1 de 22 Anúncio

Mais Conteúdo rRelacionado

Semelhante a Arrays.pptx (20)

Mais recentes (20)

Anúncio

Arrays.pptx

  1. 1. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org1 Course : PROGRAMMING IN C TOPIC: Arrays and Pointers
  2. 2. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org What is an Array ? Array is a kind of data structure that can store a fixed-size (finite) sequential collection of elements of the same (homogeneous) type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. To make it simple let's break the words: •Array is a collection - Array is a container that can hold a collection of data. •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.
  3. 3. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org What is an Array ? Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. We can divide arrays in two categories. 1.One-dimensional array (Or single-dimensional array) 2.Multi-dimensional array
  4. 4. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Declaring Array Declaring Arrays To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows − This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10-element array called balance of type double, use this statement − •type is a valid C data type that must be common to all array elements. •arrayName is name given to array and must be a valid C identifier. •arraySize is a constant value that defines array maximum capacity.
  5. 5. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Initializing Array Initializing Arrays There are two ways to initialize an array. 1.Static array initialization - Initializes all elements of array during its declaration. 2.Dynamic array initialization - The declared array is initialized some time later during execution of program. Static array initialization We define value of all array elements within a pair of curly braces { and } during its declaration. Values are separated using comma , and must be of same type. Note: Size of array is optional when declaring and initializing array at once. The C compiler automatically determines array size using number of array elements. Hence, you can write above array initialization as.
  6. 6. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Initializing Array
  7. 7. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Initializing Array Initializing Arrays Dynamic array initialization You can assign values to an array element dynamically during execution of program. First declare array with a fixed size. Then use the following syntax to assign values to an element dynamically. The array index is an integer value, so instead of hard-coding you can wrap array input code inside a loop.
  8. 8. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Declaring / Initializing Array Output
  9. 9. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Advantages/ disadvantages of Arrays ? 1. Random access of elements using array index. 2. Use of less line of code as it creates a single array of multiple elements. 3. Easy access to all the elements. 4. Traversal through the array becomes easy using a single loop. 5. Sorting becomes easy as it can be accomplished by writing less line of code. Advantages: 1. 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. 2. Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation. Disadvantages:
  10. 10. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: To find max and Min number from an array /* * Find maximum and minimum in all array elements. */ for(i=1; i<size; i++) { /* If current element is greater than max */ if(arr[i] > max) { max = arr[i]; } /* If current element is smaller than min */ if(arr[i] < min) { min = arr[i]; } } /* Print maximum and minimum element */ printf("Maximum element = %dn", max); printf("Minimum element = %d", min); return 0; } #include <stdio.h> #define MAX_SIZE 100 // Maximum array size int main() { int arr[MAX_SIZE]; int i, max, min, size; /* Input size of the array */ printf("Enter size of the array: "); scanf("%d", &size); /* Input array elements */ printf("Enter elements in the array: "); for(i=0; i<size; i++) { scanf("%d", &arr[i]); } max = arr[0]; min = arr[0];
  11. 11. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: To find second largest number in an array
  12. 12. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: To find sum of all array elements #include <stdio.h> #define MAX_SIZE 100 int main() { int arr[MAX_SIZE]; int i, n, sum=0; printf("Enter size of the array: "); /* Input size of the array */ scanf("%d", &n); printf("Enter %d elements in the array: ", n); for(i=0; i<n; i++) { scanf("%d", &arr[i]); } /* * Add each array element to sum */ for(i=0; i<n; i++) { sum = sum + arr[i]; } printf("Sum of all elements of array = %d", sum); return 0; }
  13. 13. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Multi-dimensional Array in C Multi-dimensional array is an array of array or more precisely collection of array. Unlike one-dimensional array, multi-dimensional array stores. collection of array. Let us revise the concept of dimension. •One-dimensional array : Collection of data/values. •Two-dimensional array : Collection of one-dimensional array. •Three-dimensional array : Collection of two-dimensional array. •N-dimensional array : Collection of N-1 dimensional array. We can declare array with any dimension. However, two-dimensional array is most popular and widely used to solve many mathematical problems. The simplest form of multidimensional array is the two-dimensional array. To declare a two-dimensional integer array of size [x][y],
  14. 14. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Two dimensional Array in C Where type can be any valid C data type and arrayName will be a valid C identifier. A two-dimensional array can be considered as a table which will have x number of rows and y number of columns. A two-dimensional array a, which contains three rows and four columns can be shown as follows − Two-dimensional array is a collection of one-dimensional array. Two-dimensional array has special significance than other array types. We can logically represent a two-dimensional array as a matrix. Any matrix problem can be converted easily to a two-dimensional array.
  15. 15. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Two dimensional Array in C Initializing Two-Dimensional Arrays Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns. The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to the previous example −
  16. 16. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Two dimensional Array in C Accessing Two-Dimensional Arrays
  17. 17. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: To Add To matrices
  18. 18. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: To multiply Two matrices
  19. 19. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Passing Array as a Function argument Way-1 Formal parameters as a pointer − Way-2 Formal parameters as a sized array − Way-3 Formal parameters as an unsized array −
  20. 20. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Passing Array as a Function argument
  21. 21. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Returning Array from a Function C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index. If you want to return a single-dimension array from a function, you would have to declare a function returning a pointer as in the following example Second point to remember is that C does not advocate to return the address of a local variable to outside of the function, so you would have to define the local variable as static variable.
  22. 22. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Returning Array from a Function consider the following function which will generate 10 random numbers and return them using an array and call this function as follows:

×