Professional Documents
Culture Documents
Lec-15 Multidimensional Arrays
Lec-15 Multidimensional Arrays
Lecture # 15
Arrays
Array is a collection of variables of the same data type placed
contiguously in memory.
The items or elements of array are accessed by index number.
Arrays can be used to store basic data types, i.e. int, char and
float etc.
Arrays can also be used as data members in classes.
Array can also be used to hold objects.
Arrays can also by used in structures, i.e. we can create an
array of variable of structure types and we can use array even
inside the structures for defining its members etc.
int mark[] = {19, 10, 8, 17, 9};
Declaration of an Array
// 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};
Initializing
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
// arrays example
#include <iostream>
using namespace std;
int foo [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for ( n=0 ; n<5 ; ++n )
{
result += foo[n];
}
cout << result; return 0;
}
#include <iostream>
using namespace std;
int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0;
}
using namespace std;
#include <iomanip>
using std::setw; int main ()
{ int n[ 10 ]; // n is an array of 10 integers // initialize
elements of array n to 0
for ( int i = 0; i < 10; i++ )
{
n[ i ] = i + 100;
}
cout << "Element" << setw( 13 ) << "Value" << endl;
for ( int j = 0; j < 10; j++ )
{
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
}
return 0;
}
Arrays Example 1
#include<iostream.h>
#include<conio.h>
void main(void)
{int t[7], sum=0;
for(int a=0;a<=6;a++)
{ cout<<"Enter Temperature for day "<<a+1<<" ";
cin>>t[a];
sum += t[a];
}
for(a=0;a<=6;a++)
cout<<"Temperature of day "<<a+1<<" is
"<<t[a]<<endl;
cout<<"\nAverage temperature is "<<sum/7;
getch();
}
Arrays Example 1
Output
Enter Temperature for day 1 34
Enter Temperature for day 2 32
Enter Temperature for day 3 33
Enter Temperature for day 4 35
Enter Temperature for day 5 36
Enter Temperature for day 6 41
Enter Temperature for day 7 32
Temperature of day 1 is 34
Temperature of day 2 is 32
Temperature of day 3 is 33
Temperature of day 4 is 35
Temperature of day 5 is 36
Temperature of day 6 is 41
Temperature of day 7 is 32
Average temperature is 34
#include<iostream.h>
void main(void)
Arrays Example
{
clrscr();
2
int a[5], b[5];
for(int n=0;n<=4;n++)
{ cout<<"Enter value in element no. "<<n+1<<“ “;
cin>>a[n];
cout<<endl;
b[n] = a[n] * a[n];
}
cout<<"Array A\tArray B"<<endl;
for(n=0;n<=4;n++)
cout<<a[n]<<"\t"<<b[n]<<endl;
}
Arrays Example 2
Output
Enter value in element no. 1 3
Enter value in element no. 2 4
Enter value in element no. 3 5
Enter value in element no. 4 6
Enter value in element no. 5 7
Array A Array B
39
4 16
5 25
6 36
7 49
Multidimensional array
int a[5]={20,22,50,3,11}
int high,i;
high=a[0];
for (i=1 ;i< 5;i++)
{
if (a[i]> high)
high=a[i];
}
{ cout<< “ higher element”<<high;}
Practice questions
Write a C++ program to find the element that
appears once in an array of integers and every
other element appears twice.
Write a program to display name of months
using arrays. Also display the total number of
characters (length) of each month.
Task
#include <iostream>
{ const int numRows = 10;
const int numCols = 10;
int product[numRows][numCols] = { 0 };
for (int row = 0; row < numRows; ++row)
for (int col = 0; col < numCols; ++col)
product[row][col] = row * col;
// Print the table
for (int row = 1; row < numRows; ++row)
{
for (int col = 1; col < numCols; ++col)
std::cout << product[row][col] << "\t";
std::cout << '\n';
}
return 0;
}