Like Tree4Likes

My first attempt at writing code

This is a discussion on My first attempt at writing code within the C Programming forums, part of the General Programming Boards category; I am taking a C programming class and I know I am not at the level you guys are. This ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    9

    My first attempt at writing code

    I am taking a C programming class and I know I am not at the level you guys are.

    This program is designed to output to R2 to start a motor. R1 is set at 100 ohms. The load (motor) is input by the user manually. Its suppose to be done according to the maximum power transfer theorem.
    The circuit is basically R1, R2 and motor all in series with each other. The only input is motor ohms and voltage.

    Code:
    #include <math.h>
    #include <stdio.h>
    int
    main (void)
    {
        
    
    
            double motor, power_supply, current, resistor_1,resistor_2, resistor_2_power, motor_power, resistors_1_2, maximum_power;
            while ( resistor_2 < 1) {
            power_supply = 16;
            current = 0;
            resistor_2 = 10000;
            motor = 0;
            resistor_2_power = 0;
            motor_power = 0;
            resistors_1_2 = 0;
    
    
            printf (" \n\n                        New Voltage and Load:\n\n\n");
        resistor_1 = 100;
        while (( power_supply < 1) || (power_supply > 15)){ /* voltage range*/
        printf ("    Please enter the value for the Power supply between 1 and 15 volts\n");
        scanf ("%lf", &power_supply);
        if (( power_supply < 1) || (power_supply > 15)){    /* error message*/
        printf (" %.1lf volts\n", power_supply); /* so you can see what was entered*/
        printf (" The Input voltage is out of range and could damage the equipment\n\n");
        } }
        /***    the above 7 lines are to make sure the input voltage is within range. If it is not it will loop until an input within range is entered****/
        while (motor < 100 || motor > 5000){
        printf ("    Please enter the value for motor between 100 and 5000\n\n\n");
        scanf ("%lf", &motor);
        if (motor < 100 || motor > 5000) /* error message*/    {
        printf (" %.0lf ohms\n", motor); /* so you can see value entered*/
        printf (" The value for the load resistance is out of range and could damage the equipment\n");
        }    }
        /**** the above 7 lines are to make sure the entered load value is within range. If it is not it will loop untill a value within range is entered****/ 
        resistor_2 = motor; /* starting point for output R2*/
         while (resistor_2 >= 0){ /* beginning of countdown loop*/
        resistors_1_2 = resistor_1 + resistor_2; /*adds both resistors together*/
        current = power_supply / (resistors_1_2 + motor); /*current for the cirtcuit V/I=R*/
        resistor_2_power = current * resistor_2 * current;  /*  I * R = volts * amps = watts*/
        motor_power = current * motor * current; 
        printf ("Output= %.0f   R1 and R2= %.0f   RL=%.0f   R2 Power= %.15lf \n ", resistor_2, resistors_1_2, motor, resistor_2_power);
            printf ("                                  Motor Power= %.15lf\n\n %c", motor_power);
                /*not sure if this is the best layout for the outputs but it works*/
        if (resistors_1_2 == motor) {
                printf ("               equal\n\n\n"); } /*added a few extra \n so you can see where the only equal is*/
        if (resistors_1_2 != motor) {
                printf ("               unequal\n"); }
        resistor_2 = resistor_2 - 1; /* count down increments*/
        }    
        if (motor_power > .001){
        maximum_power = motor_power * 1000;
        printf (" Maximum power transferred to RL = %f  mWatts\n", maximum_power);}
        if (motor_power < .001) {
            maximum_power = motor_power * 1000000;
            printf ("Maximum power transferred to RL = %f uWatts\n", maximum_power);}
        }
        return (0);
        }

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    And what is your question?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    I guess my question is how can I improve it?
    I am looking for some criticism. It was supposed to be my final project and i did it a few weeks early. So my instructor gave me a new "final" project. I am working on it right now and it has to do with arrays. perhaps I am jumping ahead of myself.
    The program meets all the specific requirements. My instructor knows absolutely nothing about electronics. I guess that's why I got a new project.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    For starters, format the code properly. This:

    Code:
        while (( power_supply < 1) || (power_supply > 15)){ /* voltage range*/
        printf ("    Please enter the value for the Power supply between 1 and 15 volts\n");
        scanf ("%lf", &power_supply);
        if (( power_supply < 1) || (power_supply > 15)){    /* error message*/
        printf (" %.1lf volts\n", power_supply); /* so you can see what was entered*/
        printf (" The Input voltage is out of range and could damage the equipment\n\n");
        } }
        /***    the above 7 lines are to make sure the input voltage is within range. If it is not it will loop until an input within range is entered****/
    is a complete mess.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by Data trap View Post
    I guess my question is how can I improve it?
    - horrible indentation
    - not standard conforming "%lf" in printf
    - equal comparing floating point variables
    Salem likes this.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BillyTKid View Post
    - not standard conforming "%lf" in printf
    That does not violate the standard at all; it is 100% legal:

    Quote Originally Posted by C99 draft 7.13.6.1 (The fprintf function), #3
    After the %, the following appear in sequence: [....] an optional l which has no effect on a following a, A, e, E, f, F, g, or G conversion specifier;
    It is just meaningless to the compiler because of automatic type promotion. However, it might indicate something to someone reading the code. It's a (totally harmless) matter of style.
    Last edited by MK27; 10-24-2011 at 01:17 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    In C89 its UB, see K&RII and you see what i mean.
    Therefore its UB and not 'totally harmless'.
    MK27 likes this.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    What is UB?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BillyTKid View Post
    In C89 its UB, see K&RII and you see what i mean.
    Therefore its UB and not 'totally harmless'.
    So your complaint is that it might not work right if you use a 20 year old compiler?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    It stands for Undefined Behaviour.
    Code:
    while(!asleep) {
       sheep++;
    }

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > So your complaint is that it might not work right if you use a 20 year old compiler?
    It's a 20 year old standard, not a 20 year old compiler.

    Even modern compilers implementing the old standard will complain.
    Code:
    $ cat foo.c
    #include<stdio.h>
    int main ( ) {
      double f = 1.23;
      printf("%lf",f);
      return 0;
    }
    $ gcc -Wall -ansi -pedantic foo.c
    foo.c: In function ‘main’:
    foo.c:4: warning: ISO C90 does not support the ‘%lf’ gnu_printf format
    $ gcc -Wall -std=c99 -pedantic foo.c
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem View Post
    Even modern compilers implementing the old standard will complain.
    That's true. I also thought of all the Turbo C people around here and felt a bit silly.

    But really, if I'm going to point out undefined behavior, I try to do so with current standards. It's a bit odd to say "you shouldn't do that because back in the day we didn't do it that way".


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by Salem
    It's a 20 year old standard, not a 20 year old compiler.
    Yeah, though as that version of the standard has been revised by C99, just saying "not standard conforming" without a qualification is rather misleading when it is standard conforming. That said, even when compiling with respect to C99, I'd say that %lf should be avoided since it is equivalent to %f and may be mistaken for %Lf.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Honestly I never understood why you did it one way for reading and another for writing. That always bugged me.


    Quzah.
    iceaway likes this.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    K&R (RIP) have designed C to solve practical problems in 70ies and 80ies not for theoretic discussions in the future.
    Your 'totally harmless' answer is wrong particularly with regard to newbie questions.
    Last edited by BillyTKid; 10-25-2011 at 01:24 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 07:48 PM
  2. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  3. Need some help on writing code
    By A slow C++ user in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2003, 08:32 PM
  4. self-writing code?
    By genghis in forum C++ Programming
    Replies: 19
    Last Post: 01-15-2003, 03:13 PM

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