Permuttations

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2006
Posts: 1
Reputation: Tkassis is an unknown quantity at this point 
Solved Threads: 0
Tkassis's Avatar
Tkassis Tkassis is offline Offline
Newbie Poster

Permuttations

 
0
  #1
Dec 7th, 2006
I am working on an exercise given in class. I have managed to do most of it but I a a bit stuck on the permutation part. Here is what is required of me:

Subject: Transformers

An ideal electrical transformer has an input voltage and current, Vin, Iin and an output
voltage and current, Vout, Iout. The relationship between inputs and outputs is
Vout =nVin, Iout =Iin /n Where n is the transformer’s turns ratio.

If two transformers are cascaded then they “ideally” behave as an equivalent transformer with n=n1 X n2

However, in practice each transformer has maximum values of Iin and Iout, above which
the transformer burns out.

An input file named cascade.dat in the following format is to be read
n1 Iin1max Iout1max

n2 Iin3max Iout2max

n3 Iin3max Iout3max

… etc


Each triplet of floats specifies a transformer. These transformers are to be cascaded, not
necessarily in the order of the file.

The number of transformers is unknown until the file is read. Note that the turns ratio of the single transformer equivalent to the cascade of all transformers doesn’t depend upon the cascading order.

Write a program to determine the maximum input current that can be applied to the
cascade without any transformer burning out if the transformer ordering can be arbitrary.


Note. This program is not required to be very efficient. A simple consideration of all
possible orderings of the transformers is sufficient.

Although not really necessary here, good OOP principles must be adopted – who knows
where this code may be reused.



To deal with the more general case you will need to be able to “loop through” all possible
permutations of the transformer order. How does one generate all possible orderings? –
not covered in any modules I expect. Well there are simple algorithms “out there” and in
this world of the internet, you need to get used to finding these sorts of “useful
algorithms”.


and here is the code I have so far:

  1.  
  2. #include <iostream.h>
  3. #include <fstream.h>
  4.  
  5. //define the transformer as a class
  6. class transformer
  7. {
  8. public:
  9. float iout; //output current in amps
  10. float n; //transformer's turns ratio
  11. float iinmax; //max input current before the transformer burns out in amps
  12. float ioutmax; //max output current before the transformer burns out in amps
  13. //declare constructor
  14. transformer();
  15. //delcare destructor
  16. ~transformer();
  17. };
  18. //define constructor
  19. transformer::transformer()
  20. {
  21.  
  22. iout = 0.;
  23. n = 0.;
  24. iinmax = 0.;
  25. ioutmax = 0.;
  26. }
  27.  
  28. //main program
  29. void main()
  30. {
  31. int nu = 0; //variable to hold the number of lines and thus transformers
  32. char buf[256]; //buffer of 256 charecters used to count the number of lines
  33. transformer *p; //pointer of type tranformer
  34. float turns_ratio = 1.; //turns ratio of cascade
  35. float ioutcasc, iincasc;
  36.  
  37. ifstream infile; //input file stream
  38. infile.open("cascade1.dat");
  39.  
  40. //check if file was opened successfully
  41. if (infile.fail())
  42. {
  43. cout<<"Failed to open file"<<endl;
  44. }
  45. else
  46. // cout<<"File opened successfully\n";
  47. //read the contents of the file until the end
  48. while (!infile.eof())
  49. {
  50.  
  51. infile.getline(buf,256);
  52. nu ++;
  53. }
  54. //close the file
  55. infile.close();
  56.  
  57. //The number of transformers is 1- nu
  58. nu = nu-1;
  59.  
  60. //display the number of transformers
  61. cout<<"There are "<<nu<<" transformers\n";
  62.  
  63. //create array of tranformers
  64. p = new transformer[nu];
  65. //open the file again to read the values
  66.  
  67. infile.open("cascade1.dat");
  68.  
  69. //check if file was opened successfully
  70. if (infile.fail())
  71. {
  72. cout<<"Failed to open file"<<endl;
  73. }
  74. else
  75. // cout<<"File opened successfully\n";
  76.  
  77. //assign values from the file into the corresponding tranformers
  78. for(int i=0;i<nu;i++)
  79. {
  80. infile>>p[i].n>>p[i].iinmax>>p[i].ioutmax;
  81. }
  82. //close file
  83. infile.close();
  84. //calculate and display the turns ratio of the cascade
  85. for(int k=0;k<nu;k++)
  86. {
  87. turns_ratio *= p[k].n;
  88. }
  89. cout<<"The turns ratio of the cascade is "<<turns_ratio<<"\n";
  90.  
  91. }

Now, I tried very hard to find a some kind of algorithm that I can use for permuting the sequence of cascades, but I can never understand how they work inorder to modify them to my design.

Any suggestions on how the calculation and permutation can be carried out?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC