help with arrays

Joined
Mar 31, 2011
Messages
1
Reaction score
0
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;
} 
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top