Like Tree3Likes
  • 1 Post By std10093
  • 1 Post By Cat
  • 1 Post By jimblumberg

Ohm's Law Calculator Help (ALMOST COMPLETE)

This is a discussion on Ohm's Law Calculator Help (ALMOST COMPLETE) within the C Programming forums, part of the General Programming Boards category; Hello, I have a lab assignment in an introductory programming class at the college course. The assignment is to create ...

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

    Ohm's Law Calculator Help (ALMOST COMPLETE)

    Hello,

    I have a lab assignment in an introductory programming class at the college course. The assignment is to create an ohm's law calculator.


    The program should prompt the user for 4 different inputs, Resistance (in ohms), Potential difference (in Volts), Current (in Amps) and Power (in watts). The user must input at least two inputs. Valid inputs include any real number except for -1. If a -1 is inputted, your program should recognize that the variable containing the -1 is the variable that you are solving for. The following possibilities must be dealt with:



    1. If less than two inputs are entered:


    • Prompt the user “NOT ENOUGH DATA"


    2. If two inputs are entered:


    • Then calculate the other two values


    3. If three inputs are entered:


    • Verify that the three inputs are valid (i.e. if I, V & R are input, validate that I*R = V within 2%). If valid, then calculate the fourth variable. If not valid prompt the user “INVALID DATA” and end the program.


    4. If Four inputs are entered:


    • Verify that all four inputs are valid within 2%. If valid, prompt the user “VALID DATA” if not valid, prompt the user “INVALID DATA”.


    I have written all of the program and it all works EXCEPT; I am having trouble getting the calculator to work if 4 inputs are entered. I feel that I have made a minor error but I am having trouble locating it. My code is attached. Help please!


    Jason


    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main (void)
    {
    
    
    float voltage , current , resistance, power, b, voltagecheck = 0, currentcheck = 0, resistancecheck = 0, powercheck = 0;
    
    
    printf("OHMS LAW CALCULATOR \n");
    printf("Enter voltage in volts \n");
    scanf("%f", &voltage);
    
    
    printf("Enter current in amps \n");
    scanf("%f", &current);
    
    
    printf("Enter resistance in ohms \n");
    scanf("%f", &resistance);
    
    
    printf("Enter power in watts \n");
    scanf("%f", &power);
    
    
    
    
    
    
    if ((voltage == -1) && (current == -1) && (resistance == -1))
        {
        printf("NOT ENOUGH DATA");
        goto ENDING;
        }
        else if ((voltage == -1) && (current == -1) && (power == -1))
        {
        printf("NOT ENOUGHT DATA");
        goto ENDING;
        }
        else if ((current == -1) && (resistance == -1) && (power == -1))
        {
        printf("NOT ENOUGHT DATA");
        goto ENDING;
        }
        else if ((voltage == -1) && (resistance == -1) && (power == -1))
        {
        printf("NOT ENOUGHT DATA");
        goto ENDING;
        }
    
    
    if ((voltage == -1) && (power == -1))
        {
        voltage = current * resistance;
    
    
        power = pow(current, 2) * resistance;
    
    
        printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power);
        goto ENDING;
        }
        else if ((voltage == -1) && (current == -1))
        {
        voltage = sqrt(power * resistance);
        current = sqrt(power / resistance);
        printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power);
        goto ENDING;
        }
        else if ((voltage == -1) && ( resistance == -1))
        {
        voltage= power / current;
        resistance = voltage / current;
        printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power);
        goto ENDING;
        }
        else if ((current == -1) && (resistance == -1))
        {
        current= power / voltage;
        resistance= voltage / current;
        printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power);
        goto ENDING;
        }
        else if ((current == -1) && (power == -1))
        {
        current= voltage / resistance;
        power= voltage * current;
        printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power);
        goto ENDING;
        }
        else if ((resistance == -1) && (power == -1))
        {
        resistance= voltage / current;
        power= voltage * current;
        printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power);
        goto ENDING;
        }
    
    
    
    
    if ( voltage == -1)
        {
        voltage = current * resistance;
        printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power);
        goto ENDING;
        }
        else if (current == -1)
        {
        current= voltage / resistance;
        printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power);
        goto ENDING;
        }
        else if (resistance == -1)
        {
        resistance= voltage / current;
        printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power);
        goto ENDING;
        }
        else if (power == -1)
        {
        power = voltage * current;
        printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power);
        goto ENDING;
        }
    
    
    if ((voltage != -1) && (current != -1) && (resistance != -1) && (power != -1))
    {
        voltagecheck = (current * resistance);
        scanf("%f", &voltagecheck);
    
    
        currentcheck = (voltage / resistance);
        scanf("%f", &currentcheck);
    
    
        resistancecheck = (voltage / current);
        scanf("%f", &resistancecheck);
    
    
        powercheck = (voltage * current);
        scanf("%f", &powercheck);
     
    }
        if ((voltagecheck < (voltage - (voltage * 0.02))) && (voltagecheck > (voltage + (voltage * 0.02))) && 
            (currentcheck < (current - (current * 0.02))) && (currentcheck > (current + (current * 0.02))) &&
            (resistancecheck < (resistance - (resistance * 0.02))) && (resistancecheck > (resistance + (resistance * 0.02))) &&
            (powercheck < (power - (power * 0.02))) && (powercheck > (power + (power * 0.02))))
        {
            printf("vofltagecheck: %f. \n currentcheck: %f. \n resistancecheck: %f. \n powercheck: %f. \n" ,voltagecheck, currentcheck, resistancecheck, powercheck);
            printf("VALID DATA");
        }
        
        else
        {
        printf("INVALID DATA");
        goto ENDING;
        }
    
    
    
    
    ENDING:
    scanf("%f",&b);
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Are you using goto or I am getting blind? :O
    stahta01 likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    I'm using goto to get to the end of the program so that it doesn't print unnecessary information, if I understand your question correctly.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,487
    Within 2 % tolerance implies the use of fabs might be a good idea.

    if (fabs(1 - N/D) < 0.02)

    The correct value is the one I normally use for D. So, I would use the user value for N. In other words, the check value for D; and the user input for N.

    FYI: Most graders I have had would take off a large number of points for using a goto.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,680
    There are many better ways of structuring this program than using goto.

    Also this looks very complicated even discounting the horrible goto statements.
    Code:
       if ((voltage == -1) && (current == -1) && (resistance == -1))
       {
          printf("NOT ENOUGH DATA");
          goto ENDING;
       }
       else if ((voltage == -1) && (current == -1) && (power == -1))
       {
          printf("NOT ENOUGHT DATA");
          goto ENDING;
       }
       else if ((current == -1) && (resistance == -1) && (power == -1))
       {
          printf("NOT ENOUGHT DATA");
          goto ENDING;
       }
       else if ((voltage == -1) && (resistance == -1) && (power == -1))
       {
          printf("NOT ENOUGHT DATA");
          goto ENDING;
       }
    Think about using a loop instead of the goto. After all you probably want to re-enter the data if you get improper input.

    Jim

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    also you only NEED two inputs of data, ohms law and the power law will give you the other 2

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    My professor suggested i use goto, so I don't know what to tell you (this is an introductory course). Also, I know how Ohm's law works... it's ridiculous that I have to create a program that will handle 1-4 inputs but that is what the assignment calls for.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by jrjones0109 View Post
    My professor suggested i use goto
    That is way too hard for me to believe. Are you sure you understood correctly what the prof said? In any case, read again Jim's post.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by jrjones0109 View Post
    My professor suggested i use goto
    If that is true I'd probably recommend you drop the course, or try to take it with another professor. The blind leading the blind is no way to learn programming.
    Crossfire likes this.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Cat View Post
    If that is true I'd probably recommend you drop the course, or try to take it with another professor. The blind leading the blind is no way to learn programming.
    I fully disagree. You should discuss about what you were told here with your professor, let him say why he said (if he really said) such a thing and then you make a decision
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Yes I'm sure he said that. goto was not in my original program until he suggested I put it there to get rid of the duplicate print statements I was getting. Using a loop does make sense though. Assuming I implement one of the loop functions that still doesn't solve my problem with that I stated in my original post. Something has to be wrong with my final if statement. I understand using goto is sloppy but for the time being it is working.

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by jrjones0109 View Post
    I understand using goto is sloppy but for the time being it is working.
    I guess it depends on whether your goal is to pass a programming course or learn to be a programmer. If you just want to get your grade and never look back, that's one thing. If you actually plan to program long term, you should set higher standards for yourself. "Sloppy but working" code is toxic to any project it makes its way into. It's a maintenance nightmare - the time it takes to clean up bad code after it's in production is often exponentially larger than the time it would take to do it right the first time.

    Repetition becomes habit. An introductory programming course is the place where you will instill habits - make sure you're instilling the habits you want to have when you're done with school.
    Last edited by Cat; 03-23-2013 at 11:22 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,680
    Here is an example of your data entry that removes the goto:
    Code:
       int repeat;
       do
       {
          repeat = 1;
          printf("OHMS LAW CALCULATOR \n");
          printf("Enter voltage in volts \n");
          scanf("%f", &voltage);
    
          printf("Enter current in amps \n");
          scanf("%f", &current);
    
          printf("Enter resistance in ohms \n");
          scanf("%f", &resistance);
    
          printf("Enter power in watts \n");
          scanf("%f", &power);
    
          if ((voltage == -1) && (current == -1) && (resistance == -1))
          {
             printf("NOT ENOUGH DATA");
          }
          else if ((voltage == -1) && (current == -1) && (power == -1))
          {
             printf("NOT ENOUGHT DATA");
          }
          else if ((current == -1) && (resistance == -1) && (power == -1))
          {
             printf("NOT ENOUGHT DATA");
          }
          else if ((voltage == -1) && (resistance == -1) && (power == -1))
          {
             printf("NOT ENOUGHT DATA");
          }
          else
          {
             printf("\nTry again\n");
             repeat = 0;
          }
       } while(repeat);
    This just removes the gotos, nothing else. I didn't change any of your logic and it probably isn't an ideal solution but it does eliminate several goto statements.

    Also note that comparing a floating point number with the == or != operators is not recommended. Floating point numbers are approximations.


    Jim
    stahta01 likes this.

  14. #14
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    wow EVERY programmer i know, and some prof`s from 5 different schools that i help tutor/teach with/for, simply says "goto is not part of programming, it is only left there because of old habits."

    does your programming teacher actually program, or just got thrown into teaching C? lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help out a complete beginner
    By Jmcrofts in forum C++ Programming
    Replies: 7
    Last Post: 07-28-2007, 12:09 PM
  2. complete reference to API???
    By code2d in forum Windows Programming
    Replies: 3
    Last Post: 12-07-2006, 09:38 PM
  3. Loop will not complete
    By jms318 in forum C Programming
    Replies: 7
    Last Post: 11-04-2006, 06:45 PM
  4. complete newbie help
    By terracota in forum Windows Programming
    Replies: 4
    Last Post: 11-11-2004, 04:44 PM
  5. Complete Beginner, HELP!!
    By britneyspy in forum C++ Programming
    Replies: 19
    Last Post: 06-12-2003, 11:01 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21