0% found this document useful (0 votes)
27 views13 pages

Arrays in C++

Uploaded by

Zaoui Hanane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
27 views13 pages

Arrays in C++

Uploaded by

Zaoui Hanane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 13
‘srini2022 21:66, Arrays in CIC+ - GeeksforGeoks Arrays in C/C++ Difficulty Level: Easy @ Last Updated : 27 Jan, 2022 An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C/C++ can store derived data types such as the structures, pointers etc Given below is the picture representation of an array, PPE PEP I 23 ae | 78 0 « Array Indices Array Length=9 First Index =0 Last Index=8 Why do we need arrays? We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage em with normal variables. The idea of an array is to represent many instances in one variable hitps:wwn.geeksforgecks.orgiarrays-in-ccpp ans 71032022 21:56 Arrays in CIC - GoekslorGeoks Start Your Coding Journey Now! Note: In above image int a[3]={[0...1]=3}; this kind of declaration has been obsolete since GCC 2.5 There are various ways in which we can declare an array. It can be done by specifying its type and size, by initializing it or both Array declaration by specifying size oS Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Pyth // With recent C/C++ versions, we can also // declare an array of user specified size int n = 10; int arr2{n]; Array declaration by initializing elements c 7/ Array declaration by initializing elements int arr[] = { 10, 20, 3@, 40 } a hitps:wwn.geeksforgeeks.orglarrays-in-ccppy ans. ‘srini2022 21:66, Arrays in CIC#+ - GeeksforGeoks Start Your Coding Journey Now! // Array declaration by specifying size and initializing // elements int arr[6] = { 10, 20, 30, 40 } // Compiler creates an array of size 6, initializes first // 4 elements as specified by user and rest two elements as // ®. above is same as “int arr[] = {10, 20, 30, 48, @, 0)" Advantages of an Array in C/C++: 1. Random access of elements using array index 2. Use of fewer 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 fewer line of code. Disadvantages of an Array in C/C++: 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. Facts about Array in C/C++: * Accessing Array Elements: Array elements are accessed by using an integer index. Array index starts with 0 and goes till size of array minus 1 * Name of the array is also a pointer to the first element of array. hitps:wwn.geeksforgeeks.orglarrays-in-ccppy 33 71032022 21:56 Arrays in CIC - GoeksforGeoks: Start Your Coding Journey Now! Example: c #include int main() t int arr[5]5 arr[@] = 53 arr[2] = -10; arr[3 / 2] = 2; // this is same as arr[1] = 2 are[3] = arr[0]; printf ("%d Xd %d %d", arr[o], arr[1], arr[2], arr[3]); return 0; C++ ¥ include using namespace std; int main() t int arr[5]5 arr[@] = 55 arr[2] = -10; J/ this is same as arr[1] = 2 a hitps:wwn.geeksforgeeks.orglarrays-in-ccppy ‘srini2022 21:66, Start Your Coding Journey Now! return 5 Output 52-105 No Index Out of bound Checking: There is no index out of bounds checking in C/C++, for example, the following program compiles fine but may produce unexpected output when run, c // This C program compiles fine // as index out of bound // is not checked in Cc. #include int main() { int arr[2]5 printf("xd ", arr[3])5 printf("%d ", arr[-2])5 return 3 C++ // This C++ program compiles fine // as index out of bound // is not checked in Cc. include sing namespace std; int main() hitps:wwn.geeksforgeoks orglarrays-in-c-cppl Arrays in CIC#+ - GeeksforGeoks 5113 ‘srini2022 21:66, Arrays in CIC#+ - GeeksforGeoks Start Your Coding Journey Now! return 5 Output -449684907 4195777 In C, itis not a compiler error to initialize an array with more elements than the specified size. For example, the below program compiles fine and shows just Warning. #include int main() { // Array declaration by initializing it // with more elements than specified size. int arr[2] = { 10, 20, 30, 40, 50 }; return 0; Warnings: prog.c: In function 'main': prog.c:7:25: warning: excess elements in array initializer int arr[2] = { 10, 20, 30, 40, 58 }5 prog.c:7:25: note: (near initialization for ‘arr') prog.c:7:29: warning: excess elements in array initializer int arr[2] = { 10, 20, 30, 40, 5@ }; prog.c:7:29: note: (near initialization for ‘arr’) prog.c:7:33: warning: excess elem . in array initializer hitps:wwn.geeksforgeeks.orglarrays-in-ccppy ans, ‘srini2022 21:66, Start Your Coding Journey Now! Arrays in CIC#+ - GeeksforGeoks the program generates compiler error “error: too many initializers for ‘int [2]"". The elements are stored at contiguous memory locations Example: c // © program to demonstrate that // array elements are stored // contiguous locations #include int main() t MW MW MW MW We Ms MW an array of 10 integers. If arr[@] is stored at address x, then arr[1] is stored at x + sizeof(int) arr[2] is stored at x + sizeof(int) + sizeof(int) and so on. int arr[5], 43 printf("Size of integer in this compiler is %lu\n", sizeof(int)); for (i = 0; i < 53 int) // The use of '&' before a variable name, yields // address of variable. printf("Address arr[%d] is %p\n", i, &arr[i])5 return @; C++ ' C++ program to demonstrate that array elements / are stored contiguous locations iinclude hitps:wwn.geeksforgeoks orglarrays-in-c-cppl m3 ‘srini2022 21:66, Start Your Coding Journey Now! J) address x, then arr[i) is // stored at x + sizeof(int) // arr[2] is stored at x + // sizeof(int) + sizeof(int) // and so on. int arr[S], 43 Arrays in CIC#+ - GeeksforGeoks cout << "Size of integer in this compiler is * << sizeof(int) << "\n"; for (i = 0; i < 55 itt) // The use of '&' before a variable name, yields // address of variable. cout << ‘Address arr[" << i << "] is using namespace std; int main() { int arr[6]={11,12,13,14,15,16); JI Way 1 for(int i0;i<6;i++) cout<

You might also like