help with arrays

Discussion in 'C Programming' started by drdaves, Mar 31, 2011.

  1. drdaves

    drdaves

    Joined:
    Mar 31, 2011
    Messages:
    1
    I started C++ programming and would like some help. I wrote this code and finally got it to work with a workaround. Here's the problem: 2 arrays of different sizes need to be sent to a function to print the arrays properly (all rows and all columns) in a grid (x,y).

    My program works but I have to make my small arrays larger to work with my function because you have to define the function array first.

    My question is: Is there a better way to handle this so I don't have waste (I heard about pointers and vectors, but I don't know anything about them)

    thanks

    here's the code:

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    const int col1 = 4;
    const int col2 = 5; 
    const int row1 = 3;
    const int row2 = 5;
    const int maxcol = 5;
    //I set maxcol=5 for function to work they all have to match.
    
    void showArray(int array[][maxcol], int, int);
    
    
    int main ()
    {                   
    //multi-variable arrays with data 3 rows X 4 cols
    int table1[row1][maxcol] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
    
    //multi-variable arrays with data 5 rows X 5 cols
    int table2[row2][maxcol] = {{10,20,30,40,41},{50,60,70,80,81},{90,100,110,120,121},{130,140,150,160,161},{170,180,190,200,201}};
    
    cout << "the contents of table 1 are: " << endl;
    showArray(table1,row1,col1);
    cout << "The contents of table 2 are: " << endl;
    showArray(table2,row2,col2);
    cout << endl;
    system("pause");
    return 0;
    }
    
    void showArray(int array[][maxcol],int rows, int cols)
    {
    for (int x = 0; x<rows; x++)
    {
        for (int y = 0;y<cols; y++)
        {
        cout << setw(4) << array[x][y] << " ";
        }
        cout << endl;
    } 
    }
    
     
    drdaves, Mar 31, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,653
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,260
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    569
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    387
  5. Philipp
    Replies:
    21
    Views:
    1,132
    Philipp
    Jan 20, 2009
Loading...

Share This Page