I'm having trouble implementing this function definition, any help will be appreciated.

Write only the “snippet” of C++ code for a function definition, printColumn, which prints to the console the values in a column specified by the user in the function call. printColumn has two arguments, the 2D array and a integer column number, listed in this order. ‘Protect’ arguments from being changed by the function when necessary. So far I have this.

void printColumn (int int_array[][], int number)
{
    for (int i=0; i<number; i++)
        {
            int_array[i].printColumn();
        }
}

Write only the "snippet" of C++ code for a function definition, printColumn, which prints to the console the values in a column specified by the user in the function call.

void printColumn (int int_array[][], int number);

"number" is not a very descriptive term. I assume "number" is this: column specified by the user

So why not call it "column" instead of "number"? It makes it more obvious what the integer is.

void printColumn (int int_array[][], int column);

int int_array[][]

You cannot pass a 2-dimensional array that way. You can either change it int** int_array or you can change it to int int_array[][NUM_COLUMNS] where NUM_COLUMNS is either a constant or you can stick an actual number in there like 5 or however many columns the array has.

'Protect' arguments from being changed by the function when necessary.

What does this function do? It displays. Does it need to change any of the array's values to do that? If not, it should "protect" that array, as required. How do you do that? You'll probably need the keyword "const" in that function definition.

void printColumn (int int_array[][NUM_COLUMNS], int column) // stick a "const" somewhere in this line
{
    for (int i=0; i<column; i++)
        {
            int_array[i].printColumn();
        }
}

I changed the function declaration slightly (i.e. changed "number" to "column", plus stuck NUM_COLUMNS in there).

I don't see the word "cout" anywhere in this function. How do you plan to display anything without a "cout" in there?

Think about what goes in the loop. Right now you are calling a function from within the function. Is that what you intend?

Read the specification very carefully. Several times. What are you printing? How many elements need to be printed? Which ones?

Edited 7 Months Ago by AssertNull

Here is a little more general way one might handle a problem like this ...
(the code also demo's using templates in C++.)

// print_a_col.cpp //


#include <iostream>

using std::ostream;


// set here what you need ...

const size_t NUM_COLS = 4;


// NOTE: below we pass in ALL parameters as const ...
// EXCEPT ostream ... which is modified here. //

template < typename T >
void output( const T matrix[][NUM_COLS],
             const size_t num_rows,
             const size_t col_to_print,
             std::ostream& os = std::cout,
             const char end = '\n' )
{
    if( col_to_print < NUM_COLS )
        for( size_t r = 0; r < num_rows; ++ r )
        {
            os << matrix[r][col_to_print] << end;
        }
}


int main()
{
    using std::cout;

    const int mat[][NUM_COLS] =
    {

        { 1, 3, 3, 4 },
        { 5, 6, 7, 8 }
    } ;
    const int num_rows = sizeof mat / sizeof(int) / NUM_COLS;

    cout << "printing col with index 0: \n";
    output( mat, num_rows, 0 );

    cout << "printing col with index 1: \n";
    output( mat, num_rows, 1, std::cout, ' ' );

    cout << "\nAnd num_rows is " << num_rows << '\n';
}

Edited 7 Months Ago by David W

This article has been dead for over six months. Start a new discussion instead.