- DSA using C - Discussion
- DSA using C - Useful Resources
- DSA using C - Quick Guide
- DSA using C - Recursion
- DSA using C - Sorting techniques
- DSA using C - Search techniques
- DSA using C - Graph
- DSA using C - Heap
- DSA using C - Hash Table
- DSA using C - Tree
- DSA using C - Priority Queue
- DSA using C - Queue
- DSA using C - Parsing Expressions
- DSA using C - Stack
- DSA using C - Circular Linked List
- DSA using C - Doubly Linked List
- DSA using C - Linked List
- DSA using C - Array
- DSA using C - Concepts
- DSA using C - Algorithms
- DSA using C - Environment
- DSA using C - Overview
- DSA using C - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
DSA using C - Arrays
Overview
Array is a container which can hold fix number of items and these items should be of same type. Most of the datastructure make use of array to implement their algorithms. Following are important terms to understand the concepts of Array.
Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a numerical index which is used to identify the element.
Array Representation
![Array](/dsa_using_c/images/array.jpg)
As per above shown illustration, following are the important points to be considered.
Index starts with 0.
Array length is 8 which means it can store 8 elements.
Each element can be accessed via its index. For example, we can fetch element at index 6 as 9.
Basic Operations
Following are the basic operations supported by an array.
Insertion − add an element at given index.
Deletion − delete an element at given index.
Search − search an element using given index or by value.
Update − update an element at given index.
In C, when an array is initiapzed with size, then it assigns defaults values to its elements in following order.
Sr.No | Data Type | Default Value |
---|---|---|
1 | bool | false |
2 | char | 0 |
3 | int | 0 |
4 | float | 0.0 |
5 | double | 0.0f |
6 | void | |
7 | wchar_t | 0 |
Example
#include <stdio.h> #include <string.h> static void display(int intArray[], int length){ int i=0; printf("Array : ["); for(i = 0; i < length; i++) { /* display value of element at index i. */ printf(" %d ", intArray[i]); } printf(" ] "); } int main() { int i = 0; /* Declare an array */ int intArray[8]; // initiapze elements of array n to 0 for ( i = 0; i < 8; i++ ) { intArray[ i ] = 0; // set elements to default value of 0; } printf("Array with default data."); /* Display elements of an array.*/ display(intArray,8); /* Operation : Insertion Add elements in the array */ for(i = 0; i < 8; i++) { /* place value of i at index i. */ printf("Adding %d at index %d ",i,i); intArray[i] = i; } printf(" "); printf("Array after adding data. "); display(intArray,8); /* Operation : Insertion Element at any location can be updated directly */ int index = 5; intArray[index] = 10; printf("Array after updating element at index %d. ",index); display(intArray,8); /* Operation : Search using index Search an element using index.*/ printf("Data at index %d:%d " ,index,intArray[index]); /* Operation : Search using value Search an element using value.*/ int value = 4; for(i = 0; i < 8; i++) { if(intArray[i] == value ){ printf("value %d Found at index %d ", intArray[i],i); break; } } return 0; }
Output
If we compile and run the above program then it would produce following output −
Array with default data.Array : [ 0 0 0 0 0 0 0 0 ] Adding 0 at index 0 Adding 1 at index 1 Adding 2 at index 2 Adding 3 at index 3 Adding 4 at index 4 Adding 5 at index 5 Adding 6 at index 6 Adding 7 at index 7 Array after adding data. Array : [ 0 1 2 3 4 5 6 7 ] Array after updating element at index 5. Array : [ 0 1 2 3 4 10 6 7 ] Data at index 5: 10 4 Found at index 4Advertisements