Anda di halaman 1dari 37

Assigement no

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};

// Item at i'th index in array is


typically accessed
// as "arr[i]". For example
arr1[0] gives us 'g'
// and arr2[3] gives us 40.
Initalization of an array:
The initializer for an array is a
comma-separated list of
constant expressions enclosed
in braces ( { } ). The initializer is
preceded by an equal sign ( = ).
You do not need to initialize all
elements in an array.
One
dimensional
array:
Image result for one
dimensional
arraywww.quora.com
A one-dimensional array (or
single dimension array) is a
type of linear array. Accessing
its elements involves a single
subscript which can either
represent a row or column
index. As an example consider
the C declaration int
anArrayName[10]; Syntax :
datatype
anArrayName[sizeofArray];
2 dimensional array:
2-dimensional arrays provide
most of this capability. Like a
1D array, a 2D array is a
collection of data cells, all of
the same type, which can be
given a single name. However,
a 2D array is organized as a
matrix with a number of rows
and columns.
How to declare an array?
datatype array_name [
array_size ];

For example, take an integer


array 'n'.
int n[6];

n[ ] is used to denote an array


named 'n'.

So, n[6] means that 'n' is an


array of 6 integers. Here, 6 is
the size of the array i.e., there
are 6 elements of array 'n'.
Giving array size i.e. 6 is
necessary because the compiler
needs to allocate space to that
many integers. The compiler
determines the size of an array
by calculating the number of
elements of the array.

Here 'int n[6]' will allocate


space to 6 integers.
We can also declare an array by
another method.

int n[ ] = { 2,3,15,8,48,13 };

In this case, we are declaring


and assigning values to the
array at the same time. Hence,
no need to specify the array size
because the compiler gets it
from { 2,3,15,8,48,13 }.
Following is the pictorial view
of the array.

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(){

using namespace std;

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(){

using namespace std;

int n[10]; /* declaring n as an


array of 10 integers */
int i,j;

/* 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”;
}
}

Anda mungkin juga menyukai