0
#include <bits/stdc++.h>
using namespace std;
#define R 3
#define C 6

int PrintSpiral(int arr[][C], int m, int n){
    int t= 0, b = m-1, l = 0, r = n-1;
    int dir = 0;`
    while(l <= r && t <= b){
        if(dir == 0){
            for(int k = l; k <= r; k++)
                cout << arr[t][k];
            t++;
        }
        else if(dir == 1){
            for(int k = t; k <= b; k++)
                cout << arr[k][r];
            r--;
        }
        else if(dir == 2){
            for(int k = r; k >= l; k--)
                cout << arr[b][k];
            b--;
        }
        else if(dir == 3){
            for(int k = b; k >= t; k--)
                cout << arr[k][l];
            l++;
        }
        dir = (dir+1)%4;
    }
}

int main() {
    //code
    int t;
    cin>>t;
    while(t--){
        int na,ma;
        cin>>na>>ma;
        int ar[na][ma] = {{0}};
        for(int i = 0; i < na; i++){
            for(int j = 0; j < ma; j++){
                cin>>ar[i][j];
            }
        }
        PrintSpiral(ar,na,ma);
    }
    return 0;
}

I am getting this error:

Spiral_matrix.cpp: In function 'int main()':<br/>
Spiral_matrix.cpp:47:18: error: cannot convert 'int (*)[ma]' to 'int (*)[6]'
      PrintSpiral(ar,na,ma);
                  ^~
Spiral_matrix.cpp:6:21: note:   initializing argument 1 of 'int PrintSpiral(int (*)[6], int, int)'
 int PrintSpiral(int arr[][C], int m, int n){

enter image description here

Error Resolving the issue. Kindly look into it The above code prints the given 2-D matrix into Spiral form I applied arr[][6] with random value and also i predefined column as C and updated it again with arr[][C] still it shows an error Apart from this i even used the value of R and C in my main function Still it shows an error I want a way in which i could pass my function successfully in the main function.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • For your future questions, the correct way to format code is to put the whole block between ``` ``` (triple backticks) or just select the whole code and press `{}` button on the bar over editor field. – Yksisarvinen Jun 03 '20 at 09:00
  • `int ar[na][ma] = {{0}};` This is not legal C++ because `na` and `ma` are variables not constants. That's the root of your troubles. The sensible way to solve this problem is to use `std::vector` instead of non-standard features. – john Jun 03 '20 at 09:01
  • 1
    Your code is invalid C++ as it uses [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). Please don't use "competition" sites to learn how to program or the C++ language, they are not learning/teaching resources. Get [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), take classes, learn about common data-structures and algorithms. Then when you're comfortable with all the basics maybe use such sites as training resources, to keep your knowledge fresh. – Some programmer dude Jun 03 '20 at 09:01

1 Answers1

0

Here's your code rewritten to use vectors instead of variable length arrays. I've omitted that parts that don't need changing (which is most of it)

#include <iostream>
#include <vector>
using namespace std;

void PrintSpiral(const vector<vector<int>>& arr){
    if (arr.size() == 0) return;
    int t= 0, b = arr.size()-1, l = 0, r = arr[0].size()-1;
    ...
}

int main() {
    ...
        int na,ma;
        cin>>na>>ma;
        vector<vector<int>> arr(na, vector<int>(ma));
        ...
        PrintSpiral(arr);
    ...
    return 0;
}

The sooner you learn about modern C++ (std::vector is over 20 years old), the faster your progress will be. Don't program C++ as if you're still in the last millennium.

john
  • 85,011
  • 4
  • 57
  • 81