Thread: Need help on this program!

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    34

    Need help on this program!

    Ok, so I have to calculate the reliability of the circuit below (sorry, the picture is somewhat crude). It doesn't really matter if you look at it that much because it is explained below:

    http://img110.imageshack.us/img110/6769/reliabfi3.jpg

    Anyways, I already created this program to find the reliability of x number of circuits:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        
    int a, b, c, d, e;
    double s;
    double t;
    double r;
    int num;
    
    printf("Enter the number of circuits you want to test: ");
    scanf("%d", &num);
    fflush(stdin);
    
    for (t=0; t<num; t++)   //  loop for number of trials
    {
        
    r = rand()%100;   // reliability of circuit
    if (r > 20)    //reliability of a
    {
    	a = 1;
    }
    	else 
    	a = 0;
    
    r = rand()%100;
    if (r > 25)           //reliability of b
    {
    	b = 1;
    }
    	else
    	b = 0;
    
    r = rand()%100;
    if (r > 10)           //reliability of c
    {
    	c = 1;
    }
    	else 
    	c = 0;
    
    r = rand()%100;
    if (r > 10)          //reliability of d
    {
    	d = 1;
    }
    	else 
    	d = 0;
    
    r = rand()%100;
    if (r > 30)             //reliability of e
    {
    	e = 1;
    }
    	else
    	e = 0;
    
    {
    if (a+d==2||b+d==2||c+e==2)        // computes number of successes
    {
    	s = s + 1;
    }
    }
    }
    
    printf("The reliability of the circuit is: %f%%\n", ((s/t)*100));
    
    return(0);
    }
    Now, I need to write a program that calculates the theoretical (mathematical) reliability of the circuit. Here is what I have so far, but I am stuck and I'm not sure what to do next. In my .txt file I have set:

    0-.80
    1-.75
    2-.90
    3-.90
    4-.70

    Again, I need to calculate the expected reliability of the circuit. The cost is a different part, but I don't need help with that right now, so it can be ignored for now... Anyways, the mathematical reliability of the circuit is 94.635%. To calculate this, you know that 95% will get through A or B, and 90% of those will get through D, or 85.5% through the top part of the circuit. The probability of something getting through the bottom part is 63%. To calculate the failure, you take the probability of the top failing times the probability of the bottom failing. .145 x .37 = .05635 is the chance of failing. 1 - failure = success = .94635.

    So basically I need some help figuring out how to make the program do this. I'm new to structs and don't understand them very much, some guidance with the coding would be greatly appreciated.

    Here is what I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUMBER_OF_CIRCUITS 5
    
    int main()
    {
    
    double a, b, c, d, e; 
    
    typedef struct{
            char name;
            double reliability;
            double cost;
    } circuit;
    
    
    enum components {a = 0, b = 1, c = 2, d = 3, e = 4};
    
    for(i = 0; i < NUMBER_OF_CIRCUITS; i++)
          circuits[i].count = 0;
    
    inputFile = fopen("circuitreliability.txt", "r");
    
    return (0);
    }
    TIA for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Code:
    #define NUMBER_OF_CIRCUITS 5
    
    typedef struct{
            char name;
            double reliability;
            double cost;
    } circuit;
    
    int main()
    {
        char buff[BUFSIZ];
        circuit circuits[NUMBER_OF_CIRCUITS];
        FILE *inputFile = fopen("circuitreliability.txt", "r");
        int n = 0;
    
        while ( n < NUMBER_OF_CIRCUITS && 
                fgets( buff, sizeof buff, inputFile ) != NULL ) {
            if ( sscanf( buff, "%c-%lf", &circuits[n].name, &circuits[n].reliability ) == 2 ) {
                n++;
            }
        }
    
        // now process circuits[i] from 0 to n-1
    Some points
    - we compare n in the while loop to make sure we don't overflow the circuits array
    - we check the result of the sscanf to make sure the data is valid
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Why is the struct outside of int main? Also, don't I need that enum to use my input file? Eventually, I am going to have to do some economics and figure out the best way to do things in terms of the cost of making the circuit more reliable vs. less reliable.

    Thanks for the help, I just don't understand why you did some of the things and why I don't need to use to the enum (or shouldn't use the enum).

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    As a general rule structs should be defined outside of functions and even inside header files should the project be large enough.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Is there anything wrong with using the enum or is that totally unnecessary? How does it know that A = 0, B =1, etc. without the enum?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Well if you had 50, or 500 then the enum would be completely the wrong idea.
    You have an array, that is all you should need.

    The struct contains an identity, which you can use should that kind of access be necessary in future.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The enumerate feature is one you rarely use, in C. After learning to use it, I've never used it once in 10 years of hobby programming.

    What's wrong with:
    int A = 0;
    int B = 1;
    etc?

    Declaring the variable and assigning a value which may be changed, or

    const int A = 0;
    const int B = 1;

    Declaring a constant variable, whose value may not be changed, at all.

    If you have several branches in your circuit, you really don't need A,B,C, etc., because you can arrange the data, and the results, to fit inside the appropriate element number of an array of structs, e.g.

    Code:
    0   1  2  3  4  5 ==>Element numbers in an array of structs. 
    =================
    82 74 93 88 76 97 ==>Values put into the array elements listed above, according to their branch number. 
    So element 0 == branch 0, element 1 == branch 1, etc. No A,B,C, needed.
    Last edited by Adak; 12-06-2007 at 01:53 PM.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    I see, thanks for the help. When I go to integrate the cost into the circuit, is this done in a similar way?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Yes, you can expand the struct with as many members as you need to represent all the information about a node in your circuit.

    I'm assuming for the purposes of this exercise that the connectivity of the various nodes is fixed?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Do you mean that the cost of the circuit and the reliability are fixed? There is another part of the project where I have to take different circuits with different reliabilities and figure out which one is the best to use (from a profitability standpoint).

    Here is what I have now, I've never written a program with structs before and I'm not sure how to write the functions (if I was just writing functions for the circuit, I think I could do it, but I'm not sure how I integrate it so that it uses 0, 1, 2, 3, etc for the circuits). My TA checked this for me today and he said he thought I was pretty much done except for writing the the functions. I don't need you to write all of the functions for me, but an example would be nice.

    Here is what I have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUMBER_OF_CIRCUITS 5
    
    typedef struct{
            char name;
            double reliability;
            double cost;
    } circuit;
    
    int main()
    {
    
    circuit circuits[NUMBER_OF_CIRCUITS];
    FILE *inputFile = fopen("circuitreliability.txt", "r");
    int n = 0;
    for(n = 0; n < NUMBER_OF_CIRCUITS; n++)
          circuits[n].name = 0;
    
    
    return (0);
    }
    Thanks for the help so far. I know all I would have to do is figure out the programming math essentially, but I don't understand how I use the numbers in the txt file in the functions.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The overall code is correct (except for the indentation!), but I don't assume it's what you want.
    First of all, you MUST check what fopen returns. If it returns NULL, it failed to open the file you specified. If it failed to open the file and you try to read from it, it tries to read from NULL which is instant crash on x86.
    For the struct, do you want an actual string, like say "this is my string"? Then you need char* and malloc/calloc, or a fixed-length array to which you can add your name to.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Crap! I didn't even look at the code before I opened it, just copy and pasted it from what I had done at the lab, and this is not it! I guess it didn't save correctly or something. Ugh! I will try to remember what I had before and post it back up here.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Ok, this is what I think I had before (it as at least pretty close, could be a couple things wrong/different):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUMBER_OF_CIRCUITS 5
    
    typedef struct{
            char name;
            double reliability;
            double cost;
    } circuit;
    
    int main()
    {
    
    circuit circuits[NUMBER_OF_CIRCUITS];
    FILE *inputFile = fopen("circuitreliability.txt", "r");
    FILE *outputFile;
    double reliability;
    double cost;
    int n = 0;
    int index;
    for(n = 0; n < NUMBER_OF_CIRCUITS; n++)
          circuits[n].name = 0;
    
    
    fscanf(inputFile, "&#37;d-%f-%f", &index, &reliability, &cost);
    		circuits[index].name =reliability;
            circuits[index].name =cost;
      
    fprintf(outputFile, "%f -- Reliability %f\n", circuits[n].name);
    fprintf(outputFile, "%f -- Cost %f\n", circuits[n].name);
    return (0);
    }
    Now I just need to know how I integrate the 0-.80, 1-.75, etc. in my text file into the functions.
    Last edited by Northstar; 12-07-2007 at 12:33 AM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Aside from the above problems I mentioned...
    Code:
    circuits[index].name =reliability;
    circuits[index].name =cost;
      
    fprintf(outputFile, "&#37;f -- Reliability %f\n", circuits[n].name);
    fprintf(outputFile, "%f -- Cost %f\n", circuits[n].name);
    WTF? You're trying to assign a double to a char and READ a double into a char?
    Have you been drinking again, my dear?

    Your for loop is entirely un-correct to. Perhaps you want to add braces { } to mark the start and end of the loop?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Whoops, forgot about the for loop, I had that correct at the lab and then when the file did not save correctly I forgot to put it back in. I don't really understand what I am doing wrong? The TA seemed to think I was at least on the right track.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM