I have come across the solution to print the 2 D array using C++.
The ans was to use auto
keyword:
for eg:
double nut_torques[4][5]; // this is my array with type double
for (auto& rows : nut_torques)
{
for (auto cols : rows)
{
std::cout << cols << " ";
}
std::cout << std::endl;
}
But can anyone tell me what if I don't want to use aut
?
For example, if there is 1 D array then using double
(since my type is double
) works:
for (double rows : array) {
}
I tried following for 2D array, but this time double worked only for inner foreach:
for (auto& rows : nut_torques)
{
for (**double** cols : rows)
{
std::cout << cols << " ";
}
std::cout << std::endl;
}
But the following did not work for me, when I tried adding double
for both inner and outer foreach:
for (**double** rows : nut_torques)
{
std::cout << rows<<std::endl;
for (**double** cols : rows)
{
std::cout << cols << " ";
}
std::cout << std::endl;
}
May I know the reason why we can't use double in outer foreach and what if I don't want to use auto then what shall I use there.
Also, if we std::cout
array variable, then we get address to the first index. Then how come for single foreach this line works:
for(double val: arrVariable)
Does foreach automatically dereferences the arrayVariable
and does it automatically increments it?