# 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. ## 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. And what is your question?

3. 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. 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.

5. Originally Posted by Data trap
I guess my question is how can I improve it?
- horrible indentation
- not standard conforming "%lf" in printf
- equal comparing floating point variables

6. Originally Posted by BillyTKid
- not standard conforming "%lf" in printf
That does not violate the standard at all; it is 100% legal:

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.

7. In C89 its UB, see K&RII and you see what i mean.
Therefore its UB and not 'totally harmless'.

8. What is UB?

9. Originally Posted by BillyTKid
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.

10. It stands for Undefined Behaviour.

11. > 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```

12. Originally Posted by Salem
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.

13. 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.

14. Honestly I never understood why you did it one way for reading and another for writing. That always bugged me.

Quzah.

15. 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.

Page 1 of 2 12 Last