I have had some difficulty figuring out this one a bit. I think I have the last bit figured out (printing out to the screen). However, everything else has been causing problems for me. I am not sure, so any input from anyone would be greatly appreciated! This is what the program must do:

Create a flowchart and write a complete C++ program that declares a 2 dimensional integer array of size 10 rows and 10 columns. The program should use two nested for loops to fill the array as follows. If the row number is divisible by the column number then put in the put the row number in the cell, otherwise put the column number in the cell. The program should then use nested for loops to print the array in table form to the screen.

This is what I have so far:

#include <iostream>
using namespace std;
int main()
{
    int A[10][10], row, column;

        for (row=0; row<10 && row%column == 0; ++row)
        {

            A[row][column]=row;

            for (column=0; column<10 && row%column != 0; ++column)
            {

                A[row][column]=column;

            }

        }

        for (row=0; row<10; ++row)
        {

            for (column=0; column<10; ++column)
            {
                cout<<A[row][column]<<" ";
            }

        }

        return 0;

}

Thanks again and any input would be great!

Recommended Answers

All 3 Replies

I forgot to add the initialization of column to 0 at the top, but even after doing that the program crashes

I'm not 100% sure I understand the question, but this seems OK and avoids any crashes.

#include <iostream>
using namespace std;

int main()
{
    int A[10][10], row, column;

    for (row = 0; row < 10; ++row)
    {
        for (column = 0; column < 10; ++column)
        {
            // avoid divide by zero
            if (column != 0 && row % column == 0) 
            {
                A[row][column] = row;
            }
            else
            {
                A[row][column] = column;
            }
        }
    }

    for (row = 0; row < 10; ++row)
    {    
        for (column = 0; column < 10; ++column)
        {
            cout << A[row][column] << " ";
        }

        cout << endl;
    }

    cout << endl << endl << "press Enter to exit...";
    cin.get();

    return 0;    
}

I think the divide by zero thing was my problem, I think this will work thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.