Hi everybody!
I'm a real beginner in programming, because I started classes one month ago at the university; I've got a problem with a really simple exercise with an array, i wrote this program on emacs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main() {
int dim;
double x[i];
cout<<"Give me the number of elements of the array" <<endl;
cin>>dim;
<<"Give me the " <<dim <<"elements of the array" <<endl;
for(int i=0;i<dim;i++) {
cin>>x[i];
}
cout <<"The elements of the array are" <<endl;
for (int i=0; i<dim; i++) {
cout <<x[i] <<", ";
}
cout <<endl;
}
till 4 elements the programm works, but when I put dim=5 it gives me "segmentation fault"
That code shouldn't even compile. In line 6, i is undeclared and on line 10, you're missing a cout.
You need to use dynamic allocation if you want the size of your array to be set at runtime (or preferably use vectors, but I don't know if your class is covering them). http://www.cplusplus.com/doc/tutorial/dynamic/