### diodecalculations I am new to C++ and have this program that needs to solve the equation print the results all in a nice way to read it. I got it running but I am not able to make it make the calculation of the equation and print the result.

#include <iostream>
#include <cmath>
#include <iterator> // std::advance

using namespace std;
/****************************************************************************/
int main(int argc, char**argv)
{
double i, C;
int e;

cout << "Please enter the value for i: " ; //prints sentence on screen
cin >> i; //value entry

cout << "Please enter the value for C: " ; //prints sentence on screen
cin >> C; //value entry

const float Is = 1.22478e-8;
const float q = 1.6 * e-19 * C;
const float N = 1.83369;
const float k = 1.38e-23;
const float T = 293.15 * k;

for(int i = 0 ;i < 31;i++ ){

float Vd  = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f,
1.0f, 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f, 1.7f, 1.8f, 1.9f,
2.0f, 2.1f, 2.2f, 2.3f, 2.4f, 2.5f, 2.6f, 2.7f, 2.8f, 2.9f,
3.0f};

float id;
id = Is * (e, exp (q * Vd[i]) / (N * k * T));
cout << "*.*.*.*.*.*.*.*.*.*.*.*.*.*" << endl;

//cout<< Vd[i] << " "<< endl;
cout<< "Voltage VS = " << Vd[i] << "\t\t" <<"current ID = " <<endl;
cout<< "Voltage is : " << 'id' <<endl; //prints sentence on screen

}
cout << "*.*.*.*.*.*.*.*.*.*.*.*.*.*" << endl;

return 0; // successful EXIT status
}

this is it
Last edited on Please add [code][/code] tags to your code (the <> format on the right).

> id = Is * (e, exp (q * Vd[i]) / (N * k * T));
What's the comma after the first e supposed to do? You need some loops for iterating over your Vd array. Therefore you could use at your example program the range based for loop:
 ``1234`` ``````for (auto v : Vd) // semantic: 'for each v of Vd' { ... // here you can use your v }`````` the coma after the e is a subsitute to a multiplication sign. I know this is not valid but it is not working and gives me error with the multiplication sign. Hello denwisi,

 PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code. It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/ Hint: You can edit your post, highlight your code and press the <> formatting button. You can use the preview button at the bottom to see how it looks. I found the second link to be the most help.

Your program has both small and big problems.

In the line `int main(int argc, char**argv)` "argc" and argv" are not used in the program right now. if you do not need them `int main()` will work fine. If you have some later use you can leave them.

All your variables defined as "float" would be better as a "double". The warnings I received when I compiled the program were about storing a higher precision "double" into a lesser precision "float" and that there could be data loss.

In the line `const double q = 1.6 * e - 19 * C;` "1.6" is considered a "double" and "C" is defined as a "double", so by the time it is finished you are trying to store a "double" in a "float".

Another problem that has two parts is "e". You define "e" as an int, but do not initialize the variable or ever give it a value. On my computer this is usually "-858,993,460", without the commas, which is whatever garbage was left in memory when "e" was assigned that memory address. This was an error until I initialized the variable "e". The second problem is in `id = Is * (e * exp(q * Vd[i]) / (N * k * T));`. Since I initialized the variable "e" to zero the equation becomes `id = Is * (0 * exp(q * Vd[i]) / (N * k * T));`, so anything times zero is zero. Also at this point the variable "e" is promoted to a "double" for the calculation, so might as well make it a "double" to begin with.

In the following output you can see the values of "i" and "C". I gave "e" a value of 4 to see what would happen because I have no idea what "e" is for or means or what value it should have.

In the line `cout<< "Voltage is : " << 'id' <<endl; //prints sentence on screen `. The use of single quotes are for a single character. When there are two or more characters double quotes are used, but I am guessing here that you want no quotes and to use the variable (id).

With "e" having a value of four this is what your program produced:
 ``` Please enter the value for i: 1.5 Please enter the value for C: 2.5 *.*.*.*.*.*.*.*.*.*.*.*.*.* Voltage VS = 0.00 current ID = Voltage is : 478568477951220708289158855184613376.00 *.*.*.*.*.*.*.*.*.*.*.*.*.* Voltage VS = 0.10 current ID = Voltage is : 7852243681614681939291546106986496.00 *.*.*.*.*.*.*.*.*.*.*.*.*.* Voltage VS = 0.20 current ID = Voltage is : 128837843853439756497919236112384.00 *.*.*.*.*.*.*.*.*.*.*.*.*.* Voltage VS = 0.30 current ID = ```

Include the header file "iomanip" and put this line before your two "cout" statements: `std::cout << std::fixed << std::showpoint << std::setprecision(2);`. The number "2" in setprecision is the number of digits to the right of the decimal point. You can change that to whatever you need.

If you want scientific leave out the "fixed" part or change it to 'scientific".

I do not believe that your program is complete and I am having a hard time trying to figure out what you want. It would help with some example values for "i", "C" and "e" along with the output that you expect. For those of us less learned about electronics or electrical engineering it helps.

Hope that helps,

Andy @denwisi,
I don't think you understand the problem - and you need to do that before you start coding.

I think your equation should read
 ID=Is { exp[ q Vd / (N k T) ] - 1 }

ID is your diode current.
Is is the saturation current for that type of junction
q is the charge on an electron (1.6e-19 coulomb) - C is just standing for the unit of charge; it is NOT a separate computational variable as you appear to think.
Vd is the voltage across the junction
N is an emission coefficient; your value is OK.
k is Boltzmann's constant (1.38e-23 joules per kelvin)
T is temperature (here 293.15 kelvin; about 20 celsius) - K is just standing for a unit of temperature; it is NOT a separate computational variable as you appear to think.

 ``12`` ``````double i, C; int e;``````

You need precisely none of those.

 ``12345`` ``````cout << "Please enter the value for i: " ; //prints sentence on screen cin >> i; //value entry cout << "Please enter the value for C: " ; //prints sentence on screen cin >> C; //value entry ``````

C is just plain daft - see above. It is more likely that you would have wanted Is than i. However, since you specify Is as a constant anyway, neither input is required.

Your table of voltages should be defined before the loop.

`id = Is * (e, exp (q * Vd[i]) / (N * k * T));`
You appear not to understand that ex is the same as exp(x).

I think your diode equation should also have a -1 in. Otherwise, a zero voltage would drive a current, which defies the laws of physics.

`cout << "*.*.*.*.*.*.*.*.*.*.*.*.*.*" << endl;`
Cut it out and keep it simple.

`#include <iterator> // std::advance `
I haven't a clue why you think you need that line.

@Handy Andy's comments are also good: you don't need argc and argv here, and it would be better (given the sort of numbers that you are using here) to use doubles rather than floats.

Fundamentally, you need to understand your problem before you start coding it (especially if you are an electrical engineering or physics student). For the diode equation see:
https://www.allaboutcircuits.com/worksheets/pn-junctions/

Last edited on
Topic archived. No new replies allowed.