CL103 - Computer Programming - Lab # 03 (Arrays)
CL103 - Computer Programming - Lab # 03 (Arrays)
Arrays Overview
Declaring Arrays
Initializing arrays
Accessing the values of an array
Default Initialization of Arrays
Sizeof operator for arrays
Operations of Array
Array & Functions
Parallel Arrays
Two Dimensional Arrays
Declaration of Two-Dimensional Array
Examples of Two Dimensional Arrays:
Default Initialization of 2-Dimensional Arrays
Two Dimensional Array in Functions
Multidimensional Arrays
Live Demonstration in Lab
Instructions:
Arrays Overview
An array is a series of elements of the same type placed in contiguous memory locations that can be
individually referenced by adding an index to a unique identifier.
An array is a group of items that can be identified as similar because they are of the same nature.
An array of characters
An array of cards
Figure 1: One-Dimensional Array
Arrays come in two flavors: one dimensional and multi-dimensional array. Every one of the pictures
above represents a single dimensional array.
Declaring Arrays
Initializing arrays
billy[0]=16;
billy[1]=2;
billy[2]=77;
billy[3]=40;
billy[4]=12071;
int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0;
}
/*Output:
12206
*/
Heres an example, DAYS, that sets 12 array elements in the array days_per_month to the number of
days in each month.
#include <iostream>
#include <conio.h>
void main()
{
int month, day, total_days;
cout << "Total days from start of year is: " << total_days << endl;
_getch();
}
Enter month (1 to 12): 3
Enter day (1 to 31): 5
Total days from start of year is: 64
#include <iostream>
using namespace std;
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
/*
2nd member = 720.52
5th member = 6.28
*/
Using this approach, each member of the array can have its value accessed. Here is an example:
#include <iostream>
using namespace std;
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 468.78
Distance 5: 6.28
*/
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
// Using the sizeof operator to get the dimension of the array
int index = sizeof(distance) / sizeof(double);
return 0;
}
/*
Array members and their values
Distance : 144.14
Distance : 2720.52
Distance : 396.08
Distance : 4468.78
Distance : 56.28
*/
You can use such a constant in a for loop to scan the array and access each of its members. Here is an
example:
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
Operations of Array
#include <iostream>
#include <conio.h>
using namespace std;
const int SIZE = 7;
void main()
{
int a[SIZE], i;
cout << "Enter <<SIZE<< numbers : ";
for(i=0; i<SIZE; i++)
{
cin >> a[i]; // read array
}
cout << "\n\nNumbers are : ";
for(i=0; i< SIZE; i++)
{
cout << " , "<< a[i]; // print array
}
int sum = 0;
for(i=0; i< SIZE; i++)
{
sum = sum + a[i];
}
cout<<"\n\nSum of array Elements: "<<sum;
int mul=1;
for(i=0; i< SIZE; i++)
{
mul = mul * a[i];
}
cout<<"\n\nMultiplication of array Elements: "<<mul;
getch();
}
/*Output:
Enter 7 numbers : 1 2 3 4 5 6 7
Numbers are : , 1 , 2 , 3 , 4 , 5 , 6 , 7
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04, 364.55,
6234.12};
return 0;
}
Parallel Arrays
Parallel arrays are several arrays with the same number of elements that work in tandem to organize
data. True beauty of parallel arrays, is that each array may be of a different data type
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int a[3][3], i, j;
cout << "\n\t Enter matrix of 3*3 : \n";
for(i=0; i<3; i++)
{
cout<<"\t\t\t\t";
for(j=0; j<3; j++)
{
cin >> a[i][j]; //read 3*3 array
}
}
cout << "\n\t\t
Matrix is : \n";
for(i=0; i<3; i++)
{
cout<<"\n\t\t\t\t";
for(j=0; j<3; j++)
{
cout <<a[i][j]<<"\t";
}
}
getch();
}
/* Output:
Enter matrix of 3*3 :
1 2 3
#include <iostream>
#include <conio.h>
void main()
{
int d, m;
double sales[DISTRICTS][MONTHS];
_getch();
}
/*Sample Output
Enter sales for district 1, month 1: 55
Enter sales for district 1, month 2: 32.5
Enter sales for district 1, month 3: 65.2
Enter sales for district 2, month 1: 95.1
Enter sales for district 2, month 2: 25.4
Enter sales for district 2, month 3: 45.0
Enter sales for district 3, month 1: 63.8
Enter sales for district 3, month 2: 45.9
Enter sales for district 3, month 3: 25.1
Month 1 2 3
District 1 55 32.5 65.2
District 2 95.1 25.4 45
District 3 63.8 45.9 25.1
District 4 12.2 32.1 75.66
*/
void main()
{
int d, m;
double sales[DISTRICTS][MONTHS]
= {{ 1432.07, 234.50, 654.01 },
{ 322.00, 13838.32, 17589.88 },
{ 9328.34, 934.00, 4492.30 },
{ 12838.29, 2332.63, 32.93 } };
_getch();
}
Output
Month 1 2 3
District 1 1432.07 234.5 654.01
District 2 322 13838.3 17589.9
District 3 9328.34 934 4492.3
District 4 12838.3 2332.63 32.93
#include <iostream>
using namespace std;
int main()
{
// A 2-Dimensional array
double distance[2][4] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04,
364.55, 6234.12};
print(distance);
getch();
return 0;
}
So how we can remove this error? But it seems to be valid for single dimensional array. Now change the
prototype of the function like this.
#include <iostream>
#include <conio.h>
void main()
{
_getch();
}
Output
Month 1 2 3
District 1 1432.07 234.5 654.01
District 2 322 13838.3 17589.9
District 3 9328.34 934 4492.3
District 4 12838.3 2332.63 32.93
Multidimensional Arrays
An array having more than two dimensions is called a multidimensional array.
#include<iostream>
#include<conio.h>
using namespace std;
int arraySum(int[]);
void arrayReferenceTest(int[]);
void printArray(int[]);
void changeMe(int&);
void initializeMat(int[][COLS]);
void squareMatElement(int[][COLS]);
void printMat(int[][COLS]);
void initializeArray1(int[]);
void main()
{
int nums[SIZE];
cout<<"Enter "<<SIZE<<" elements: ";
initializeArray1(nums);
cout<<"Entered array is: ";
for(int i=0; i<SIZE; i++)
{
cout<<nums[i]<<", ";
}
cout<<"\nPrint the array in reverse order:"<<endl;
for(int i=SIZE-1; i>=0;i--)
{
cout<<nums[i]<<" ";
}
int xy = arraySum(nums);
cout<<"\n Array sum : "<<xy<<endl;
printArray(nums);
arrayReferenceTest(nums);
cout<<"\n\n Modified array by refference test: ";
printArray(nums);
int mat2d[ROWS][COLS];
int matrix[ROWS][COLS];
initializeMat(matrix);
cout<<"\nAfter Initialization: ";
printMat(matrix);
cout<<"\nAfter Square: ";
squareMatElement(matrix);
printMat(matrix);
int matrix3d[2][ROWS][COLS];
initializeMat(matrix3d[0]);
initializeMat(matrix3d[1]);
cout<<"\nAfter Initialization: ";
cout<<"\nLayer -1 [0]\n";
printMat(matrix3d[0]);
cout<<"\nLayer -2 [1]\n";
printMat(matrix3d[1]);
cout<<endl;
system("PAUSE");
}
void initializeArray1(int t[])
{
for(int i=0; i<SIZE; i++)
{
//cin>>nums[i];
t[i] = i*i;
}
}
int arraySum(int a[])
{
int sum=0;
for(int i = 0; i< SIZE; i++)
{
sum+= a[i]; //sum= sum + a[i];
}
return(sum);
}
void printArray(int x[])
{
cout<<"\n";
for(int i=0; i<SIZE; i++)
{
cout<<"["<<i<<"]: "<<x[i]<<" ";
}
cout<<"\n";
}
}
/* Output
After Initialization:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
Layer -2 [1]
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
Press any key to continue . . .
*/