c++ 2 dimensional array

Hello, my simple code is not printing out the users correct values:
#include <iostream>
using namespace std;
void echo(int a[][5], int sizeOfarray);
int main(void)
{
const int col=4;
const int rows=5;

int a[col][rows];

for (int i =0; i<col; i++)
{
	for(int b=0; b<rows; b++)
	{
      cout <<"please enter in an number : ";
	  cin >> a[col][rows];
	  
	}

}
 
echo(a,5);

}
void echo(int a[][5], int sizeOfarray)
{
	for(int i =0; i < sizeOfarray; i++)
	{
		for(int index2=0; index2 <5; index2++)
		{
			cout <<a[i][index2] <<" ";
			cout <<endl;
		}

	}

}

Open in new window


businessesatozAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

farzanjCommented:
Here is revised code.  
#include <iostream>
using namespace std;

const int col=2;
const int rows=2;
void echo(int a[][rows], int sizeOfarray);

int main(void)
{
    int a[col][rows];

    for (int i =0; i<col; i++)
    {
        for(int b=0; b<rows; b++)
        {
          cout <<"Please enter in a number : ";
          cin >> a[i][b];
        }

    }
    echo(a,5);
}


void echo(int a[][rows], int sizeOfarray)
{
    for(int i =0; i < col; i++)
    {
        for(int index2=0; index2 <rows; index2++)
        {
            cout <<a[i][index2] <<" ";
            cout <<endl;
        }

    }

}

Open in new window


The problem was that you were using col and rows in the input which is a constant.  So you were changing the same row over and over
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
farzanjCommented:
Also constants are kept global and this is typically the way it is done and there is on harm in doing so.

Since the rows and col are both constants, you don't need to pass the size of array.
0
businessesatozAuthor Commented:
Thanks i found my error.. i was trying to get a[cols][rows] when i should have been using my for loop variable i believe..
0
businessesatozAuthor Commented:
thank you..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.