Array Fun
Array Fun
Array
An array is a collection of elements of the same type.
f[4] = 2.5;
m[2] = 4;
cout << f[4]; // outputs f[4], which is 2.5
Various ways to initialize an array
In the above example, we have just declared the array and later we initialized it with the values input
by user. However you can also initialize the array during declaration like this:
int arr[5] = {1, 2, 3, 4 ,5}; OR (both are same) int arr[] = {1, 2, 3, 4, 5};
int children[3];
children[0] = 2;
c[0]=‘c’;
children[1] = 12;
c[1]=‘a’
Exercises
What is the output of the following code?
char symbol[3] = {’a’, ’b’, ’c’};
for (int i = 0; i < 3; i++)
cout << symbol[i];
#include<iostream>
using namespace std;
int main()
{
int ar1[3]={10,20,30}, ar2[3];
ar2[0]=ar1[2];
cout<<“element 0 in ar2= "<<ar2[0];
return 0;
}
Example elements array summation
#include<iostream>
using namespace std;
int main()
{
int sum=0, ar[3];
for (int i=0; i<3 ;i++)
{
cin>>ar[i];
sum =sum+ar[i];
cout<<"sum of array elements= "<<sum;
//Reads in 5 scores and shows the highest score.
#include <iostream>
int main( )
{
using namespace std;
int i, score[5], max=0;
cout << "Enter 5 scores:\n";
cin >> score[0];
max = score[0];
for (i = 1; i < 5; i++)
{
cin >> score[i];
if (score[i] > max)
max = score[i]; //max is the largest of the values score[0],..., score[i].
}
cout << "The highest score is " << max << endl;
Return 0;
}
Array of int elements find the sum of odd
and sum of even elements
int s=3, sume=0,sumo=0, ar[s]={10,25,30};
for (int i=0; i<s ;i++)
{
if (ar[i] %2 ==0)
sume=sume + ar[i];
else
sumo=sumo + ar[i];
}
cout<<"sum of even= "<<sume<<"sumod"<<sumo;
A two-dimensional array
A two-dimensional array is implemented as an “array of arrays.”
For example “int A[15][30]” declares A to be an array of 30 objects, each of which is an array of 15 integers.
An element in such an array is indexed as A[i][j], where i is in the range 0 to 14 and j is in the range 0 to 29.
When declaring an array, we can initialize its values by enclosing the elements in curly braces ({...}). When
doing so, we do not have to specify the size of the array, since the compiler can figure this out.
array. Such a two-dimensional array is sometimes also called a matrix. In C++, we declare a two-
dimensional array as follows:
int M[8][10]; // matrix with 8 rows and 10 columns
This statement creates a two-dimensional “array of arrays,” M, which is 8×10, having 8 rows and
10 columns. That is, M is an array of length 8 such that each element of M is an array of length 10
of integers.
Figure: A two-dimensional integer array that has 8 rows and 10 columns. The value of M[3][5] is
100 and the value of M[6][2] is 632.
Arrays are nice and simple for storing things in a certain order, but they have drawbacks.
They are not very adaptable. For instance, we have to fix the size n of an array in
advance, which makes resizing an array difficult. Insertions and deletions are difficult
because elements need to be shifted around to make space for insertion or to fill empty
positions after deletion.
int arr[4][2] = {
{12, 56},
{21, 33},
{34, 80},
{13, 78}
};
int arr[4][2] = {12, 56, 21, 33, 34, 80, 13, 78};
#include<iostream>
main( )
int arr[4][2] = {
{ 10, 11 },
{ 20, 21 },
{ 30, 31 },
{ 40, 41 }
};
int i,j;
cout<<"Printing a 2D Array:\n";
for(i=0;i<4;i++)
for(j=0;j<2;j++)
cout<<"\t"<<arr[i][j];
cout<<endl;
}
#include<iostream>
using namespace std;
main( )
{
int s[2][2];
int i, j;
cout<<"\n2D Array Input:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
cin>>s[i][j];
void greet()
{
cout << "Hello there!";
}
int main() {
#include <iostream>
using namespace std;
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}
int main() {
int num1 = 5;
double num2 = 5.5;
// calling the function
displayNum(num1, num2);
return 0;
}
#include <iostream>
using namespace std;
// declaring a function
int add(int a, int b) {
return (a + b);
}
int main() {
int sum;
sum = add(100, 78);
cout << "100 + 78 = " << sum << endl;
return 0;
}