Looping Question

This is a discussion on Looping Question within the C++ Programming forums, part of the General Programming Boards category; Hi I am working on a RLC Circuit sim for C++ and I cant get a loop to work, I ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    4

    Looping Question

    Hi

    I am working on a RLC Circuit sim for C++ and I cant get a loop to work, I want to increase frequency by 0.1 , and restart the While loop, until Frequency is less than MaxFreq . Does anyone have any idea on how to implement this???

    Code:
    #include<stdafx.h>
    #include <iostream>
    #include <cmath>
    
    using namespace std; 
    double Resistance;
    double Inductance;
    double Capacitance; 
    double SupplyVoltage;
    double MinFreq;
    double MaxFreq; 
    double Frequency; 
    double Current;
    double Impedance;
    double InductiveReactance;
    double CapacitiveInductance; 
    double InductorVoltage;
    double CapacitorVoltage;
    double ResistorVoltage; 
    double VoltageRatio;
    double NaturalFrequency;
    double Fn; 
    double Pi;
    int menu; 
    
    class RLC
    {
    public :
    
    	void attributes () 
    	{
    		std::cout<<"RLC Series Circuit Simulation \n";
    		std::cout<<"Enter the values for the circuit components \n";
    		std::cout<<"Enter the Resistance [Ohms] ->";
    		std::cin>>Resistance; std::cout<<"\n";
    		std::cout<<"Enter the inducance [H]->";
    		std::cin>>Inductance; std::cout<<"\n";
    		std::cout<<"Enter the Capactiance [F]->";
    		std::cin>>Capacitance; std::cout<<"\n";
    		std::cout<< endl ;
    		std::cout<< "******************************* \n";
    		std::cout<< endl ; 
    		std::cout<< "Enter the operating conditions : \n" ; 
    		std::cout<< "Enter the supply voltage [V] -> " ;
    		std::cin>> SupplyVoltage ; std::cout<<"\n";
    		std::cout<< "Enter the minimum supply frequency [Hz] -> " ; 
    		std::cin>> MinFreq ;std::cout<<"\n";
    		std::cout<< "Enter the Maximum Supply Frequency [Hz] -> " ; 
    		std::cin>> MaxFreq ; std::cout<<"\n";
    	}
    	void NaturalFreq()
    	{
    		Pi=3.141592654;
    		NaturalFrequency=1/(2*Pi*sqrt(Capacitance*Inductance));
    	}
    	void InductiveReactanceXL() 
    	{
    		 InductiveReactance=(2*Pi*Frequency*Inductance);
    	}
    	void CapacitiveInductanceXC()
    	{
    		CapacitiveInductance=1/(2*Pi*Frequency*Capacitance);
    	}
    	void ImpedenceZ()
    	{
    		Impedance=sqrt((Resistance*Resistance)+((InductiveReactance-CapacitiveInductance)*(InductiveReactance-CapacitiveInductance)));
    	}
    	
    	void CurrentI()
    	{
    		Current=SupplyVoltage/Impedance;
    	}
    	void VoltageVR()
    	{
    		ResistorVoltage=Current*Resistance;
    	}
    	void VoltageVC()
    	{
    		CapacitorVoltage=Current*CapacitiveInductance;
    	}
    	void VoltageVL()
    	{
    		InductorVoltage=Current*InductiveReactance;
    	}
    	void VoltageRatioVOVR()
    	{
    		VoltageRatio = ResistorVoltage/SupplyVoltage;
    	}
    	void OutputTest()
    	{
    		std::cout<<"Voltage Ratio is!!:";
    		std::cout<<VoltageRatio ; 
    		std::cout<<"[V]";
    	}
    
    
    
    
    };
    
    int main()
    {
    
    	RLC console;
    	console.attributes();
    
    	RLC Calculations ;
    	
    	Calculations.NaturalFreq ();
    	Frequency = MinFreq ; //Setting Frequency to minimum 
    	while (Frequency <= MaxFreq)
    	{
    	Calculations.InductiveReactanceXL ();
    	Calculations.CapacitiveInductanceXC ();
    	Calculations.ImpedenceZ();
    	Calculations.CurrentI();
    	Calculations.VoltageVR();
    	Calculations.VoltageVC();
    	Calculations.VoltageVL();
    	Calculations.VoltageRatioVOVR();
    	Frequency=Frequency+0.1;
    	}
    
    
    
    	RLC Output;
    	Output.OutputTest();
    
    
    	return 0;
    	}
    Thanks in advance!

    Ewan :-)

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Like this?
    Code:
    for ( ; Frequency < MaxFreq; Frequency += 0.1 )
    {
       // do stuff
    }
    You said you wanted to loop "until" its "less than", but you probably mean you want to "loop while" its less than, right? Also, in your code your using "<=" which isnt "less than". Besides that, my suggestion and your code is equivalent. Because of that, Im wondering what the actual problem is. Is your loop not working properly right now or something?

    Also, I think the common practice is to start (non constant) variables, functions, etc with a lower case letter, so "MaxFreq" would be "maxFreq", etc. But of course that sort of thing is subjective--however there are recommendations and best practices.

    EDIT: Also, when doing the calculations, make sure to force the numbers to be interpreted as doubles, like
    Code:
    NaturalFrequency=1/(2*Pi*sqrt(Capacitance*Inductance));
    would become
    Code:
    NaturalFrequency=(double) 1.0/(2.0*Pi*sqrt(Capacitance*Inductance));
    Last edited by nadroj; 12-21-2009 at 12:59 PM.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by nadroj View Post
    EDIT: Also, when doing the calculations, make sure to force the numbers to be interpreted as doubles, like
    Code:
    NaturalFrequency=1/(2*Pi*sqrt(Capacitance*Inductance));
    would become
    Code:
    NaturalFrequency=(double) 1.0/(2.0*Pi*sqrt(Capacitance*Inductance));
    Strange suggestion, because you seem to be taking advantage of automatic promotion of integers to doubles when they are combined with doubles. But the expression is ALREADY combining with double -- Pi is a double, and sqrt() returns a double. So changing the integral constants to floats is redundant.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    OK! Then he needs to ignore my suggestions.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by nadroj View Post
    OK! Then he needs to ignore my suggestions.
    Well, it's never bad to be specific about types, it's just redundant in this particular case. I'd probably write it the same way. My point is just that the code is correct as currently written.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Gotcha.

    His question just seemed a little strange to me, or at least I felt that I didnt understand fully what he was asking. I think he was asking how to do a loop which he described. However, it seemed that from the code, the required loop was already implemented. So I thought maybe he knows he has written the loop, but for some reason its not "working". This thought popped into my head after I had replied, so I edited it to add that maybe its not "working" because of some (implicit) casting error.

    I was unaware that it "upcasted" the "int"s. I thought the compiler would interpret the entire expression to be the type of the first variable in the expression, so in this case an "int". But I guess doing this would give a "possible loss of precision" warning/error (depending on compiler flags, I believe) when later downcasting the floats to int. Anyways, good to know, thanks.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You're not getting any useful replies because none of us understand your question.
    A class isn't doing anything useful in that code, and having a ton of globals is just a mess.

    You don't seem to understand Object Oriented Programming, so you may as well just program procedurally.
    Come to think of it though, it almost look like you'd you're progamming as if you're using a functional programming language, or even a relational one such as Prolog.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    4
    Quote Originally Posted by iMalc View Post
    You're not getting any useful replies because none of us understand your question.
    A class isn't doing anything useful in that code, and having a ton of globals is just a mess.

    You don't seem to understand Object Oriented Programming, so you may as well just program procedurally.
    Come to think of it though, it almost look like you'd you're progamming as if you're using a functional programming language, or even a relational one such as Prolog.
    I am aware of the class, but for some reason the assignment specifies that you must use only a single class for everything I dont know why, so I just did that to keep the lecturer happy !!!

    I am used to using Matlab before C++ hence why I am used to having it set up like a functional layout.

    The concept that I do not understand is how to set up the loop so it repeats itself until Frequency is less than Max Frequency . When the statement becomes false, the program should output some data and then terminate.

  9. #9
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    It's bugging me to death and irrelevant but if you have "using namespace std;" then you don't need the "std::" before calling functions like cout.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by ewandougie View Post
    I am aware of the class, but for some reason the assignment specifies that you must use only a single class for everything I dont know why, so I just did that to keep the lecturer happy !!!
    But you're not using it for everything. You've got a lot of globals outside of that class. Because you don't see the point of it, you're using it in a pointless way. If you understood why it was useful then you would most likely be making good use of it.

    I am used to using Matlab before C++ hence why I am used to having it set up like a functional layout.
    Yes but that wont help you in C++. You're just writing out equations. In C++ your program doesn't just say what a function is, it must say how to calculate something. Variables are not just things that need to exist in order for you to write out an equation. Whilst your program may calculate some things correctly, it's like pushing a round peg through a square hole. Sure if you make your peg out of rubber then you can probably force it through, but that's just not the sensible thing to do. You need to learn how to write a proper C++ program so that you can make a square peg that'll slide through easily.

    The concept that I do not understand is how to set up the loop so it repeats itself until Frequency is less than Max Frequency . When the statement becomes false, the program should output some data and then terminate.
    But you're starting at min frequency. Surely that is already less than max frequency! Why would it need to loop at all then?
    It would be like me asking you to come closer until you can read this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 05:52 PM
  2. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  3. Looping question
    By sparkyf in forum C Programming
    Replies: 2
    Last Post: 10-25-2003, 04:21 PM
  4. Not the normal looping question...
    By JKI in forum C++ Programming
    Replies: 12
    Last Post: 10-18-2003, 07:02 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM

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