![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Jun 2005
Posts: 8
| [question]Simulating the Reliability of a Component-Based System [with my code] 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. Your program must: 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;
}
Last edited by burbose; 06-13-2005 at 07:55 AM. |
| burbose is offline | |
| | #2 |
| Registered User Join Date: Jun 2005
Posts: 8
| |
| burbose is offline | |
| | #3 |
| Anti-Poster Join Date: Feb 2002
Posts: 1,212
| Learn how to use rand() here.
__________________ Rule #1: Every rule has exceptions Traveller's Dilemma Contest Site - Results posted! |
| pianorain is offline | |
| | #4 |
| Registered User Join Date: Jun 2005
Posts: 8
| 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);
}
2 decimal space. |
| burbose is offline | |
| | #5 | |
| Anti-Poster Join Date: Feb 2002
Posts: 1,212
| 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. Quote:
__________________ Rule #1: Every rule has exceptions Traveller's Dilemma Contest Site - Results posted! | |
| pianorain is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Smooth walking on tile based map system | abraham2119 | C Programming | 8 | 07-10-2009 10:33 AM |
| Search a Softwrae Dev Guide System Which based on Web application.... | userpingz | General Discussions | 0 | 06-10-2009 07:55 PM |
| My calculation, delete and update functions dont work (Programming language C) | TKaeWoods | C Programming | 2 | 03-05-2009 10:25 AM |
| [question]Simulating the Reliability of a Component-Based System | burbose | C Programming | 3 | 06-13-2005 07:28 AM |
| Setting Up a sector based grid system | Auron | C Programming | 6 | 05-20-2005 08:19 AM |