M
M
MrFlatman2016-10-15 22:26:08
C++ / C#
MrFlatman, 2016-10-15 22:26:08

How to properly declare variables and set an array?

Hello everyone, please help, how in this code, code snippet, to correctly declare variables and set an array?
The program is designed to solve such an equation:
dc29868966b04230907c00a64e95bf12.png
I don’t understand how to correctly declare variables in this code, I know that int cannot be divided by int, and there are problems with creating an array VS constantly swears that "N is an undeclared identifier
" knowledge is still weak.
Thanks in advance for any help in this matter)
Here is the code itself

double f[N];
double h;
double res = 0;
h = (b-a)/n;
for(int i = 1; i<N + 1; i++)
{
f[i-1] = (a + (i - (1/2)*h)) / (1 + (a+(i - 1/2 ) * h));
}
 
for(int i = 0; i<N; i++)
res+=f[i];
 
res*=h;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2016-10-15
@MrFlatman

First, the variable N is not defined anywhere, just like a, b and n.
Secondly, you don't need an array, just add the resulting f i at once .
Thirdly, you can divide int by int, but the result will also be of type int.
Fourth, since you are working in real numbers, then write not 1/2, but 0.5, 1/2. or 1./2 to make the compiler understand that these subexpressions should be treated as real.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question