0
#include <bits/stdc++.h>
using namespace std;

int cs(int n, int m, int ar[][101]){
    int dp[n][m];
    dp[0][0] = ar[0][0];
    for(int i = 1; i<m; i++){
        dp[0][i] = ar[0][i]+dp[0][i-1];
    }
    for(int i = 1; i<n; i++){
        dp[i][0] = ar[i][0]+dp[i-1][0];
    }
    
    for(int i = 1; i<n; i++){
        for(int j = 1; j<m; j++){
            dp[i][j] = ar[i][j] + min(dp[i-1][j], dp[i][j-1]);
        }
    }
    return dp[n-1][m-1];
}

int main(){
    int n, m;
    cin>>n>>m;
    int arr[n][m];
    for(int i = 0 ; i < n ;i++){
        for(int j = 0; j<m; j++)
        cin>>arr[i][j];
    }
    cout<<cs(n,m,arr)<<endl;
}

I am getting this error can someone help

main.cpp:30:18: error: cannot convert int ()[m] to int ()[101]

I can't add know the exact no. of rows and columns so I can't specify it beforehand, is there a way to get rid of this problem without using vector, the question link is: https://www.pepcoding.com/resources/online-java-foundation/dynamic-programming-and-greedy/min-cost-maze-traversal-official/ojquestion

Evg
  • 25,259
  • 5
  • 41
  • 83
  • 2
    Maybe related: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Ted Lyngmo Jul 14 '21 at 10:22
  • Variable length arrays are not a part of C++ standard and can't be used this way. You cannot solve it without changing the type - `std::vector>` is the easiest to use, you could also also allocate memory manually, but that's far more difficult. – Yksisarvinen Jul 14 '21 at 10:23
  • 3
    why "without using vector" ? You could use a manually managed dynamic array, but why would you? – 463035818_is_not_an_ai Jul 14 '21 at 10:25
  • 1
    no offense, but it is really bad that beginners learn about exotic compiler extensions before learning about the non-exotic alternatives. Whoever told you to use `cin>>n>>m; int arr[n][m];` didn't do you a favour – 463035818_is_not_an_ai Jul 14 '21 at 10:27

1 Answers1

1

Each language has its rules. So, if you use C++, the you have to follow the rules of C++. The Syntax has to be correct.

If you use the wrong Syntax or invalid language constructs, the compiler will emit an error message.

You are not using C++. VLA (Variable Length Arrays) are not part of the C++ Language. Additionally, even if you would use C++ language, you can do things wrong.

For example, if a function, like in your case, expects a Type int ar[][101] (which will by the way result in a decayed pointer), then you must pass the exact expected type, when calling the function.

You are calling your function with arr[n][m]; which has not the same type as int ar[][101]. This will lead to the error message.

Even if I ignore for now that arr[n][m]; is invalid C++ code.

You cannot fix it in the way you expect. Using an int** in the function signature could help you.

But still the code is horrible. But this is not a problem for a "competitive coding"-guru. In competetive programming, you can do any dirt that you want. It has hardly anything to do with C++.

You do also dislike std::vector because the followers of the competitive-programming relgion treat it as evil. But real C++ programmers would always and all the time use it.

It is difficult to help you, because competetive programmers do often even not want to listen . . .

A M
  • 14,694
  • 5
  • 19
  • 44