Thread: Monte Carlo reliability program - SegFault :(

1. Monte Carlo reliability program - SegFault :(

Hello,

This is my first post here and sadly, it's because I've hit a snag in my program. I've tested it using both irand() and irandN() random number generator functions. I entered '2' for all inputs. In either case, right after all values had been entered, it gave:

Segmentation Fault (core dumped)
I attempted to use gdb to debug following a tutorial which gave me:

Program received signal SIGSEGV, Segmentation fault.
0x08050bfd in main ()
Being a programming novice, I'm not sure what do do with this info. It's not acting the same as the tutorial's example.

Here's the program and thanks for looking!!

Eric

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>

int irandN(int n);
int irand(int min, int max);
float rel(int HR, int T);

int main()
{
int NT;
int HR;
int NL;
int NV;
int NC;
int NS;
int NA;
int TV;
int TC;
int TS;
int nsV;
int nsC;
int nsS;
int i;
int j;
int k;

int nsLOC;
int nsSYS;
float RSYS;

float RV[i][j];
float RC[i][j];
float RS[i][j];

printf("\n\nEnter the number of hours the system is to be used (up to 14): ");
scanf("%d", &HR);
printf("Enter the number of bundle locations (3, 4, 5, or 6): ");
scanf("%d", &NL);
printf("Enter the number of vibration sensors (0, 1, 2, or 3): ");
scanf("%d", &NV);
printf("Enter the number of cameras (2, 3, or 4): ");
scanf("%d", &NC);
printf("Enter the number of sound sensors (1, 2, or 3): ");
scanf("%d", &NS);
printf("\nOn average, the alarm fails once in how many activations? ");
scanf("%d", &NA);

for(i=1; i<=NL; i++)
{
printf("\n\nLocation #%d: ", i);

for(j=1; j<=NV; j++)
{
printf("Enter the MTTF for vibration sensor %d: ", j);
scanf("%d", &TV);
RV[i][j] = rel(HR, TV);
}

for(j=1; j<=NC; j++)
{
printf("Enter the MTTF for camera %d: ", j);
scanf("%d", &TC);
RC[i][j] = rel(HR, TC);
}

for(j=1; j<=NS; j++)
{
printf("Enter the MTTF for sound sensor %d: ", j);
scanf("%d", &TS);
RS[i][j] = rel(HR, TS);
}
}

printf("\nEnter the number of tests: ");
scanf("%d", &NT);

for(k=1; k<=NT; k++)
{
RSYS=0;

nsSYS=0;
for(i=1; i<=NL; i++)
{
nsLOC=0;

nsV=0;
for(j=1; j<=NV; j++)
{
if( (irandN(1000)/1000) < RV[i][j] )
{
nsV = nsV+1;
}
}

nsC=0;
for(j=1; j<=NC; j++)
{
if( (irandN(1000)/1000) < RC[i][j] )
{
nsC = nsC+1;
}
}

nsS=0;
for(j=1; j<=NS; j++)
{
if( (irandN(1000)/1000) < RS[i][j] )
{
nsS = nsS+1;
}
}

if( (nsV < (NV-1)) && (nsC < 2) && (nsS < 1) )
{
nsLOC = nsLOC+1;
}

}

if( nsLOC >= NL-1 )
{
nsSYS = nsSYS+1;
}

}

RSYS = (nsSYS/NT);
printf("\n\nThe estimated reliability of this system is: %d\n\n", RSYS);

}

int irand(int min, int max)
{
static int Init = 0;
int rc;

if(Init == 0)
{
srand(time(NULL));
//void srand(unsigned int seed);                //uses same seed for testing
Init = 1;
}

rc = (rand() % (max - min + 1) + min);
return(rc);
}

int irandN(int n)
{
const unsigned range = ((unsigned)(RAND_MAX)+1)/n;
int r;

do r = rand()/range;
while(r >= n);

return(r);
}

float rel(int HR, int T)
{
float R;
R = exp(-HR/T);
return(R);
}```

2. I imagine the source of the problems is
Code:
```int i;
int j;

// ...

float RV[i][j];
float RC[i][j];
float RS[i][j];```
"i" and "j" arent initialized, but you try and create 3 arrays, all of which use these two variables as the dimensions. You need to either hard code the size of these arrays, like (of course with more reasonable dimensions instead of these)
Code:
```float RV[1][2];
float RC[10][12343];
float RS[543][42];```
or dynamically create the arrays, like
Code:
```int i;
int j;

// either manually set the values of i and j or read them in from keyboard/wherever, then:

float ** RV = malloc( sizeof(float*) * i);
// do a check to make sure RV != null, in which case malloc failed and is a "serious" problem)
// then initialize each "row" of the array:
// for loop from cnt = 0 to i
RV[cnt] = malloc( sizeof(float) * j );
// again check if failed```
And a similar process is done for the other arrays.

If you use malloc, of course you have to free each of the "j" rows of the array, (for loop) then free the RV array itself. Obviously do this for all arrays.

3. Wow! Thank you for the quick response... I got it running after setting all matrices to be 10x10.

Onto the next problem: the estimated reliability is always zero, even if I set the initial ns_ values and RSYS values are high. Could this be a problem with the random number generator?

Thanks for the help!

Eric

PS: inputs were '2' for everything up to the MTTF inputs which i entered as 100. These numbers should give RSYS -> 1
(MTTF = mean time to failure)

4. After moving some brackets around in the for loops, I've got it down to the last step:

RSYS = (nsSYS/NT);

with nsSYS=1 and NT=1 going into it [verified] but RSYS still equals zero.

last time i checked, 1/1 isn't = 0

what gives?

5. I would double check the values again, explicitly in code, try something like
Code:
```if ( nsSYS == 1 && NT == 1 )
{
printf("both 1\n");
}
else
{
printf("oh no\n");
}

RSYS = (nsSYS/NT);
printf("\n\nThe estimated reliability of this system is: %d\n\n", RSYS);```
Also, 'RSYS' is a float but your printing it as an integer. Your compiler should probably be giving you a warning that theres possible loss of precision. Print it as a "%f" instead of "%d". Let us know the exact output of these modifications.

6. Here's my input:
Code:
```Enter the number of hours the system is to be used (up to 14): 14
Enter the number of bundle locations (3, 4, 5, or 6): 3
Enter the number of vibration sensors (0, 1, 2, or 3): 0
Enter the number of cameras (2, 3, or 4): 2
Enter the number of sound sensors (1, 2, or 3): 1

On average, the alarm fails once in how many activations? 12341234

Location #1:
Enter the MTTF for camera 1: 10
Enter the MTTF for camera 2: 10
Enter the MTTF for sound sensor 1: 10

Location #2:
Enter the MTTF for camera 1: 10
Enter the MTTF for camera 2: 10
Enter the MTTF for sound sensor 1: 10

Location #3:
Enter the MTTF for camera 1: 10
Enter the MTTF for camera 2: 10
Enter the MTTF for sound sensor 1: 10

Enter the number of tests: 100```
Here's my output:
Code:
```nsSYS=1
NT=100
RSYS=0.000000

The estimated reliability of this system is: 0.000000

R=0.367879```
that RC[2][2] value is always 0.367879 ???

Here's the updated code:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>

void initrand()
{
srand((unsigned)(time(0)));
}

float randfloat();
int irandN(int n);
int irand(int min, int max);
float rel(int HR, int T);

int main()
{
int NT;
int HR;
int NL;
int NV;
int NC;
int NS;
int NA;
int TV;
int TC;
int TS;
int nsV;
int nsC;
int nsS;
int i;
int j;
int k;

int nsLOC;
int nsSYS;
float RSYS;

float RV[10][10];
float RC[10][10];
float RS[10][10];

printf("\n\nEnter the number of hours the system is to be used (up to 14): ");
scanf("%d", &HR);
printf("Enter the number of bundle locations (3, 4, 5, or 6): ");
scanf("%d", &NL);
printf("Enter the number of vibration sensors (0, 1, 2, or 3): ");
scanf("%d", &NV);
printf("Enter the number of cameras (2, 3, or 4): ");
scanf("%d", &NC);
printf("Enter the number of sound sensors (1, 2, or 3): ");
scanf("%d", &NS);
printf("\nOn average, the alarm fails once in how many activations? ");
scanf("%d", &NA);
for(i=1; i<=NL; i++)
{
printf("\n\nLocation #%d: \n", i);

for(j=1; j<=NV; j++)
{
printf("Enter the MTTF for vibration sensor %d: ", j);
scanf("%d", &TV);
RV[i][j] = rel(HR, TV);
}

for(j=1; j<=NC; j++)
{
printf("Enter the MTTF for camera %d: ", j);
scanf("%d", &TC);
RC[i][j] = rel(HR, TC);
}

for(j=1; j<=NS; j++)
{
printf("Enter the MTTF for sound sensor %d: ", j);
scanf("%d", &TS);
RS[i][j] = rel(HR, TS);
}
}

printf("\nEnter the number of tests: ");
scanf("%d", &NT);

RSYS=0;
nsSYS=0;
for(k=1; k<=NT; k++)
{
nsLOC=0;
for(i=1; i<=NL; i++)
{
nsV=0;
for(j=1; j<=NV; j++)
{
if( (randfloat()) < RV[i][j] )
{
nsV = nsV+1;
}
}
printf("%d\n", nsV);
nsC=0;
for(j=1; j<=NC; j++)
{
if( (randfloat()) < RC[i][j] )
{
nsC = nsC+1;
}
}
printf("%d\n", nsC);
nsS=0;
for(j=1; j<=NS; j++)
{
if( (randfloat()) < RS[i][j] )
{
nsS = nsS+1;
}
}
printf("%d\n", nsS);
if( (nsV >= (NV-1)) && (nsC >= 2) && (nsS >= 1) )
{
nsLOC = nsLOC+1;
}
}
printf("%d\n", nsLOC);

if( nsLOC >= (NL-1) )
{
nsSYS = nsSYS+1;
}

}
printf("nsSYS=%d\n", nsSYS);
printf("NT=%d\n", NT);
RSYS = (nsSYS/NT);
printf("\tRSYS=%f\n", RSYS);
printf("\n\nThe estimated reliability of this system is: %f\n\n", RSYS);

printf("\n\tR=%f\n", RC[2][2]);

}
float randfloat()
{
return rand()/((RAND_MAX)+1.0);
}

int irand(int min, int max)
{
static int Init = 0;
int rc;

if(Init == 0)
{
srand(time(NULL));
//void srand(unsigned int seed);                //uses same seed for testing
Init = 1;
}

rc = (rand() % (max - min + 1) + min);
return(rc);
}

int irandN(int n)
{
const unsigned range = ((unsigned)(RAND_MAX)+1)/n;
int r;

do r = rand()/range;
while(r >= n);

return(r);
}

float rel(int HR, int T)
{
float R;
R = exp(-HR/T);
return(R);
}```
You'll see I changed random number generators

7. I would say your output doesnt correspond to the given input and code.

Code:
```nsSYS=0;
// ...
if( nsLOC >= (NL-1) )
{
nsSYS = nsSYS+1;
}

}
printf("nsSYS=%d\n", nsSYS);
printf("NT=%d\n", NT);
RSYS = (nsSYS/NT);
printf("\tRSYS=%f\n", RSYS);```
It is zero because you assign nsSYS it to zero. The only place where nsSYS changes is in the above portion of code, if "nsLOC >= NL-1". I looked to see when this happens and its impossible to debug due to all of your conditions and huge number of variables. I ran this code with your input and got 0 as nsSYS, so RSYS was 0.

You'll see I changed random number generators
No, I wont. I will ntoice that you ignored my suggestion above. Try and edit the code to include the debug statements I suggested above (using ==, etc). Then post all of your input, output, and code. I will then try it on my side to verify your results. If you dont follow this then I give up and wont waste my time anymore.

8. Originally Posted by Kibble Fat
Here's my output:
Code:
```nsSYS=1
NT=100
RSYS=0.000000```
Here's the updated code:
Code:
```printf("nsSYS=%d\n", nsSYS);
printf("NT=%d\n", NT);
RSYS = (nsSYS/NT);
printf("\tRSYS=%f\n", RSYS);```
(nsSYS/NT) tells the computer to divide two integers and give the result as an integer. 1/100 = 0.01 = 0 when you're doing integer math. This result is then cast into a float with the value 0.0 and assigned to RSYS.

Cast one of the operands to a float and the code will work as expected since that will force the division to be done using floating point values.