Printing an Array after passing it to a function
Aug 12, 2012 at 9:42pm UTC
I'm trying to pass an array from a function to another to print it, but the numbers are not displaying properly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <iostream>
using namespace std;
int Input(int One[2][3])
{
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 4; column++)
{
cout << "Input element [" << row << "][" << column << "]: " ;
cin >> One[row][column];
}
}
}
int Print(int One[2][3])
{
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 4; column++)
{
cout << One[row][column] << " " ;
}
cout << endl;
}
}
int main()
{
int One[2][3];
Input(One);
Print(One);
cin.get();
cin.ignore();
return 0;
}
However when entering 1, 2, 3, ..., I get this as output:
1 2 3 5
5 6 7 9
9 10 11 12
The number 5 is repeated and so is 9.
Aug 12, 2012 at 9:49pm UTC
The loops shall be set the following way
1 2 3 4 5 6 7
for (int row = 0; row < 2 ; row++)
{
for (int column = 0; column < 3 ; column++)
{
// some code
}
}
Also the both function shall have return type
void because they return nothing.
Last edited on Aug 12, 2012 at 9:52pm UTC
Aug 12, 2012 at 11:36pm UTC
Changing the code works, but it only produces 2 rows and 3 columns. I'm trying to have 3 rows and 4 columns.
What I get:
What I want:
1 2 3 4
5 6 7 8
9 10 11 12
Aug 12, 2012 at 11:42pm UTC
That's because your int array One is initialized with [2][3]. So you have two rows and two columns. That means you have to change it to [3][4] to get what you want.
Aug 13, 2012 at 3:16am UTC
Oh that works! Thanks for all the help!
Topic archived. No new replies allowed.