Openmp reduction for loop errors? - c

This is testing part of the code:
float a = 0;
float b = 0;
int c = 0;
int d = 0;
#pragma omp parallel for schedule (dynamic, 1) reduction(+ : a, b, c, d)
for(i=0; i<100; i++) {
a +=1;
b +=1;
c +=1;
d +=1;
}
printf("a: %d, b: %d, c: %d, d: %d\n", a, b, c, d);
For some reasons my results are always:
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
a: 100, b: 100, c: 0, d: 202
Why aren't a, b, c, d all equal to 100?

You are using %d formats to print floating point numbers. That causes undefined behaviour. Use:
printf("a: %f, b: %f, c: %d, d: %d\n", a, b, c, d);
And you'll see you get the right answers.

Related

Incorrect result after adding days to date

Using below function, which simply adds days to date (yyyymmdd), works fine throughout years.
int dateplusdays(int datein, int days) {
int year, month, day;
int dateout;
struct tm date;
time_t secs;
year = (int)floor(datein / 10000.0);
month = (int)floor(datein / 100.0) - year * 100;
day = datein - month * 100 - year * 10000;
date.tm_sec = 0;
date.tm_min = 0;
date.tm_hour = 12;
date.tm_year = year - 1900;
date.tm_mon = month - 1;
date.tm_mday = day;
date.tm_isdst = -1;
secs = mktime(&date) + days * 86400;
date = *localtime(&secs);
dateout = (date.tm_year + 1900) * 10000 + (date.tm_mon + 1) * 100 + date.tm_mday;
return dateout;
}
I stress-tested from 1900 to 2100 using this test code. No errors!
for (i = 19000101; i < 21001231; i++) {
int a = dateplusdays(i, 0); // make date out of i
if (i == a) { // check for valid date
int b = dateplusdays(a, 1);
int c = dateplusdays(b, 1);
if (b == c)
printf("i:%d a:%d b:%d c:%d\n", i, a, b, c);
}
}
Now ... when changing date.tm_hour from 12 to 0, I get exactly 184 errors on very specific dates, spread completely irregularly throughout the range of years 1900-2100 (e.g. 30.10.2022 adding 1 day results in 30.10.2022).
i:19160930 a:19160930 b:19161001 c:19161001
i:19161001 a:19161001 b:19161001 c:19161001
...
i:20221029 a:20221029 b:20221030 c:20221030
i:20221030 a:20221030 b:20221030 c:20221030
...
i:20381030 a:20381030 b:20381031 c:20381031
i:20381031 a:20381031 b:20381031 c:20381031
On top, only months September-December are concerned.
geohei#vm92:~/Devel$ ./dateplusdays | cut -c7-8 | sort | uniq -c
47 09
131 10
6 11
What am I missing?
You are missing switches between winter and summer time — standard and daylight saving time, or whatever other jargon is used. The choice of 12:00:00 for the time of day was not accidental.
You should use date.tm_day = day + days; to get the right answer (instead of adding days * 86400). The mktime() function normalizes dates. Note that there are 82800 and 90000 seconds on some days because of the switch in time zone offset from UTC.
Side note: why on earth are you using floating point arithmetic instead of plain integer division?
Hmmm: I tried your code with the bare minimum extras (headers, an actual main() function) and only got output when I changed the 12 in the code to 0 — running on a MacBook Pro with macOS Big Sur 11.7.2.
The dates you quote are near the end of October, when the clocks "fall back". Daylight saving time was first introduced in 1918 in the USA, then stopped after 1921. It was reintroduced for one year in 1945, then 'permanently' in 1965. The rules changed in 1991 so the autumnal time switch changed from "last Sunday in October" to "first Sunday in November", so I see different values from you. (The rules for the springtime time switch also changed from "first Sunday in April" to "second Sunday in March" at the same time.) I'd guess you are living somewhere outside North America (or at least, outside the USA).
Here's some revised code, exploiting mktime()'s abilities, using midnight. It also formats the data using the ISO 8601 notation. However, it produces no output unless it is compiled with -DUSE_BROKEN_CODE as a compile-time option.
#include <stdio.h>
#include <time.h>
static int dateplusdays(int datein, int days)
{
struct tm date = { 0 };
int year = datein / 10000;
int month = (datein / 100) % 100;
int day = datein % 100;
date.tm_sec = 0;
date.tm_min = 0;
date.tm_hour = 0;
date.tm_year = year - 1900;
date.tm_mon = month - 1;
date.tm_isdst = -1;
#ifdef USE_BROKEN_CODE
date.tm_mday = day;
time_t secs = mktime(&date) + days * 86400;
#else
date.tm_mday = day + days;
time_t secs = mktime(&date);
#endif /* USE_BROKEN_CODE */
date = *localtime(&secs);
int dateout = (date.tm_year + 1900) * 10000 + (date.tm_mon + 1) * 100 + date.tm_mday;
return dateout;
}
static void print_date(int date)
{
printf("%.4d-%.2d-%.2d", date / 10000, (date / 100) % 100, date % 100);
}
int main(void)
{
for (int i = 19000101; i < 21001231; i++)
{
int a = dateplusdays(i, 0); // make date out of i
if (i == a) // check for valid date
{
int b = dateplusdays(a, 1);
int c = dateplusdays(b, 1);
if (b == c)
{
printf("i: %d a: ", i);
print_date(a);
printf(" b: ");
print_date(b);
printf(" c: ");
print_date(c);
putchar('\n');
}
}
}
return 0;
}
I live in Colorado, USA, so I get results like this from the program when compiled with the broken code active:
i: 19181026 a: 1918-10-26 b: 1918-10-27 c: 1918-10-27
i: 19181027 a: 1918-10-27 b: 1918-10-27 c: 1918-10-27
i: 19191025 a: 1919-10-25 b: 1919-10-26 c: 1919-10-26
i: 19201030 a: 1920-10-30 b: 1920-10-31 c: 1920-10-31
i: 19201031 a: 1920-10-31 b: 1920-10-31 c: 1920-10-31
i: 19210521 a: 1921-05-21 b: 1921-05-22 c: 1921-05-22
i: 19210522 a: 1921-05-22 b: 1921-05-22 c: 1921-05-22
i: 19450929 a: 1945-09-29 b: 1945-09-30 c: 1945-09-30
i: 19450930 a: 1945-09-30 b: 1945-09-30 c: 1945-09-30
i: 19651030 a: 1965-10-30 b: 1965-10-31 c: 1965-10-31
i: 19651031 a: 1965-10-31 b: 1965-10-31 c: 1965-10-31
i: 19661029 a: 1966-10-29 b: 1966-10-30 c: 1966-10-30
i: 19661030 a: 1966-10-30 b: 1966-10-30 c: 1966-10-30
…
i: 19881029 a: 1988-10-29 b: 1988-10-30 c: 1988-10-30
i: 19881030 a: 1988-10-30 b: 1988-10-30 c: 1988-10-30
i: 19891028 a: 1989-10-28 b: 1989-10-29 c: 1989-10-29
i: 19891029 a: 1989-10-29 b: 1989-10-29 c: 1989-10-29
i: 19901027 a: 1990-10-27 b: 1990-10-28 c: 1990-10-28
i: 19901028 a: 1990-10-28 b: 1990-10-28 c: 1990-10-28
i: 19911026 a: 1991-10-26 b: 1991-10-27 c: 1991-10-27
i: 19911027 a: 1991-10-27 b: 1991-10-27 c: 1991-10-27
i: 19921024 a: 1992-10-24 b: 1992-10-25 c: 1992-10-25
i: 19921025 a: 1992-10-25 b: 1992-10-25 c: 1992-10-25
…
i: 20201031 a: 2020-10-31 b: 2020-11-01 c: 2020-11-01
i: 20201101 a: 2020-11-01 b: 2020-11-01 c: 2020-11-01
i: 20211106 a: 2021-11-06 b: 2021-11-07 c: 2021-11-07
i: 20211107 a: 2021-11-07 b: 2021-11-07 c: 2021-11-07
i: 20221105 a: 2022-11-05 b: 2022-11-06 c: 2022-11-06
i: 20221106 a: 2022-11-06 b: 2022-11-06 c: 2022-11-06
…
i: 20371031 a: 2037-10-31 b: 2037-11-01 c: 2037-11-01
i: 20371101 a: 2037-11-01 b: 2037-11-01 c: 2037-11-01
i: 20381106 a: 2038-11-06 b: 2038-11-07 c: 2038-11-07
i: 20381107 a: 2038-11-07 b: 2038-11-07 c: 2038-11-07
i: 20391105 a: 2039-11-05 b: 2039-11-06 c: 2039-11-06
i: 20391106 a: 2039-11-06 b: 2039-11-06 c: 2039-11-06
…
i: 20981101 a: 2098-11-01 b: 2098-11-02 c: 2098-11-02
i: 20981102 a: 2098-11-02 b: 2098-11-02 c: 2098-11-02
i: 20991031 a: 2099-10-31 b: 2099-11-01 c: 2099-11-01
i: 20991101 a: 2099-11-01 b: 2099-11-01 c: 2099-11-01
i: 21001106 a: 2100-11-06 b: 2100-11-07 c: 2100-11-07
i: 21001107 a: 2100-11-07 b: 2100-11-07 c: 2100-11-07

Bisection method to find root of sin(x) in c recursively

I want to find the root of sin(x) function by approximating an interval [a,b] recursively, but my output is:
-0.350783
But I want to get
3.1415
This is my code so far
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double dicho(double a,double b)
{
double m=(a+b)/2;
double eps=0.000001;
if((b-a)>=eps)
{
if(abs(sin(0)-sin(m))<=eps){
return m;
}
if(sin(m)<0){
a=m;
}
if(sin(m)>0){
b=m;
}
dicho(a,b);
}
}
void main()
{
int a,b;
a=3;
b=4;
double res=sin(dicho(a,b));
printf("%lf\n",res);
}
Everything klutt said. But there's also a glitch in your algorithm. Namely in the [3, 4] interval the sine function is strictly decreasing and this breaks down your logic a little bit.
Reworking your dicho function something like this seems to work:
double dicho(double a,double b)
{
double m=(a+b)/2;
double eps=0.000001;
if((b-a)>=eps)
{
printf("a: %lf b: %lf, m: %lf diff: %lf\n",
a, b, m, sin(0) - sin(m));
if(fabs(sin(0)-sin(m)) <= eps){
return m;
}
if(sin(m) < 0){
b = m; // look in [a, (a+b)/2]
}
if(sin(m)>0){
a = m; // look in [[a+b]/2, b]
}
return dicho(a,b);
}
return m;
}
When running with this function you can see the solution actually converges towards pi:
a: 3.000000 b: 4.000000, m: 3.500000 diff: 0.350783
a: 3.000000 b: 3.500000, m: 3.250000 diff: 0.108195
a: 3.000000 b: 3.250000, m: 3.125000 diff: -0.016592
a: 3.125000 b: 3.250000, m: 3.187500 diff: 0.045891
a: 3.125000 b: 3.187500, m: 3.156250 diff: 0.014657
a: 3.125000 b: 3.156250, m: 3.140625 diff: -0.000968
a: 3.140625 b: 3.156250, m: 3.148438 diff: 0.006845
a: 3.140625 b: 3.148438, m: 3.144531 diff: 0.002939
a: 3.140625 b: 3.144531, m: 3.142578 diff: 0.000985
a: 3.140625 b: 3.142578, m: 3.141602 diff: 0.000009
a: 3.140625 b: 3.141602, m: 3.141113 diff: -0.000479
a: 3.141113 b: 3.141602, m: 3.141357 diff: -0.000235
a: 3.141357 b: 3.141602, m: 3.141479 diff: -0.000113
a: 3.141479 b: 3.141602, m: 3.141541 diff: -0.000052
a: 3.141541 b: 3.141602, m: 3.141571 diff: -0.000022
a: 3.141571 b: 3.141602, m: 3.141586 diff: -0.000006
a: 3.141586 b: 3.141602, m: 3.141594 diff: 0.000001
a: 3.141586 b: 3.141594, m: 3.141590 diff: -0.000003
a: 3.141590 b: 3.141594, m: 3.141592 diff: -0.000001
3.141592
But with your original recursion logic (use fabs instead of abs though):
a: 3.000000 b: 4.000000, m: 3.500000 diff: 0.350783
a: 3.500000 b: 4.000000, m: 3.750000 diff: 0.571561
a: 3.750000 b: 4.000000, m: 3.875000 diff: 0.669405
a: 3.875000 b: 4.000000, m: 3.937500 diff: 0.714499
a: 3.937500 b: 4.000000, m: 3.968750 diff: 0.736010
a: 3.968750 b: 4.000000, m: 3.984375 diff: 0.746497
a: 3.984375 b: 4.000000, m: 3.992188 diff: 0.751673
a: 3.992188 b: 4.000000, m: 3.996094 diff: 0.754243
a: 3.996094 b: 4.000000, m: 3.998047 diff: 0.755524
a: 3.998047 b: 4.000000, m: 3.999023 diff: 0.756164
a: 3.999023 b: 4.000000, m: 3.999512 diff: 0.756483
a: 3.999512 b: 4.000000, m: 3.999756 diff: 0.756643
a: 3.999756 b: 4.000000, m: 3.999878 diff: 0.756723
a: 3.999878 b: 4.000000, m: 3.999939 diff: 0.756763
a: 3.999939 b: 4.000000, m: 3.999969 diff: 0.756783
a: 3.999969 b: 4.000000, m: 3.999985 diff: 0.756793
a: 3.999985 b: 4.000000, m: 3.999992 diff: 0.756798
a: 3.999992 b: 4.000000, m: 3.999996 diff: 0.756800
a: 3.999996 b: 4.000000, m: 3.999998 diff: 0.756801
a: 3.999998 b: 4.000000, m: 3.999999 diff: 0.756802
4.000000
When compiling with -Wall -Wextra this code gives me three warnings.
$ gcc b.c -lm -Wall -Wextra
b.c: In function ‘dicho’:
b.c:12:12: warning: using integer absolute value function ‘abs’ when argument is of floating point type ‘double’ [-Wabsolute-value]
12 | if(abs(sin(0)-sin(m))<=eps){
| ^~~
b.c: At top level:
b.c:28:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
28 | void main()
| ^~~~
b.c: In function ‘dicho’:
b.c:26:1: warning: control reaches end of non-void function [-Wreturn-type]
26 | }
| ^
You need to:
Change the signature of main to int main()
Change abs to fabs
Make sure that the recursive function actually does return. For instance, the recursive call dicho(a,b); within the dicho function should be return dicho(a,b); Also, what happens if (b-a)<eps? You should return something there too.
What happens if sin(m) evaluates to 0? Then you would be stuck in an endless loop. Well, not really because fabs(sin(0)-sin(m))<=eps will catch that case, but were you really aware of that? ;)
This does not solve the issue with wrong output, but it is the minimum on your way to solving it.
You have some serious programming errors like using integer abs where you should use floating point fabs and missing return statements.
But the worst part is the algorithm is wrong. When the algorithm is wrong, it won't help to fix the programming errors. The program will still fail.
The main problem with your algorithm is that you assume that sin(a) is less than zero and that sin(b) is greater than zero. You can't just assume that!
You need to handle all cases - for instance:
if (sin(a) > 0 && sin(b) > 0)
{
// both greater than zero
// What to do now ??? Maybe there isn't a solution in this interval
}
if (sin(a) < 0 && sin(b) < 0)
{
// both less than zero
// What to do now ??? Maybe there isn't a solution in this interval
}
if (sin(a) > 0)
{
// sin(a) larger than sin(b)
// Do recursive call
return dicho(?, ?);
}
else
{
// sin(a) less than sin(b)
// Do recursive call
return dicho(?, ?);
}
But what to put into the recursive call ?
Your approach is to do
return dicho(a, (a+b)/2));
in one case and
return dicho((a+b)/2), b);
in the other.
But that will not work!
Assume the following:
sin(a) is less than zero
sin(b) is greater than zero
sin((a+b)/2) is greater than zero
In this case we can do:
option 1:
return dicho((a+b)/2), b); // very bad as both are now greater than zero
option 2:
return dicho(a, (a+b)/2)); // good! one negative and one positive
So from this you see that just cutting the interval in half isn't a good idea. You need to add code that checks whether sin at the middle point is less or greater than zero and then do the call for the correct sub-interval (depending on the value of sin(a) and sin(b))
Something like:
if (sin(a) > 0)
{
// sin(a) larger than sin(b)
if (sin((a+b)/2) > 0)
{
return dicho((a+b)/2, b);
}
return dicho(a, (a+b)/2);
}
else
{
// sin(a) less than sin(b)
.... similar to the aboove code
}
Putting all cases into a truth table is often very helpful. Like:
----------------------------------------------------------------------------------
fabs(sin(a)) < eps | | | return a;
----------------------------------------------------------------------------------
fabs(sin(b)) < eps | | | return b;
----------------------------------------------------------------------------------
sin(a) > 0 | sin(b) > 0 | | oh dear - bad
----------------------------------------------------------------------------------
sin(a) < 0 | sin(b) < 0 | | oh dear - bad
----------------------------------------------------------------------------------
sin(a) > 0 | sin(b) < 0 | sin((a+b)/2) > 0 | return dicho((a+b)/2), b);
----------------------------------------------------------------------------------
sin(a) > 0 | sin(b) < 0 | sin((a+b)/2) < 0 | return dicho(a, (a+b)/2));
----------------------------------------------------------------------------------
sin(a) < 0 | sin(b) > 0 | sin((a+b)/2) > 0 | return dicho(a, (a+b)/2));
----------------------------------------------------------------------------------
sin(a) < 0 | sin(b) > 0 | sin((a+b)/2) < 0 | return dicho((a+b)/2), b);
----------------------------------------------------------------------------------
clang is even more helpful, suggesting that you use fabs
so6.c:12:12: warning: using integer absolute value function 'abs' when argument is of floating point type [-Wabsolute-value]
if(abs(sin(0)-sin(m))<=eps){
^
so6.c:12:12: note: use function 'fabs' instead
if(abs(sin(0)-sin(m))<=eps){
^~~
fabs
so6.c:25:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^

C - pointer to array: how to copy just the value (not address) of one pointer to another?

If a pointer pr points to an array aa = {1, 2, 3, 4, 5, 0, 0, 0, 0, 0}, then we can access the array through the pointer pr[0], pr[1], pr[2],....
How can I shift the array (operated through the pointer) such that I can get{1, 1, 2, 3, 4, 5, 0, 0, 0, 0}? obviously pr[i] = pr[i-1] won't work.
Here is my code:
#include <stdio.h>
int main() {
int aa[10] = {1, 2, 3, 4, 5, 0, 0, 0, 0, 0};
int i, m, *pr = aa;
printf("\n pr[0] = %d, pr[1] = %d, pr[2] = %d, pr[3] = %d", pr[0], pr[1], pr[2], pr[3]);
for(i = 0; i < 9; i++) {
m = *(pr + i);
pr[i+1] = m;
}
printf("\n \n pr[0] = %d, pr[1] = %d, pr[2] = %d, pr[3] = %d \n", pr[0], pr[1], pr[2], pr[3]);
printf("\n \n aa[0] = %d, aa[1] = %d, aa[2] = %d, aa[3] = %d \n", aa[0], aa[1], aa[2], aa[3]);
return(1);
}
I am writing a C function for R using .Call, all the arrays in the C function have to be accessed through this type of the pointers. And I am very confused by the grammar of pointers in C.
You basically want to prepend a value, in your example 1, and remove the last value from the array, in your example a 0.
If you take a look at what happens you will see the following:
position: 0 1 2 3 4 5 6
starting: { a0 a1 a2 a3 a4 a5 a6 }
\ \ \ \ \ \
final: { b0 a0 a1 a2 a3 a4 a5 }
So you want to do as you proposed shifting each value, but you have to start at the end (or you will overwrite everything with the same value).
for(i = 9; i > 0; i--) {
pr[i]=pr[i-1];
}
pr[0] = NEWVALUE;

Pointers in C, out of the code below

What should be the output of the code below?
I cant understand what *Y-- do. If someone can explain what *Y do this strange thin in F1. Thanks in advance.
int F1(int , int *);
int A = 3;
int B = 7;
int C = 4;
int D = 2;
void main(void)
{
A = F1 (C, &D);
printf("\n%d %d %d %d", A, B, C, D);
C = 3;
C = F1 (A, &C);
printf("\n%d %d %d %d", A, B, C, D);
}
int F1(int X, int *Y)
{
int A;
A = X * *Y;
C++;
B += *Y;
printf("\n%d %d %d %d", A, B, C, D);
*Y--;
return (C);
}
with CodeBlocks the output is :
8 9 5 2
5 9 5 2
15 13 4 2
5 13 4 2
But I dont understand why 13, if I have B=B+*Y ...B=12 (?)
A = 5, B = 9, C = 3, D = 2 before calling C = F1(A, &C) ;
when we make call to F1(A, &C) see here we are passing address of C, now Y is actually referencing to C .
Instruction Execution in function call
local A variable = 5 * 3 (value at which Y pointing)
C++, Global C now becomes 4 ;
B += *Y (value at which Y pointing. this will be 4 because of previous C
++)
so B = 9 + 4. It is the reason for 13 in third line .

Triple nested C loop, integer iterator getting large before leaving inner loop

I have resolved this issue but I am not sure I understand where it came from. I believe it was because I declared par_x par_y and par_z incorrectly ( e.g. float par_y = []). I resolved it by declaring it instead with a size and then calling initialize_particle.
For interest my code is a CFD one, I am adding to it some more functionality (passive particle tracking) and I am having problems with my triple nested loop.
int ii_ter, k_ter, j_ter
void initialize_particle(int d2x, int d2y, int d2z, float *par_x, float *par_y, float *par_z){
for (ii_ter = 0; ii_ter < number_particles; ii_ter++){
for (j_ter = 0; j_ter < number_particles; j_ter++){
for (k_ter = 0; k_ter < number_particles; k_ter++){
// number of particles in each direction, centred around d2x d2y d2z
//
printf("\nvalue ii_ter: %d, j_ter: %d, k_ter: %d\n", ii_ter, j_ter, k_ter);
index_part = k_ter+j_ter*number_particles+ii_ter*number_particles*number_particles;
printf("calc index %d \n", index_part);
par_x[index_part] = 1 + d2x + k_ter - number_particles/(float)2;
par_y[index_part] = 1 + d2x + j_ter - number_particles/(float)2;
par_z[index_part] = 1 + d2x + ii_ter - number_particles/(float)2;
printf("value of parx: %5.5f y: %5.5f z: %5.5f\n", par_x[index_part], par_y[index_part], par_z[index_part]);
}
}
}
}
The code is meant to go through and place a particle in a 3d cube, the k_ter for x axis y_ter for y axis and ii_ter for z axis.
index_part should go from 0 to number_particles^3
Except my output looks like
value ii_ter: 0, j_ter: 0, k_ter: 0
calc index 0
value of parx: 34.50000 y: 34.50000 z: 34.50000
value ii_ter: 0, j_ter: 0, k_ter: 1
calc index 1
value of parx: 35.50000 y: 34.50000 z: 34.50000
value ii_ter: 0, j_ter: 0, k_ter: 2
calc index 2
value of parx: 36.50000 y: 34.50000 z: 34.50000
value ii_ter: 0, j_ter: 1, k_ter: 0
calc index 3
value of parx: 34.50000 y: 35.50000 z: 34.50000
value ii_ter: 0, j_ter: 1, k_ter: 1
calc index 4
value of parx: 35.50000 y: 35.50000 z: 34.50000
value ii_ter: 0, j_ter: 1, k_ter: 2
calc index 5
value of parx: 36.50000 y: 35.50000 z: 34.50000
value ii_ter: 0, j_ter: 2, k_ter: 0
calc index 6
value of parx: 34.50000 y: 36.50000 z: 34.50000
value ii_ter: 0, j_ter: 2, k_ter: 1
calc index 7
value of parx: 35.50000 y: 36.50000 z: 1108213794.50000
value ii_ter: 1108213760, j_ter: 2, k_ter: 2
calc index 1383989256
I have tried using a different variable and even a while loop. Even commenting out the par_x...par_z section of the code.
Help would be greatly appreciated.
Thanks
It's a late addition, but consistent with my comment — no global variables.
#include <stdio.h>
static
void initialize_particle(int d2x, int d2y, int d2z, int num, float *par_x, float *par_y, float *par_z)
{
for (int i_ter = 0; i_ter < num; i_ter++)
{
for (int j_ter = 0; j_ter < num; j_ter++)
{
for (int k_ter = 0; k_ter < num; k_ter++)
{
printf("\nvalue i_ter: %d, j_ter: %d, k_ter: %d\n",
i_ter, j_ter, k_ter);
int index_part = k_ter + j_ter * num + i_ter * num * num;
printf("calc index %d \n", index_part);
par_x[index_part] = 1 + d2x + k_ter - num / 2.0;
par_y[index_part] = 1 + d2y + j_ter - num / 2.0;
par_z[index_part] = 1 + d2z + i_ter - num / 2.0;
printf("value of parx: %5.5f y: %5.5f z: %5.5f\n",
par_x[index_part], par_y[index_part], par_z[index_part]);
}
}
}
}
int main(void)
{
float x[30];
float y[30];
float z[30];
initialize_particle(35, 36, 37, 3, x, y, z);
return 0;
}
This doesn't crash. An earlier version with 10 in place of 30 did crash!

Resources