Tutorial 7 - Concordia University

Report
Tutorial 7
Shruti Rathee
Concordia University
Arrays
 An array is a group of items that can be identified as similar
because they are of the same nature.
 Arrays come in two flavors: one dimensional and multidimensional arrays.
On dimensional array
 Declaration :
DataType ArrayName[order]
 Int num[5];
 Float mark[10];
 Double angle[360];
Initialization
 DataType ArrayName[dimension] = { element1, element2, …,
elementn};
 int number[2] = {18, 12,};
 double distance[5] = {44.14, 720.52, 96.08, 468.78, 6.28};
Access an array
 #include <iostream>
using namespace std;
int main()
{
int arr[] = {4, 7, 9, 4, 6};
cout << ”array 1: " << array [0] << endl;
cout << "array 2: " << array [1] << endl;
cout << "array 3: " << array [2] << endl;
cout << "array 4: " << array [3] << endl;
cout << "arrayDistance 5: " <array [4] << endl;
return 0;
}
Excercise
 Take input from user for an array of length 10 and find the
sum of all elelments in that array.
 Declare an array like numbers[] = {8, 25, 36, 44, 52, 60, 75,
89} and then ask the user to input a number using cin and
check whether it is in array.
Solution 1.
#include <iostream>
using namespace std;
int main()
{
// We know that we need a constant number of elements
const int max = 10;
int number[max];
// We will calculate their sum
int sum = 0;
cout << "Please type 10 integers.\n”;
for( int i = 0; i < max; i++ )
{
cout << "Number " << i + 1 << ": ";
cin >> number[i];
sum += number[i];
}
cout << "\n\nThe sum of these numbers is " << Sum << "\n\n";
return 0;
}
Solution 2
#include <iostream>
using namespace std;
int main()
{
// Declare the members of the array
int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89};
int find;
int i, m = 8;
cout << "Enter a number to search: ";
cin >> find;
for (i = 0; (i < m) && (Numbers[i] != Find); ++i)
continue;
/ Find whether the number typed is a member of the array
if (i == m)
cout << find << " is not in the list" << endl;
else
cout << find << " is the " << i + 1
<< "th element in the list" << endl;
return 0;
}
Multidimensional array
 An array of arrays is called a multidimensional array.
 int anArray[3][5]; // a 3-element array of 5-element arrays
 [0][0] [0][1] [0][2] [0][3] [0][4]
[1][0] [1][1] [1][2] [1][3] [1][4]
[2][0] [2][1] [2][2] [2][3] [2][4]
Initialize
 int anArray[3][2] =
{
{ 1, 2 }, // row 0
{ 6, 7 }, // row 1
{ 11, 12 } // row 2
};
Excercise
 Write a program that calculates and prints a multiplication
table for all values between 1 and 9 using two dimensional
array.
 Write a program to find the smallest number of the elements
of an array
Solution 3
// Declare a 10x10 array
const int nNumRows = 10;
const int nNumCols = 10;
int nProduct[nNumRows ][nNumCols ] = { 0 };
// Calculate a multiplication table
for (int nRow = 0; nRow < nNumRows; nRow++)
for (int nCol = 0; nCol < nNumCols; nCol++)
nProduct[nRow][nCol] = nRow * nCol;
// Print the table
for (int nRow = 1; nRow < nNumRows; nRow++)
{
for (int nCol = 1; nCol < nNumCols; nCol++)
cout << nProduct[nRow][nCol] << "\t";
cout << endl;
}
Solution 4
#include <iostream>
using namespace std;
int main()
{
// The members of the array
int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89};
int minimum = numbers[0];
int a = 8;
// Compare the members
for (int i = 1; i < a; ++i) {
if (numbers[i] < minimum)
minimum = numbers[i];
}
// Announce the result
cout << "The lowest member value of the array is "
<< minimum << "." << endl;
return 0;
}
For more info
 http://www.learncpp.com/cpp-tutorial/65-multidimensionalarrays/
 http://www.functionx.com/cpp/Lesson12.htm

similar documents