int linspace[4/dx-3/dx+1]; <-- An array size can only be an integer value and this array may not always be an integer. An array can store floating point values, but it's size must be an integer.
double current[i] <-- You cannot declare an array with a variable size. Use something like vectors for this. This array needs to read something like double current[10];
= -0.5+0.5*cos(linspace); <-- linspace is nothing. You must say which element of the array you want to use, linspace[0] or linspace[1], for example.
0;linspace<=2*PI;linspace+dx); <-- linspace is nothing. You must say which element of the array you want to use, linspace[0] or linspace[1], for example.
In fact, that problem occurs a lot.
int linspace <-- is declared again one line later.
current is also declared twice.
I think you should read up on arrays and then correct the above errors and any duplicates of those errors.
¿why are you doing that?
A couple of things:
_ Arrays index goes from 0 to n-1 (not 1 to n)
_ Look at std::valarray. There are function overloads that will apply to every element. Also it's got operators. (pretty much like matlab vectors)
_ Variables must be declared (just 1 time). Its type cannot change, so you can't "resize" an array.
___There is however std::valarray or std::vector, their size can change but they will not "expand" automagically.
Basically you should know that unlike Matlab you should always declare variables in C/C++ before use.
Also always initialize your variables before use.
You cannot change variable type whenever you like. Try to take this in mind when programming.
For arrays you should pay attention not to exceed boundary limits (no checking here just errors or crash) and to remember as @ne555 that arrays indeces start from 0 n ot 1!
Okay let me ask these questions to clean up my code a little.
How do I say:
set positions (1 through n) = 0 in the array 'current'
current (at position 1) = XXX
current (from X to Y) = XXX
for (int i = X; i < Y + 1; i++)
{
current[i] = XXX;
}
Explanation of for loop:
"int i = X": i is the loop counter, and we are starting it at X
"i < Y + 1": continue looping while i < Y +1, we have to say Y + 1 because we are being inclusive of element Y, this is equivalent to i <= Y (for integers at least)
"i++": increment i by one for each iteration of loop, equivalent to i = i +1 and i += 1. If you only wanted to process every other element you would say something like "i += 2"
"i++": increment i by one for each iteration of loop, equivalent to i = i +1 and i += 1. If you only wanted to process every other element you would say something like "i += 2"
can I do something like i+dx to process every .01 element?
can I do something like i+dx to process every .01 element?
Not in this context because there's no such thing as the .01th element of an array, only integers can be array indices (current[1], current[2], etc). But in general, yes you can use floating point numbers in for loop counters: