lundi 3 juin 2019

Segmentation Fault in Function to print concentric rectangular pattern

I am trying to solve a problem on Interviewbit: Print concentric rectangular pattern in a 2d matrix. Let us show you some examples to clarify what we mean.

Example 1:

Input: A = 4. Output:

4 4 4 4 4 4 4 4 3 3 3 3 3 4 4 3 2 2 2 3 4 4 3 2 1 2 3 4 4 3 2 2 2 3 4 4 3 3 3 3 3 4 4 4 4 4 4 4 4

I only have to complete the function and print required matrix . Function given in question :

vector<vector<int> > Solution::prettyPrint(int A) {
}

My Solution :

vector<vector<int> > Solution::prettyPrint(int A) {
    int arraySize = A * 2 - 1; 
    int result[arraySize][arraySize]; 


    for(int i = 0; i < arraySize; i++) 
    { 
        for(int j = 0; j < arraySize; j++) 
        { 
            if(abs(i - arraySize / 2) > abs(j - arraySize / 2)) 
                result[i][j] = abs(i - arraySize / 2) + 1; 
            else
                result[i][j] = (abs(j-arraySize / 2) + 1); 

        } 
    } 


    for(int i = 0; i < arraySize; i++) 
    { 
        for(int j = 0; j < arraySize; j++) 
        { 
            cout << result[i][j] << " "; 
        } 
        cout << endl; 
    } 

}

I am getting Segmentation Fault error

Aucun commentaire:

Enregistrer un commentaire