Thread: Need help with Code! Having problems.

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Need help with Code! Having problems.

    Hey guys..

    This assignment is due in class tomorrow, but im really stuck on it and cannot figure out why its not working. I have been trying for hours and no luck.

    It will not find any errors or anything, but when I run it, it will not use the use input (E, R1, R2, R3) and send that out of the main program and get returned the values after the equations.

    Basically, it gets up to the part where it will send the R1, R2, and R3 values out of the main and I get a program fault and it closes out.

    I just don't know what I am not seeing here... So please, if any of you guys can help, please!

    The assignment is under the code.

    Here is the code I have so far!

    -------------------------------------------------------------
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main ()
    {
    int E, R1, R2, R3, S, P;
    void info (void);
    int resist_P(int R1, int R2, int R3);
    int resist_S(int R1, int R2, int R3);
    
    printf("Please enter source voltage: ");
    scanf("%i", &E);
    printf("Please enter first of three resistors: ");
    scanf("%i", &R1);
    printf("Please enter second of three resistors: ");
    scanf("%i", &R2);
    printf("Please enter third of three resistors: ");
    scanf("%i", &R3);
    
    info ();
    printf("E R1 R2 R3");
    printf("\n%i\t%i\t%i\t%i\n\n", E, R1, R2, R3);
    
    P= resist_P(R1, R2, R3);
    S= resist_S(R1, R2, R3);
    
    printf("Power for parallel Power for series");
    printf("\n%6i %i", E*E/P, E*E/S);
    
    printf("\n\nCurrent for parallel Current for series");
    printf("\n%6i %i\n\n", E/P, E/S);
    
    return 0;
    }
    
    void info (void)
    {
    printf("\n\nMy Student Information\n");
    printf("ESC 151 Spring 08\n");
    printf("Recitation 4, Feb 4, 2008\n\n\n");
    return;
    }
    
    int resist_P(int R1, int R2, int R3)
    {
    int resist_p = 1/(1/R1+1/R2+1/R3);
    return resist_p;
    }
    
    int resist_S(int R1, int R2, int R3)
    {
    int resist_s = (R1+R2+R3);
    return resist_s;
    }
    ---------------------------------------------------------------

    And here is the assignment:

    *The program asks for the user to input the source voltage, E, and resistance of three resistors, R1, R2, R3.

    * Program has two functions resist_P(R1,R2,R3) and resist_S(R1,R2,R3) to calculate values of equivalent parallel and series resistances.

    * It has a function power(voltage,R) to calculate the power consumed. The program prints the R1,R2,R3 and E values in the format below. The main calls power(voltage,R) twice, once to calculate and print the power for the parallel circuit and then for the series circuit. It also prints the current_parallel and current_sries. Output on the screen has the following format.

    Hint: Parallel resistors= 1/ (1/R1+1/R2+1/R3)
    Series resistors=R1+R2+R3.
    Current= E/Equivalent resistance
    Power= E*E/Reqv.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you divide by zero in resist_P. No really: there's a difference between 1/R1 and 1.0/R1.

    Also: function prototypes should appear outside of main, unless you have a very good reason to put them inside. (Hint: you don't.)

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The parallel resistors calculation is not going to result in an int and so needs to return a double type.

    All the inverses in the resist_P calculation function result in 0 because they're integers, and get truncated to 0. These need to be doubles instead in order to prevent a divide-by-zero problem, so we should promote them to doubles as they're passed to the function.

    Code:
    double resist_P(double R1, double R2, double R3);
    You will also need to change some format specifiers for your printfs and some variables need to be changed to doubles, but I'll leave that as an exercise for you.

    And as tabstop said (on preview), the function prototypes should be outside of main.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    Ahhh yes, stupid of me to not use double...

    I converted all my values to double, and also, changed my 1 to 1.0 like said above and it is working now.

    Now I just to tweak a few more things and I should have the rough draft of this.. In class we have to then change it to his format, but thats not the bad part.

    Thank you so much for the help so far, I will post the code in a bit revised and with any other questions or anyway you guys can help me make it better.

    Thanks again!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just remember to indent a bit.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with compiling code in cygwin
    By firyace in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2007, 08:16 AM
  2. Problems with GetDiskFreeSpaceEx/my code...
    By scrappy in forum Windows Programming
    Replies: 1
    Last Post: 07-30-2003, 11:16 AM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 02:02 AM
  5. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM