0

I am learning C++ and this is my first question for martix array in c++. I am getting Process returned -1073741571 (0xC00000FD) in terminal window. When Tried running it after removing the #define N 1000 then my code gave me the correct result. I want to know why it is not executing the code for #define constant. This is the code.

#include <iostream>
#include <cmath>
#define N 1000
using namespace std;

#function to give components of Vector A in different dimensions.
double vectorA (int n)
{
    double ai = pow(-1, n)*2*n;
    return ai;
}

#function to give components of Vector B in different dimensions.
double vectorB (int n)
{
    double bi = pow(-1, n)*n/2;
    return bi;
}

int main()
{

    int n;
    cin >> n;
#arr is the cross product vector.
    double arr [N][N];
#vector 1 and vector 2 using components defined by function A and B.
    double arr1 [N];
    double arr2 [N];

#initialising the vector 1 (arr1)
    for (int i = 0; i < n; i++)
    {
        arr1 [i] = vectorA (i);
    }
#initialising the vector 2 (arr2)
    for (int i = 0; i < n; i++)
    {
        arr2 [i] = vectorB (i);
    }
#initialising the cross product vector (arr).
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            arr [i][j] = arr1 [i] * arr2 [j];
        }
    }

    int dot = 0;
#dot product of vector 1 and vector 2.
    for (int i = 0; i < n; i++)
    {
        dot += arr1 [i] * arr2 [i];
    }

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

    cout << dot << endl;
    return 0;
}
  • 4
    You have a stack overflow because you are allocating too much data on the stack. You can make your variables global to bypass the issue or use `std::vector` instead. – Retired Ninja Dec 29 '20 at 15:03
  • Not sure what you're saying. Your code has errors using `#` as a comment delimiter. Also (as in the previous comment), allocating a 1 million element array on the stack is going to cause problems (your error is "stack overflow"). But, without that `#define N...` your code won't compile. Can you add some clarity/details? – Adrian Mole Dec 29 '20 at 15:04
  • I mean that if I don't use #define N and use int n (The integer that the user will input) to define the size of arr and arr1 and arr2; – Siddharth Singh Dec 30 '20 at 07:56

0 Answers0