+ 2
How to input a 2-D array by using functions in c++
How to use functions for taking input from user and a function to display the 2-D array entered by the user in c++ ? My code is below 👇but it's not working.. https://code.sololearn.com/ceuZaoiSBqZt/?ref=app
30 odpowiedzi
+ 2
Mohamed ELomari in the example I gave I advised him to return the pointer that points to the first element which is,
arr or which also is same thing with arr[0][0], this way since he already knows rows, cols and the address of the first element, he can iterate thru array using this pointer. I don't see any problem here?
+ 2
~ swim ~
why is int[row][col] illegal?
Aayushi Mittal
ever heard about vectors? using vectors, you don’t have to deal with all the array pointers crap.
+ 2
~ swim ~
I deleted my comment that you replied to because I though it wouldn't work too...but had a go and it did. work...now I understand why.
+ 2
~ swim ~
Actually I’ve only just read the guy’s code. Agree with you about that, wouldn’t be the case if row/col were declared as global const.
+ 2
Martin Taylor
you mentioned that vectors are “sometimes” a viable alternative to arrays in C++.
when exactly do you prefer arrays over vectors?
+ 2
~ swim ~
notice that the post I was referring to mentioned C++, not some low level embedded system that runs C.
also if memory is an issue, isn’t it even better to use a dynamic array?
+ 2
~ swim ~
I did some research before posting my questions. People said the same thing, but nobody mentioned about the numerical differences between these two.
Anyway, thank you for your answers.
+ 1
Instead of this,
return arr[rows][cols];
Change the return type of function from int to int* then,
return arr; // which is equal to &arr[0][0]
int* p = input(rows, cols);
Since you already know rows, cols and pointer to the first element, you can iterate thru array like this,
for (int i=0; i<rows; i++)
for (int j=0; j<cols; j++)
cout<< *p; // prints the element pointed by p
p++; // p now points to the next element
+ 1
@Mustafa K., a local array cannot be directly returned from a C/C++ function as it may not exist in memory after function call is over, in simple words functions can’t return arrays in C/C++.
+ 1
~ swim ~ oh well, I didn't know that. Does it not work even if array gets created with new operator?
+ 1
Thank you so much for the answers!... But actually I don't know about this concept can you please elaborate..and I do not know much about pointers. Is it possible to do them without pointers ?
+ 1
If you can insert the code or if you can give some reference regarding this problem then It'll be very helpful :-)
+ 1
Ok thank you 👍🏻
+ 1
~ swim ~
Well, I’ve only recently noticed that there are low embedded systems that run C++, so please excuse my ignorance.
However, since you insisted on bringing low level embedded systems into the discussion and since I have very little knowledge about it, I should ask some questions:
In what kind of systems/compilers, and shouldn’t today’s processor be so tight on memory management? Is the speed/memory difference *that* critical, that it is worth missing all the goodies stl has to offer, especially exception handling? I’ve been thinking that these differences are negligible.
+ 1
Martin Taylor
And the question is why would you? You can declare a vector with a fixed size as well. Vectors are basically arrays under the hood, their performance are the same.
If you scrolled above, swim has pretty much addressed my question in his answers.
0
What is concord
0
Ok no problem