

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to arrays, their declaration, initialization, and access in c/c++ programming. It includes examples of declaring and initializing arrays of different data types and sizes, as well as accessing and displaying their elements. The document also provides incomplete and complete code examples for initializing arrays using loops and initializer lists.
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Arrays An array is a sequence of objects all having same data types, stored at contiguous memory locations. The objects in an array are called โelementsโ of the array, array elements are accessed using an index starting from 0 (in C/C++) Declaring Array type arrayName [ size ]; Examples int A[10]; //This will create an array of 10 integers int A[10],B[10]; //This will create two arrays of 10 integers each float A[10]; //This will create an array of 10 floats char A[10]; //This will create an array of 10 floats Exceptions An array can also be declared using a variable (The variable should must be declared and initialized before using with array) int size=10; int A[size]; Initializing Array Arrays can be initialized in many ways INITIALIZATIONโ 1 (just like variables) int A[5]; A[0]=1; A[1]=2; A[2]=3; A[3]=4; A[4]=5; INITIALIZATIONโ 2 (using loops) int A[5]; for(int n=0;n<5;n++) A[n]=n+1; INITIALIZATIONโ 3 (using Initializer list) int A[5]={1,2,3,4,5]; int A[5]={1,2,3}; //rest of the elements will be initialized to 0 Accessing and Displaying the value of Array Elements Array elements can be accessed and used by means of elements indexes int A[5]={2,4,6,8,10}; cout<<A[0]<<A[1]<< A[2]<< A[3]<< A[4]; OR for(int n=0; n<5;n++) cout<<A[n];
The following program declares an array of 5 integers, initializes the array using an initializer list (complete) and displays the values stored in array elements #include<stdio.h> #include<conio.h> #include<iostream.h> main(){ int A[5]={1,2,3,4,5}; int index; for(index=0;index<5;index++) cout<<A[index]<<endl; }
The following program declares an array of 5 integers, initializes the array using an initializer list(incomplete) and displays the values stored in array elements #include<stdio.h> #include<conio.h> #include<iostream.h> main(){ int A[5]={1,2,3,4,5}; int index; for(index=0;index<5;index++) cout<<A[index]<<endl; }
The following program declares two arrays of 5 integers each, initializes the arrays using initializer lists, and displays the sum of their values #include<stdio.h> #include<conio.h> #include<iostream.h> main(){ int A[5]={1,2,3,4,5}; int B[5]={6,7,8,9,10}; int index; //displaying elements of Array A for(index=0;index<5;index++) cout<<โA [โ<<index+1<<โ] = โ<<A[index]<<endl; //displaying elements of Array B for(index=0;index<5;index++) cout<<โB [โ<<index+1<<โ] = โ<<B[index]<<endl; cout<<โDisplaying sum of elements of two arraysโ<<endl; for(index=0; index<5;index++) cout<<A[index]+B[index]<<endl }
The following program declares an array of 5 elements, asks user to input the value of 5 integers that are to be stored in the array, and displays the values #include<stdio.h> #include<conio.h> #include<iostream.h> main(){ int A[5]; int index;