+ 2

Help me please, arrays in C

How do you assing value to an array in c with a function. I have an empty array and I have to pass it as parameter to a function an give values. I mean: where i wrote Ā°Ā°Ā°Ā°Ā° I don't know what to put, because that value can't be empty, that is the place for the colum, but I'm reading the value void values(arr[][Ā°Ā°Ā°Ā°Ā°Ā°Ā°],row,col){ for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ arr[i][j]= rand(); } } } void main(){ scanf("%d",&row); scanf("%d",&col); int myArray[row][col]; }

19th Oct 2017, 4:17 AM
Victor_*
Victor_* - avatar
4 Answers
+ 16
First of all, You are getting the col and row size from the input so you've got to dynamically allocate this particular array. [https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new] //================================================ // Array of pointers to arrays (Dynamic 2D array) // Done for : Victor_* // By : Babak Sheykhan //================================================ #include <iostream> #include <cstdlib> #include <ctime> void filler(int **arr, int r, int c){ std::cout << std::endl; for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { arr[i][j]= rand() % 21 + (-10); // [-10, 10] std::cout.width(5); std::cout << arr[i][j]; } std::cout << std::endl; } } int main() { srand(static_cast<size_t>(time(0))); int row = 0; int col = 0; std::cout << "Row is "; std::cin >> row; std::cout << "Col is "; std::cin >> col; // Declare an array of pointers to arrays (a dynamic 2D array) // which we dynamically allocate memory to the first dimension (row) int** myArray = new int *[row]; // In order to complete our allocation, we've got to use a for loop // to claim the required space out of heap for columns group for(int i = 0; i < row; ++i) myArray[i] = new int[col]; // Calling filler function filler(myArray, row, col); // Clean up the heap for(int i = 0; i < row; ++i) { delete [] myArray[i]; myArray[i] = 0; // reset columns to null } delete [] myArray; myArray = 0; // reset rows to null } [https://code.sololearn.com/cgf1339VwBFC] As you see, memory management is a bit pain. One of the best alternatives for dynamic arrays would be std::vector. There are three ways to pass a 2D array to a function. The following link is a related discussion about those 3 ways. [https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function]
19th Oct 2017, 8:06 AM
Babak
Babak - avatar
+ 4
There are some mistakes in your code : 1) main should return an int. Even if it is not mandatory, it is a good practice :) 2) A static array (declared with brackets) needs to have CONSTANT size. That means that the size of the array will NEVER change once your code is compiled. 3) If you want a function to take your static array as a parameter, as it is of constant size, you'll have to put its size instead of Ā°Ā°Ā°Ā°Ā°Ā° 4) If you want to do a have an array with undefined size (like it seems you wanted in your code), then use pointers instead, and use them also as parameter of your function Do you already know how to use pointers or do you want a little explanation ?
19th Oct 2017, 7:28 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
thank you guys,for taking the time ,reading and explaining. I'm supposed to be learning that at thr univerisity but my professor just got sit and stays there waiting if we have a question jajajj
19th Oct 2017, 12:23 PM
Victor_*
Victor_* - avatar
+ 1
but as I'm reading the value of the colum it's gonna be different, please I need your help, or tell me another way to fill an array with a function It just have to be 2x2 I didn't call the funtion values() in the main because of lack of space
19th Oct 2017, 4:19 AM
Victor_*
Victor_* - avatar