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.
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.
Where is the code?
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;
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
ASKER
Thank you very much.
rajpol
rajpol