User Tools

Site Tools


cs509:cplusplus_array_example_2

Example: C++ array

// Histogram printing program using arrays
// Note: Think about how this would be accomplished w/o arrays.

#include<iostream>

using namespace std;

int main()
{
    const int arraySize = 10;

    int n[ arraySize ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };

    cout << "\n Element" << "\t" << "Value"
         << "\t\t" << "Histogram" << endl;

    for ( int i = 0; i < arraySize; i++ ) {
        cout << " " << i << "\t\t"
             << n[ i ] << "\t\t";

        for ( int j = 0; j < n[ i ]; j++ )  // print # stars equal to 
            cout << "*";                    // value of array element

        cout << endl;
    }

    return 0;
}
cs509/cplusplus_array_example_2.txt · Last modified: 2013/03/06 19:02 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki