Lec 11 Arrays
Lec 11 Arrays
Lec 11 Arrays
1. Arrays
a. Array declaration
b. Array initialization
c. Access element of an array
2. Multi-dimensions arrays
d. Declaration & initialization
3. Passing array to a function
4. Array in a structure
5. Array as a class member
6. Arrays of objects
Arrays:
1. It is a group of variables of similar data types referred to by a single element.
2. Its elements are stored in a contiguous memory location.
3. The size of the array should be mentioned while declaring it.
4. Array elements are always counted from zero (0) onward.
5. Array elements can be accessed using the position of the element in the array.
6. The array can have one or more dimensions.
Contiguous memory locations:
Disadvantages:-
• Size Limit: We can store only the fixed size of elements in the array. It doesn’t grow its
size at runtime.
How to declare an array?
The first element is mark[0], the second element is mark[1] and so on.
Here, we haven't specified the size. However, the compiler knows its size is 5 as we are initializing it
with 5 elements.
Here
Examples:
Two dimensional array: int two_d[10][20];
Three dimensional array: int three_d[10][20][30];
Enter sales for district 1, month 1: 3964.23 cout << “\n\n”;
Enter sales for district 1, month 2: 4135.87
Example Program:
Enter sales for district 1, month 3: 4397.98
cout << “
cout << “
Months\n;
1 2 3”;
Enter
// displays sales chart using 2-d sales
array for district 2, month 1: 867.75 for(d=0; d<DISTRICTS; d++)
#include <iostream> Enter sales for district 2, month 2: 923.59
{
two-dimensional array to store sales figures for several districts and several months:
#include <iomanip> //for Enter sales foretc.
setprecision, district 2, month 3: 1037.01cout <<”\nDistrict “ << d+1;
using namespace std; Enter sales for district 3, month 1: 12.77 for(m=0; m<MONTHS; m++) //display array values
Enter sales for
const int DISTRICTS = 4; //array dimensions district 3, month 2: 378.32 cout << setiosflags(ios::fixed) //not exponential
const int MONTHS = 3; Enter sales for district 3, month 3: 798.22 << setiosflags(ios::showpoint) //always use point
int main() Enter sales for district 4, month 1: 2983.53 << setprecision(2) //digits to right
{ Enter sales for district 4, month 2: 3983.73 << setw(10) //field width
int d, m; Enter sales for district 4, month 3: 9494.98 << sales[d][m]; //get number from array
double sales[DISTRICTS][MONTHS]; //two-dimensional array }//definition
Month //end for(d)
cout << endl; 1. 2 cout
3 << endl;
for(d=0; d<DISTRICTS; District
d++) //get
1 array values 4135.87 4397.98
3964.23 return 0; } //end main
for(m=0; m<MONTHS; m++) District 2 867.75 923.59 1037.01
{ District 3 12.77 378.32 798.22
cout << “Enter sales for District
district “4<< d+1;
2983.53 3983.72 9494.98
cout << “, month “ << m+1 << “: “;
cin >> sales[d][m];
}
Function Call with Array
Passing Array to a function: Arguments
Arrays can be used as arguments to functions.
Function Definition
with Array
Arguments
Arrays as Class Member Data:
Arrays of objects:
// Driver code
int main()Example of array of object:
{
////This
C++ is an array
program of objects having
to implement // Defining the function outside
////maximum limit
the above of 30 Employees
approach // the class
Employee emp[30];
#include<iostream> void Employee::getdata()
int n, i;
using namespace std; {
cout << "Enter Number of Employees - ";
cout << "Enter Id : ";
cin >> n;
class Employee cin >> id;
{ cout << "Enter Name : ";
// Accessing
int id; the function
cin >> name;
for(i = 0;
char i < n; i++)
name[30]; }
emp[i].getdata();
public:
// Defining the function outside
cout//<<Declaration
"Employee Data - " << endl;
of function // the class
void getdata(); void Employee::putdata()
// Accessing the function
{
for(i
// =Declaration
0; i < n; i++)
of function cout << id << " ";
emp[i].putdata();
void putdata(); cout << name << " ";
} }; cout << endl;
}
// Function to get item details
Example of array of object:
void item::getitem()
{
int main() #include<iostream> cout << "Item Name = ";
{ using namespace std; cin >> name;
item t[size];
class item cout << "Price = ";
for(int i ={ 0; i < size; i++) cin >> price;
{ char name[30]; }
cout << "Item : " <<
int price;
(i public:
+ 1) << "\n"; // Function to print item
t[i].getitem();
void getitem(); // details
} void printitem(); void item ::printitem()
}; {
for(int i = 0; i < size; i++) cout << "Name : " << name <<
{ "\n";
cout << "Item Details : " << cout << "Price : " << price <<
(i + 1) << "\n"; "\n";
t[i].printitem(); }
}
} const int size = 3;
Advantages of Array of Objects:
String Declaration in C