Link to home
Start Free TrialLog in
Avatar of rajpol
rajpol

asked on

Error: 'Segmentation fault'

Hi experts,

I just beginner in computer programming- not very far from the 'Hello world'. OS is linux and I use it through 'putty' in a pc (Windows XP).  My problem is that the following little piece of code compiles(c++) but gives an error message "Segmentation Fault". Any help with explanation? Thanks.
Avatar of Anthony2000
Anthony2000
Flag of United States of America image

Where is the code?
Avatar of rajpol
rajpol

ASKER

Sorry
Its here:

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
    int n;
    double x[25];
    double y[25];

    x[n] = n*15/180;
    y[n] = sin(M_PI*x[n]);

    ofstream output("sine_data.txt");

    for(int i =0;i <25;i++)
    {
        output <<"\t" << x[i] <<"\t\t"<<y[i]<<'\n';

    }

    return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rajpol

ASKER

Thanks jkr. It solved the problem. Just one more dumb question. Do the arrays, as in this example,  initialize by default? Any precautions?

rajpol
Arrays initialize to whatever was in the memory location where they are created, so it is always a good idea to initialize them "manually". In the above, that is done by the calculation foop.
Avatar of rajpol

ASKER

Thank you very much.

rajpol