The discussion revolves around how to pass a 2D array in C and C++ for numerical computing purposes. Participants explore various coding approaches, syntax differences, and the implications of using global variables versus passing arrays as parameters.
Discussion Character
Technical explanation
Debate/contested
Mathematical reasoning
Main Points Raised
One participant expresses confusion about how to pass and return values from a subroutine using a 2D array in C.
Another participant points out issues with the initial code and suggests that a 2D array is an array of pointers.
Some participants propose moving the 2D array outside of the main function to make it global or passing it as a parameter to the subroutine.
A later reply questions whether making the array global would allow the updated values to be used in the subroutine and if the return value would be correctly passed back to the main program.
There is a discussion about the differences between C and C++ solutions, with some participants arguing that they can differ significantly beyond just syntax.
One participant provides a C++ example using vectors and iterators, highlighting the richer data structures available in C++ compared to C.
Another participant emphasizes that C++ offers various container types and features that can enhance numerical computing, such as operator overloading and generic polymorphism.
Areas of Agreement / Disagreement
Participants express differing views on the necessity and implications of using global variables versus passing arrays as parameters. There is no consensus on whether the solutions in C and C++ are fundamentally different, with some asserting they are while others believe the differences are minimal.
Contextual Notes
Some participants mention specific coding practices and syntax that may not be universally applicable, depending on the context of the programming task. There are unresolved questions about the best practices for passing arrays and the implications of using global variables.
#1
eswarm21
8
0
Hi I have this small doubt. i am not good in C . i like to bring i,j,pi values to subroutine and bring back Asi to main program. how to write ! Plz help..
for (i=2;i<n;i++)
{
for (j=2;j<m;j++)
{
pi[j]=pow((i+j),2)
Ai=A_operate(i,j, pi[][]);
r[j]=Ai;
}
}
}// main ends
double A_operate(int i,int j, double pi [][])
{
double Asi;
Asi=20*pi[j];
return (Asi);
}
Is it ok ?... i think still some problem is there...
if i make global like this ... will it use updated pi in subroutine ?? will it bring Asi to main program.?...Can anyone write the same program as an expert would..?
Note, arrays are passed to functions by reference (by their address). You have to specify the size of all but the first dimension so the function knows how to access the array. Example for a 2d array. The hex printf prints out the address of the matrix so you can see that it is the same.
Code:
#include <stdio.h>
#define NCOL 8
#define NROW 4
int test(int [][NCOL]);
int main()
{
unsigned int i, j;
int a2d[NROW][NCOL];
for(j = 0; j < NROW; j ++){
for(i = 0; i < NCOL; i++){
a2d[j][i] = j*10 + i;
printf("%2d ", a2d[j][i]);
}
printf("\n");
}
printf("%08x\n", &a2d[0][0]);
test(a2d);
return(0);
}
int test(int x2d[][NCOL])
{
printf("%08x\n", &x2d[0][0]);
return(0);
}
Last edited:
#10
eswarm21
8
0
oohhh sorry about title .. actually i am compiling in C++ compiler. But I wrote my code in C .
I am using these codes for very simple purposes. OK ... i didnt understand "solution differs between C and C++"... it surprises me ...
This is neither intended to be "the" best matrix class (there is nothing like "the best" at all), nor sth. for production usage (let alone supposed to be complete in any means¹); it's just to meet the requirements of the OP in a flexible way by means of C++.
At its heart there's this one
Code:
std::valarray<T> _data;
hidden from the public; here just² to be understood as a plain chunk storage of numerical values along with some handy overloaded operators for bulk ops like this
is implemented C/C++-style, that is, row-major ordering.
For e.g. OpenGL or Fortran interop, column-major ordering would be better, so if that requirement would arise in the future, one could simply replace "i*N" with "i*M" and keep the rest intact.
For serious programming, one would not rely on "replacing" sth in much tested code, but would design the template accordingly from the beginning, but that's not our point here.
which offers two convenience STL-style iterators for usage with STL-algorithms and functors; eg the initial fill could be carried out by sth like
Code:
std::generate(P.begin(), P.end(), init);
where "init" is an instance of a custom "functor", that is a class with a custom "( )" operator.
---
That should be enough to demonstrate that there's more about C++ than just a fancier output syntax.
Regards, Solkar
¹To see a really sophisticated matrix implementation with C++ pls attend Boost uBLAS
²the concept behind std:valarray is in fact much more elaborate, but that's not our topic here
Last edited:
#16
rbj
2,222
11
Solkar said:
It's somewhat annoying if people needlessly include "C++" when raising questions about plain "C" coding.
i would agree that it's a C question. even with C++, when you drill down to the "leaf" methods (i don't know what you call them, but in C we might call functions that don't call other functions a "leaf", you cannot have indirect referrals forever if you want to actually get something done) the code is C.
A quality C++ solution often would look entirely different than any solution possible with C.
i would be careful with that remark. I've seen some really crappy C++ "solutions". C++ is an OOP, but it's an ugly and inconcise OOP. C++ is not Smalltalk.
#17
Solkar
107
3
rbj said:
I've seen some really crappy C++ "solutions". C++ is an OOP, but it's an ugly and inconcise OOP. C++ is not Smalltalk.
Well... "OOP" in a strict sense and HPC numerics don`t go well together, e.g. "late binding" and "vectorization" bite each other.
STL/Boost-like generic polymorphism is a different topic.
Personally speaking I do
- system programming with plain C99
- complicated maths with Fortran
- and all the rest needed to be compiled (esp application of graph theory) with C++,
and I tend to keep this apart module- or even process-wise.