fork download
  1. //parallel resistor calculator : input is target value (float)
  2. //this calculator can search a 100 combination up to 10 of linked-resistors.
  3. //output is combination (int array)
  4. //variation of PRECISION can make a combinations more wide.
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9. #define PRECISION 0.5
  10. #define DECIMAL_PLACE 2 //round decimal place
  11. #define RLIST 6
  12.  
  13. int* assignRarr(int* Rlist, int numofRlist, int const* Rarr){
  14. for(int i=0; i<numofRlist; i++)
  15. Rlist[i]=Rarr[i];
  16.  
  17. return Rlist;
  18. }
  19.  
  20. double isEqual(int* Rarr, int numR, double target){
  21.  
  22. double result=0;
  23. double inverseSum=0;
  24.  
  25. for(int i=0; i<numR; i++){
  26. inverseSum+=1/(double)Rarr[i];
  27.  
  28. }
  29.  
  30. result=1/inverseSum;
  31. int compvalue=fabs(result-target)<=PRECISION; //this is a default option, epslion comparison
  32.  
  33. if (compvalue){
  34. return result;
  35. }
  36. else {
  37. return 0; //if 0->result, you can search a all combination.
  38. }
  39. }
  40.  
  41. int biSearchR(int combination[][10], int const* Rlist, int* selectedRarr, double* valuelist, int iter,
  42. double target, int option, int numofRlist){
  43. option = option > 0 ? 0 : target; //range mode
  44. for(int p = option; p < target + 1; p++) { //if p==target, not search a range.
  45. for (int i = 0; i < numofRlist - 1; i++) {
  46. selectedRarr[0] = Rlist[i];
  47. for (int j = i + 1; j < numofRlist; j++) {
  48. selectedRarr[1] = Rlist[j];
  49. double value = isEqual(selectedRarr, 2, p);
  50. if (value) {
  51. for (int q = 0; q < 2; q++)
  52. combination[iter][q] = selectedRarr[q];
  53. valuelist[iter] = value;
  54. iter++;
  55. }
  56. }
  57. }
  58. }
  59. return iter;
  60. }
  61.  
  62. int triSearchR(int combination[][10], int const* Rlist, int* selectedRarr, double* valuelist, int
  63. iter, double target, int option, int numofRlist){
  64. option = option > 0 ? 0 : target; //range mode
  65. for(int p = option; p < target + 1; p++){
  66. for(int i=0; i<numofRlist-2; i++){
  67. selectedRarr[0]=Rlist[i];
  68. for(int j=i+1; j<numofRlist-1; j++){
  69. selectedRarr[1]=Rlist[j];
  70. for(int k=j+1; k<numofRlist; k++){
  71. selectedRarr[2]=Rlist[k];
  72. double value = isEqual(selectedRarr, 3, p); //p <--> target
  73. if(value){
  74. for(int q=0; q<3; q++)
  75. combination[iter][q]=selectedRarr[q];
  76. valuelist[iter]=value;
  77. iter++;
  78. }
  79. }
  80. }
  81. }
  82. }
  83. return iter;
  84. }
  85.  
  86. int quadSearchR(int combination[][10], int const* Rlist, int* selectedRarr, double* valuelist, int
  87. iter, double target, int option, int numofRlist){
  88. option = option > 0 ? 0 : target; //range mode
  89. for(int p = option; p < target + 1; p++) { //if p==target, not search a range.
  90. for (int i = 0; i < numofRlist - 3; i++) {
  91. selectedRarr[0] = Rlist[i];
  92. for (int j = i + 1; j < numofRlist - 2; j++) {
  93. selectedRarr[1] = Rlist[j];
  94. for (int k = j + 1; k < numofRlist - 1; k++) {
  95. selectedRarr[2] = Rlist[k];
  96. for (int l = k + 1; l < numofRlist; l++) {
  97. selectedRarr[3] = Rlist[l];
  98. double value= isEqual(selectedRarr, 4, p);
  99. if (value) {
  100. for(int q=0; q<4; q++)
  101. combination[iter][q]=selectedRarr[q];
  102. valuelist[iter]=value;
  103. iter++;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. return iter;
  111. }
  112.  
  113. void printListedR(int* Rarr, int numofRlist){
  114. printf("( ");
  115. if (numofRlist<10){
  116. for(int i=0; i<numofRlist-1; i++)
  117. printf("%d Ω, ", Rarr[i]);
  118. } else {
  119. for(int i=0; i<5; i++)
  120. printf("%d Ω, ", Rarr[i]);
  121. printf("..., ");
  122. }
  123. printf("%d Ω ).\n\n", Rarr[numofRlist-1]);
  124. }
  125.  
  126. int printCombination(int (*combination)[10], int numofcombination, double* result){
  127. if(!numofcombination){
  128. printf("None");
  129. return 0;
  130. }
  131.  
  132. for(int i=0; i<numofcombination; i++){
  133. int j=0;
  134.  
  135. printf("Resistors Combination %d : ", i+1);
  136. while (combination[i][j]){
  137. printf("%d Ω", combination[i][j]);
  138. j++;
  139.  
  140. if(!combination[i][j])
  141. break;
  142.  
  143. printf(" || ");
  144. }
  145. printf(" = %.3f Ω", result[i]);
  146. puts("");
  147. }
  148. return 1;
  149. }
  150.  
  151. int printRatioCombination(int (*combination)[10], int numofcombination){
  152. if(!numofcombination){
  153. return 0;
  154. }
  155.  
  156. double newRlist[100][10]={0};
  157. int roundvalue= pow(10, DECIMAL_PLACE);
  158.  
  159. for(int i=0; i<numofcombination; i++){
  160. int j=0;
  161. while (combination[i][j]){
  162. newRlist[i][j]=(double)combination[i][j]/combination[i][0];
  163. j++;
  164. }
  165. }
  166.  
  167. for(int i=0; i<numofcombination; i++){
  168. int j=0;
  169.  
  170. printf("Resistors' ratio %d : ", i+1);
  171. while (newRlist[i][j]){
  172. printf("%.2f", round(roundvalue*newRlist[i][j])/roundvalue);
  173. j++;
  174.  
  175. if(!newRlist[i][j])
  176. break;
  177.  
  178. printf(" || ");
  179. }
  180. puts("");
  181. }
  182.  
  183. return 1;
  184. }
  185.  
  186. int main(void) {
  187. int numofRlist=RLIST;
  188. int* Rlist=(int*) malloc(sizeof(int)*numofRlist);
  189. int selectedRarr[10]={0};
  190. int combination[100][10]={0};
  191. double valuelist[100]={0};
  192. int iter=0;
  193. double target;
  194.  
  195. int Rarr[RLIST]={820, 1000, 2200, 3300, 4700, 5600}; //resistors list, you can modify it.
  196. assignRarr(Rlist, numofRlist, Rarr);
  197.  
  198. puts("Welcome to parallel resistor combination calculator.\n");
  199. puts("This calculator can search combinations of resistors from : ");
  200. printListedR(Rlist, numofRlist);
  201. puts("target value? (without unit)");
  202. scanf("%lf", &target);
  203. printf("\ntarget value : %.3f Ω\n", target);
  204.  
  205.  
  206. iter = biSearchR(combination, Rlist, selectedRarr, valuelist, iter, target, 0, numofRlist);
  207. iter = triSearchR(combination, Rlist, selectedRarr, valuelist, iter, target, 0, numofRlist);
  208. //iter = quadSearchR(combination, Rlist, selectedRarr, valuelist, iter, target, 0, numofRlist);
  209.  
  210. printCombination(combination, iter, valuelist);
  211. //printRatioCombination(combination, iter);
  212.  
  213. free(Rlist);
  214. return 0;
  215. }
  216.  
Success #stdin #stdout 0.01s 5288KB
stdin
825
stdout
Welcome to parallel resistor combination calculator.

This calculator can search combinations of resistors from : 
( 820 Ω, 1000 Ω, 2200 Ω, 3300 Ω, 4700 Ω, 5600 Ω ).

target value? (without unit)

target value : 825.000 Ω
Resistors Combination 1 : 1000 Ω || 4700 Ω = 824.561 Ω