0

The pattern to print if n=5 is

1 2 3 4 5

11 12 13 14 15

21 22 23 24 25

16 17 18 19 20

6 7 8 9 10

if anyone can tell me why and where is the overflow/segmentation fault.

#include <bits/stdc++.h> 
    
vector<string> printPattern(int n)
{
    int matrix[n][n];
    int i=0;
    int j=n-1;
    int k=1;
    while(i<=j)
    {
        int x=0;
        while (x <n&& i<=j) 
        {
            matrix[i][x]=k;
            k++;
            x++;
        }
        i++;
        int y=0;
        while(y<n && i<=j)
        {   
            matrix[j][y]=k;
            k++;
            y++;
        }
        j--;
    }
    for (int i = 0; i <= n-1; i++)
    {
        for (int j = 0; j <=n-1; j++)
        {
            cout<< matrix[i][j];
            if (j < n - 1)
            {
                cout << " ";
            }
        }
        cout<<endl;
    }
}

Its a 2D array approach to a pattern problem in codingninja's codestudio interview problems.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • 1
    This is not even standard C++: You should not use `#include ` and for `int matrix[n][n];` , `n` should be a compile time constant. This shows me that codingninja is not doing a good job of teaching you C++. Replace your matrix with `std::vector>` and then use `.at(index)` instead of `[index]` operator. My bet is that your code runs out of bounds somewhere. Another option is to use a debugger – Pepijn Kramer Jun 10 '23 at 10:12
  • Better sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Jun 10 '23 at 10:14
  • `for (int i = 0; i <= n-1; i++)` the idiomatic way of writing the condition would be `for (int i = 0; i < n; i++)` – fabian Jun 10 '23 at 10:40
  • This code appears to be GNU++, and not C++. – Eljay Jun 10 '23 at 14:43

1 Answers1

0

Basically your logic works.

The code is not C++ and the function does not return a value. This might cause the trouble.

The task is not described very well, so, it is unknown how the function is called by main() or some other function. If the function is called with negative values, it will also crash. It is not clear, why the function returns a vector of strings.

C++ does also not know VLAs (Variable Length Arrays). C++ compiler would not compile your int matrix[n][n]; statement. a 2d-vectorshould be used instead.

Applying these fixes will lead to

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

vector<string> printPattern(int n)
{
    if (n > 0) {
        vector<vector<int>> matrix(n, vector<int>(n));
        //int matrix[n][n];
        int i = 0;
        int j = n - 1;
        int k = 1;
        while (i <= j)
        {
            int x = 0;
            while (x < n && i <= j)
            {
                matrix[i][x] = k;
                k++;
                x++;
            }
            i++;
            int y = 0;
            while (y < n && i <= j)
            {
                matrix[j][y] = k;
                k++;
                y++;
            }
            j--;
        }
        for (int i = 0; i <= n - 1; i++)
        {
            for (int j = 0; j <= n - 1; j++)
            {
                cout << matrix[i][j];
                if (j < n - 1)
                {
                    cout << " ";
                }
            }
            cout << endl;
        }
    }
    return {};
}
int main() {
    for (int i =-2; i < 10; ++i) {
        std::cout << "\n--------------------------------------\ni = " << i << "\n";
        printPattern(i);
    }
}

This should work.

Gurnet
  • 118
  • 7