2:
Arrays:
Defination of
arrays:
An array is collection of items
stored at contiguous memory
locations. The idea is to store
multiple items of same type
together. This makes it easier to
calculate the position of each
element by simply adding an
offset to a base value, i.e., the
memory location of the first
element of the array (generally
denoted by the name of the
array).
For simplicity, we can think of
an array a fleet of stairs where
on each step is placed a value
(let’s say one of your friends).
Here, you can identify the
location of any of your friends
by simply knowing the count of
the step they are on.
Types an
indexing in
aray:
0 (zero-based indexing): The
first element of the array is
indexed by subscript of 0
1 (one-based indexing): The
first element of the array is
indexed by subscript of 1
n (n-based indexing): The base
index of an array can be freely
chosen. Usually programming
languages allowing n-based
indexing also allow negative
index values and other scalar
data types like enumerations,
or characters may be used as
an array index.
Advantages of
using array:
Arrays allow random access of
elements. This makes accessing
elements by position faster.
Arrays have better cache
locality that can make a pretty
big difference in performance.
Examples :
// A character array in
C/C++/Java
char arr1[] = {'g', 'e', 'e', 'k', 's'};
// An Integer array in
C/C++/Java
int arr2[] = {10, 20, 30, 40, 50};
int n[ ] = { 2,3,15,8,48,13 };
element 2 3 15 8 48 13
index 0 1 2 3 4 5
0,1,2,3,4 and 5 are the indices.
It is like these are the identities
of 6 different elements of the
array. Index starts from 0. So,
the first element has index 0.
We access the elements of an
array by writing
array_name[index].
Here,
n[0] is 2
n[1] is 3
n[2] is 15
n[3] is 8
n[4] is 48
n[5] is 13
example:
#include <iostream>
int main(){
int marks[3];
float average;
cout << "Enter marks of first
student" << endl;
cin >> marks[0];
cout << "Enter marks of
second student" << endl;
cin >> marks[1];
cout << "Enter marks of third
student" << endl;
cin >> marks[2];
average = ( marks[0] +
marks[1] + marks[2] )/ 3.0;
cout << "Average marks : "
<< average << endl;
return 0;
}
using for loop:
#include <iostream>
int main(){
/* initializing elements of
array n */
for ( i = 0; i<10; i++ )
{
cout << "Enter value of n["
<< i << "]"<< endl;
cin >> n[i];
}
/* printing the values of
elements of array */
for (j = 0; j < 10; j++ )
{
cout << "n[" << j << "] = "
<< n[j] << endl;
}
return 0;
}
passing the whole array of a
function:
#include <iostream>
void display(int a)
{
std::cout << a << std::endl;
}
int main(){
int n[ ] = { 20, 30, 23, 4, 5, 2,
41, 8 };
display(n[2]);
return 0;
}
no 3
#include<iostream>
#include<time.h>
#include<stdlib.h>
void poulateArray(int [][10]);
void showElements(int [][10]);
void showLargestElement(int
[][10]);
void transposeArray(int [][10]);
using namespace std;
int main()
{
int option,arr[10][10]= {0};
bool arrEmpty=true;
while(1)
{
cout<<“1.Press 1 to populate a
two-dimensional array with
integers from 1 to 100.\n”
<<“2.Press 2 to display the
array elements.\n”
<<“3.Press 3 to display the
largest element present in the
array along with its row and
column index.\n”
<<“4.Press 4 to find and show
the transpose of the array.\n\n”
<<“Please select a option, use
numbers from 1 to 5: “;
cin>>option;
cout<<“\n—————————
—————————–\n\n”;
switch(option)
{
case 1:
poulateArray(arr);
arrEmpty=false;
break;
case 2:
if(arrEmpty==true)
{
cout<<“Sorry the array is
empty, first populate it by
pressing 1 to perform this
task\n\n\n\n”;
}
else
{
showElements(arr);
}
break;
case 3:
if(arrEmpty==true)
{
cout<<“Sorry the array is
empty, first populate it by
pressing 1 to perform this
task\n\n\n\n”;
}
else
{
showLargestElement(arr);
}
break;
case 4:
if(arrEmpty==true)
{
cout<<“Sorry the array is
empty, first populate it by
pressing 1 to perform this
task\n\n\n\n”;
}
else
{
transposeArray(arr);
}
break;
case 5:
exit(0);
break;
default:
cout<<“You entered wrong
option.\n\n”;
}
}
return 0;
}
void poulateArray(int ar[][10])
{
int num;
srand(time(0));
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
num=1+rand()%100;
ar[i][j]=num;
}
}
cout<<“Array has been
populated successfully!\n\n”;
}
void showElements(int ar[][10])
{
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
cout<<ar[i][j]<<“\t”;
}
cout<<endl;
}
}
void showLargestElement(int
ar[][10])
{
int largest=0,i,j,row,colum;
for( i=0; i<10; i++)
{
for( j=0; j<10; j++)
{
if(ar[i][j]>largest)
{
largest=ar[i][j];
row=i+1;
colum=j+1;
}
}
}
cout<<“Largest element is
“<<largest<<” which is at row
“<<row<<” and colum
“<<colum<<“\n\n”;
}
void transposeArray(int
ar[][10])
{
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
cout<<ar[j][i]<<“\t”;
}
cout<<“\n”;
}
}