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:
Quote:
Segmentation Fault (core dumped)
I attempted to use gdb to debug following a tutorial which gave me:
Quote:
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);
}