02 Arrays

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

Object Oriented

Programming
Lec # 2
By
Engr. Sajid Saleem
Arrays & Vectors
Arrays
Declaring / Accessing Arrays
Passing Arrays to functions
Multi-dimentional arrays
Vector
Arrays
Collection of similar data types in consecutive
memory locations.
Declaring / Accessing Arrays
// Calculating gas mileage
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
int main()
{
const int MAX = 20; // Maximum number of values
double gas[ MAX ]; // Gas quantity in gallons
long miles[ MAX ]; // Odometer readings
int count = 0; // Loop counter
char indicator = y; // Input indicator
while( (indicator == y || indicator == Y) && count < MAX )
{
cout << endl
<< Enter gas quantity: ;
cin >> gas[count]; // Read gas quantity
cout << Enter odometer reading: ;
cin >> miles[count]; // Read odometer value
++count;
cout << Do you want to enter another(y or n)? ;
cin >> indicator;
}
Declaring / Accessing Arrays
if(count <= 1) // count = 1 after 1 entry completed
{ // ... we need at least 2
cout << endl
<< Sorry - at least two readings are necessary.;
return 0;
}
// Output results from 2nd entry to last entry
for(int i = 1; i < count; i++)
cout << endl
<< setw(2) << i << . // Output sequence number
<< Gas purchased = << gas[i] << gallons // Output gas
<< resulted in // Output miles per gallon
<< (miles[i] - miles[i - 1])/gas[i] << miles per gallon.;
cout << endl;
return 0;
}
Passing Arrays to functions
// Passing arrays and individual array elements to functions.
#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

void modifyArray( int [], int ); // appears strange


void modifyElement( int );
int main()
{
const int arraySize = 5; // size of array a
int a[ arraySize ] = { 0, 1, 2, 3, 4 }; // initialize array a
cout << "Effects of passing entire array by reference:
<< "\n\nThe values of the original array are:\n";

// output original array elements


for ( int i = 0; i < arraySize; i++ )
cout << setw( 3 ) << a[ i ];
cout << endl;
// pass array a to modifyArray by reference
modifyArray( a, arraySize );

cout << "The values of the modified array are:\n";


Passing Arrays to functions
// output modified array elements
for ( int j = 0; j < arraySize; j++ )
cout << setw( 3 ) << a[ j ];
cout << "\n\n\nEffects of passing array element by value:"
<< "\n\na[3] before modifyElement: " << a[ 3 ] << endl;
modifyElement( a[ 3 ] ); // pass array element a[ 3 ] by value
cout << "a[3] after modifyElement: " << a[ 3 ] << endl;
return 0; // indicates successful termination
} // end main

// in function modifyArray, "b" points to the original array "a" in memory


void modifyArray( int b[], int sizeOfArray )
{
// multiply each array element by 2
for ( int k = 0; k < sizeOfArray; k++ )
b[ k ] *= 2;
} // end function modifyArray

// in function modifyElement, "e" is a local copy of


// array element a[ 3 ] passed from main
void modifyElement( int e )
{ // multiply parameter by 2
cout << "Value of element in modifyElement: " << ( e *= 2 ) << endl;
} // end function modifyElement
Multi-dimensional Arrays
Arrays that require two subscripts to identify a particular element are
called two-dimensional arrays or 2-D arrays.
Multi-dimensional Arrays
Multi-dimensional Arrays
// Initializing multidimensional arrays.
#include <iostream>
using std::cout;
using std::endl;

void printArray( const int [][ 3 ] ); // prototype

int main()
{
int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };

cout << "Values in array1 by row are:" << endl;


printArray( array1 );

cout << "\nValues in array2 by row are:" << endl;


printArray( array2 );

cout << "\nValues in array3 by row are:" << endl;


printArray( array3 );
return 0; // indicates successful termination
} // end main
Multi-dimensional Arrays
// output array with two rows and three columns
void printArray( const int a[][ 3 ] )
{
// loop through array's rows
for ( int i = 0; i < 2; i++ )
{ // loop through columns of current row
for ( int j = 0; j < 3; j++ )
cout << a[ i ][ j ] << ' ';

cout << endl; // start new line of output


} // end outer for
} // end function printArray
Vectors
C++ Standard Library class template vector, represents a more robust type
of array featuring many additional capabilities.

Library provides class template vector to allow programmers to create a


more powerful and less error-prone alternative to arrays.

Standard class template vector is defined in header <vector> and belongs


to namespace std
Vectors
#include <iostream>
#include <vector>

using namespace std; // same as std :: vector


/* Namespaces allow to group entities like classes, objects and functions under a name. This way the
global scope can be divided in "sub-scopes", each one with its own name.The format of namespaces is:

namespace identifier
{
entities
} */

int main()
{
vector<double> student_marks(20);

for (vector<double>::size_type i = 0; i < 20; i++)


{
cout << "Enter marks for student #" << i+1
<< ": " << flush;
cin >> student_marks[i];
}
// ... Do some stuff with the values

return 0;
}
Topics Covered
C++ How to Program, Fifth Edition By
H.M.Deitel (Ebook references)
7.1 to 7.5 , 7.8 to 7.11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy