How to pass a 2 D array of unknown n elements into the a function in C. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to pass a 2 D array of unknown n elements into the a function in C.

I want to pass a 2D array of integers with unknown n that I'll input from the user at the run time. I am getting strange errors when attempting to pass the array to the function. Can someone explain with the help of a small code example?

31st Oct 2021, 10:04 AM
Ruchika Sehgal
Ruchika Sehgal - avatar
17 Answers
+ 1
Here is the question: In order to impress Chefina, Chef is trying to improve his chess calculation skills. Today, Chef took a big chessboard with N rows (numbered 1 through N) and N columns (numbered 1 through N). Let's denote the square in row r and column c of the chessboard by (r,c). Chef wants to place some queens on this chessboard in such a way that the following conditions are satisfied: Each square of the board contains at most one queen. There are exactly N−2 queens on the board. All squares are reachable by at least one queen. A queen can reach all cells along the row, column, and diagonals it is placed at. Formally, a queen placed at square (r,c) can reach square (x,y) if at least one of the following conditions holds: x=r y=c r+c=x+y r−c=x−y Help Chef find a possible distribution of queens. If there are multiple solutions, you may find any one. It is guaranteed that under the given constraints, a solution always exists. Input Format The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first and only line of each test case contains a single integer N. Output Format For each test case, print N lines. For each valid i, the i-th of these lines should contain a single string with length N describing row i of the chessboard; for each valid j, the j-th character of this string should be 'Q' if there is a queen in the square (i,j) or '.' if this square is empty. Please note you have to place exactly N−2 queens. Constraints 1≤T≤100 3≤N≤100 The sum of N2 over all test cases does not exceed 105 Sample Input 1 2 3 4 Sample Output 1 ... .Q. ... ...Q .... .Q.. .... Explanation Test Case 1: The Queen in the middle covers all squares on the board. Test Case 2: The Queen at square (1,4) covers all squares in the first row and the last column, while the other Queen covers all the other squares.
31st Oct 2021, 2:08 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
+ 2
Ruchika Sehgal can u tell what u expecting as i tried with inputs your code working fine their is not synthetic errors learn about STL you can reduce many lines
31st Oct 2021, 1:01 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
This problem will not visible to other becz you have participated in challenges i guess others cannot see your question without registration
31st Oct 2021, 1:25 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Ruchika Sehgal I changed something in your code based on the problem statement like taking number of test cases and removed some unwanted codes. I'm not sure it would work properly for big inputs, just check it out. https://code.sololearn.com/cCxIe0zA342F/?ref=app
31st Oct 2021, 4:23 PM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 1
Can you post the code you got?
31st Oct 2021, 10:13 AM
Slick
Slick - avatar
+ 1
The error?
31st Oct 2021, 11:43 AM
Ruchika Sehgal
Ruchika Sehgal - avatar
+ 1
the error, the code, everything! Ruchika Sehgal what does code chef have to do with anything? Please post the code
31st Oct 2021, 11:44 AM
Slick
Slick - avatar
+ 1
'n' undeclared here (not in a function)
31st Oct 2021, 11:44 AM
Ruchika Sehgal
Ruchika Sehgal - avatar
+ 1
Nah, post it in the playground, save it, and share it please. Plus its cut off
31st Oct 2021, 11:48 AM
Slick
Slick - avatar
+ 1
put it in the playground and share it bud
31st Oct 2021, 11:50 AM
Slick
Slick - avatar
+ 1
Okays
31st Oct 2021, 11:51 AM
Ruchika Sehgal
Ruchika Sehgal - avatar
31st Oct 2021, 12:02 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
+ 1
www.codechef.com/SNCK1B21/problems/QPLACE
31st Oct 2021, 1:07 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
+ 1
Am trying to solve this question
31st Oct 2021, 1:07 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
+ 1
Oh
31st Oct 2021, 1:29 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
+ 1
Martin Taylor Thanks for the suggestion.
5th Nov 2021, 10:08 AM
Ruchika Sehgal
Ruchika Sehgal - avatar
0
I am actually attempting a Codechef problem
31st Oct 2021, 11:45 AM
Ruchika Sehgal
Ruchika Sehgal - avatar