# Thread: [question]Simulating the Reliability of a Component-Based System [with my code]

1. ## [question]Simulating the Reliability of a Component-Based System [with my code]

[question]Simulating the Reliability of a Component-Based System

Acknowledgement

This part of the lab is based on Question 19 in Chapter 4 of Etter's "Engineering Problem Solving with C"

Problem

In this part of the lab you will write a program to simulate the reliability of the following component-based system:

http://pic.ourplus.com/yourpic/2005/...7061770b15.gif

Note that the reliability of such a system is measured as the probability that the system succeeds (i.e. does not fail). The system succeeds if there is a path from A to B through components that do not fail. So, for example, if Component 1 does not fail, the whole system succeeds because you can get from A to B through Component 1. However, if both Component 1 and Component 2 fail, the only way the system can succeed is if neither Component 3 nor Component 4 fails.

prompt the user for the reliability of each of the four components (as a number between 0.0 and 1.0 inclusive that represents the probability that the component will not fail)
prompt the user for the number of simulations to perform
compute the average reliability of the system
report the average reliability of the system to the user as a number between 0.0 and 1.0 representing the probability that the system will not fail

For each simulation, use the rand_float function developed in Chapter 4 of your textbook to randomly determine if each component succeeds or fails. The entire system will succeed if:

( Comp 1 succeeds ) OR ( Comp 2 succeeds ) OR ( Comp 3 AND Comp 4 succeed )

By counting the number of simulations for which the system succeeds, you can estimate the reliability of the system as:

( number of times system succeeds ) / ( number of simulations )

Note: Section 4.5 of Etter provides more details on how to use the rand_float function to simulate the success or failure of each component.

Developing a test suite

Before you write your program, develop a test suite that can be used to test your program once it is written. Refer to the notes in earlier labs on how to choose appropriate test values.

Having chosen your test values, compute by hand the expected output. After you have written your program, you can then test your program with each of your test values and check that your program produces the expected value. If it doesn't, you probably have one or more logic errors in your code.

Note: the theoretical reliability of this system (i.e., the probability that it does not fail) is given by:

R = r1 + r2 - r1r2 + r3r4 ( 1 - r1 - r2 + r1r2 )

where rn is the reliability of component n in the system (i.e. a number between 0.0 and 1.0 that represents the probability that the component will not fail).

Algorithm development

Sketch out, in point form, an algorithm for solving the problem.

Coding

Create a new project in Dev-C++ and implement your algorithm in C. Remember to first implement your functions as stubs and to compile your code before attempting to implement any of the functions. Remove any syntax errors before proceeding further. Implement your functions one at a time. Compile your code after implementing each function and remove any syntax errors as you go. Read the Guide to Dev-C++ for instructions on how to create a new project, edit your source code, and compile and run your program.

Testing

Once your code has successfully compiled, verify that it produces correct results using the test suite that you developed earlier. If any of your tests fails, you must look for logic errors in your algorithm (which will, of course, have resulted in logic errors in your code!)

Debugging

Now that the programs that you are writing are becoming a little more complex, you might find it useful to use the debugger that is integrated into the Dev-C++ environment. Read the Guide to debugging with Dev-C++ for instructions on how to use the debugger.

Code:
```#include <stdio.h>
#include <stdlib.h>

float comp1, comp2, comp3, comp4;
int n;

int main (){

printf ("Enter Comp 1 's Reliability :" );
scanf ("%f" , &comp1 );
printf ("Enter Comp 2 's Reliability :" );
scanf ("%f" , &comp2 );
printf ("Enter Comp 3 's Reliability :" );
scanf ("%f" , &comp3 );
printf ("Enter Comp 4 's Reliability :" );
scanf ("%f" , &comp4 );

printf ("How many time do u want to Simulate?\n");
scanf ("%d" , &n );

int i = 0;
int nextNum;
while( i < n )
{
nextNum = rand();
printf( "%d \n", nextNum );

i++;
}
/* I dont know how to write the code here:(

i dont know how to use rand()

when i try to use rand(), the number is pretty much large . but i just need random float number which can  compare with 0.0-1.0

any 1 can help me?
*/

system ("PAUSE");
return 0;
}``` 2. 3. Learn how to use rand() here. 4. Code:
```#include <stdio.h>
#include <stdlib.h>

int GetRand(int min, int max);

int main(void)
{
int i, r;

for (i = 0; i < 20; i++)
{
r = GetRand(0, 1);
printf ("Your number is %d\n", r);
}
system ("pause");
return(0);
}

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

if (Init == 0)
{
/*
*  As Init is static, it will remember it's value between
*  function calls.  We only want srand() run once, so this
*  is a simple way to ensure that happens.
*/
srand(time(NULL));
Init = 1;
}

/*
* Formula:
*    rand() % N   <- To get a number between 0 - N-1
*    Then add the result to min, giving you
*    a random number between min - max.
*/
rc = (rand() % (max - min + 1) + min);

return (rc);
}```
how to get the number between 0 and 1,
2 decimal space. 5. Learning how to use rand() doesn't mean copying and pasting code straight into your program. Your first clue that the GetRand function wouldn't work in your case should have been that it returned an int, not a float. Originally Posted by Random Number FAQ
In order to generate a pseudo-random number you can use the rand() function that is defined in stdlib.h (or cstdlib for C++). This will return you a number between 0 and RAND_MAX (also defined in stdlib.h) <snip>
The above fact and some simple division should net you an answer. Popular pages Recent additions 