derivative/ slope issue

Hey folks,

first of all I want to say thats a great forum, I am a complete beginner in programming and I already found some useful information.

Now to my issue, I want to write a simple program which is able to differentiate numerically with a well defined stepsize that gives you the values of every point in an array, which can be plotted in gnuplot or sth.

Here what I have till now, first I want it to show me the data of the array, but since I included that last part I just get a segmentation error.

The longer I try to work on that programm the more unsure I get about it, for example I didnt have yet defined a function. Right now I am absolutely confused and don't know what to do next.
Do you guys have some hints for me?

Cheers,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

int main(){

int i, nx,n, dx=0.1;
int  x[n], y[nx];

for (i=0; i<nx; i++){
y[i+1] = y[i] - y[i]*dx;

reverse_iterator<int *> begin(y + sizeof(y) / sizeof(y[0]));
  reverse_iterator<int *> end(y);

  copy(begin,
       end,
       ostream_iterator<short>(cout, "\n"));
return 0;

}
}
Hi sphstctd,


Ok, a bunch of problems here.

So your program contains very basic mistakes, yet you have various types of iterators and pointers and algorithms. Are you trolling?

If you genuinely have a problem, post some code that is more normal, not nonsense like this.
Hi IdeasMan,

Im sorry for my "nonsense" I have to admit I copied the last part with all these iterators and pointers somewhere in the web in order to see whats in the array. But I tried it with different things also with the normal cout << y[i] << " "; and I always get a segmentation error.

If you neglect the last part, just
1
2
3
4
5
6
7
8
9

int main(){

int i, nx,n, dx=0.1;
int  x[n], y[nx];

for (i=0; i<nx; i++){
y[i+1] = y[i] - y[i]*dx;
}}


how can I see whats in the array without the segmentation error, better said what causes the segmentation error in this case?
Last edited on
You have uninitialized values for the array sizes n and nx.

If dx is going to be a floating point value, why have you defined it as an int?
Hi sphstctd,

Sorry if I sounded grumpy earlier, but occasionally we get some time wasters here.

So, some advice:

Have a look at the tutorial at the top left of this page, there is also lots of reference material & articles.

With your program, if you make dx a double there will still be problems, because types are implicitly cast (promoted) according to a set of rules. For example y[i]*dx, would be int multiplied by double which is double, so y[i] - y[i]*dx is also double, but y[i+1] is expecting an int.

To solve this, make the arrays double, but their sizes as int, and leave the for loop counter as int because each of the variables on line 7 have to be of an integral type.

One huge lesson is to ALWAYS Initialise your variables with something. This can be one of the biggest source of errors - especially for beginners.

Style wise it is a really good idea to always declare & initialise and comment one variable per line. Give variables meaningful names, provide comments about expected range of values - value must be positive as an example. The main thing is to be as clear as possible with your code.

For beginners, I would recommend that you learn things in this order:

> Learn to write pseudo-code - a recipe or methodology as comments to guide you when writing code.

> Understand all the different basic types (int, short, long, char, double, float ) and their qualifiers (unsigned, const), what their ranges are (max, min values), what their precision is, how to declare them, how to assign values to them, how to declare & assign in one statement, how to print them with std::cout, how to read them in with std::cin, and how they are stored, and what one can and can't do with them, how they are promoted, and how one can change the type of the copy of their value by casting.

> Understand the different operators, what types they can be used with, and why some are better than others. For example a++ is better than a = a + 1; , and a+=10; is better than a = a + 10; for integral types. Also understand the different groups of operators: assignment, increment / decrement, comparison, mathematical, binary

> Understand what the different types of expressions are. As an example a for loop is this:
1
2
3
for (assignment ; conditional ; increment) {
    statements
}


> Understand compound statements.

> Understand the control-flow features like if (conditional) {} else if (conditional){} else {}, switch statements, loops.

> Understand arrays, how to make them with different types, make with 2 dimensions, initialise them, retrieve values from, assign values to individual elements, copy them.

> Understand functions, what are parameters & arguments, how & where to declare & define them, return types, pass references to .

All this might look a bit technical to you right now, but it really only scratches the surface.

Hope all goes well, and look forward to seeing your code + answering any questions you might have.
Topic archived. No new replies allowed.