arrays - Adding Values of Field in C? -
i want ask how can add values of array in c. example:
array[5] -------- array[1]=0.2 array[2]=0.2 ----> want add first 0.2 second 0.2 (=0.4) array[3]=0.3 array[4]=0.15
i want following output:
0.2 0.4 0.7 0.85
how can that? special operators? ps: want in simple loop. not libaries etc.
much fail. many error.
tag question [c]
or [c++]
, not both.
not same thing.
if declare array[5]
, valid indicies 0
through 4
.
array[5]
out of bounds.
you describe wanting output, program has no output!
show printf
, or cout
, or similar output statement.
if want add value array member, simple:
array[2] = array[2] + 0.2;
if want add 2 array members together:
array[2] = array[1] + array[2];
although did not specify clearly, looks want make each member of array sum of earlier members (including itself).
here's code:
int main(void) { double array[] = { 0.2, 0.2, 0.3, 0.15 }; for(int i=1; i<4; ++i) { array[i] += array[i-1]; } for(int i=0; i<4; ++i) { printf("[%d] : %lf\n", i, array[i]); } return 0; }
Comments
Post a Comment