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.
rajpolAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
'n' is unintialized, thus the error (it is just a random value at that point) - try

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

    for (n = 0; n < 25; n++)
    {
      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;
}

instead to fill in the values. You might want to define M_PI as well.
0
 
Anthony2000Commented:
Where is the code?
0
 
rajpolAuthor Commented:
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;
}
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

rajpol
0
 
jkrCommented:
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.
0
 
rajpolAuthor Commented:
Thank you very much.

rajpol
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.