Monday, May 24, 2010

About C programming. Please Help!?

OK, I want to write a programme which will ask the user to define the dimension of a 2D-matrix and then to input the matrix. How can I do that? And I think one can not define a 2D-array of undifine size. Is it correct or am I wrong?

About C programming. Please Help!?
Declare a two dimensional pointer for example :





int **x;





x = static_cast%26lt;int **%26gt;(malloc(sizeof(void *) * dimension x));


for ( i = 0; i%26lt;dimension x; i++)


{


x[i] = static_cast%26lt;int**%26gt;(malloc(sizeof(void) * dimension y));


}





You are ready to use x[i][j]
Reply:in C-99 the latest version - you can!!!!





char x[a];





is allowed in C-99.


But before that - like Ansi C, no, it is not allowed. But you can realloc. For example you malloc an array. if the value is not large enough then realloc!!!





Good luck
Reply:Ask for the dimensions of the array, then pass the dimensions to a function. Declare the array variable in this function, using the passed parameters to determine the size. Pseudocode:





main {


get dimX, dimY


inputArray(dimX, dimY)


}





inputArray(sizeX, sizeY) {


declare anArray[sizeX, sizeY]


}





Hope this makes sense.





Rawlyn.


No comments:

Post a Comment