fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int e_resistance(float orig_resistance, float *res_array);
  5. float getClosestValue(float ohm);
  6. float calc_resistance (int count, char conn, float *array);
  7.  
  8. int main () {
  9.  
  10. float voltage;
  11. printf("Power in V: " );
  12. scanf("%f", &voltage);
  13.  
  14. int no_of_components;
  15. printf("No of components: ");
  16. scanf("%d", &no_of_components);
  17.  
  18. //Allocate dynamic space for an array
  19. float *array;
  20. array = malloc(no_of_components * sizeof(float));
  21.  
  22. int i = 0; //counter for each component
  23. float ohmsize;
  24.  
  25. while(i < no_of_components){
  26. printf("Component %d in ohm: ", i+1);
  27. scanf("%f", &ohmsize);
  28.  
  29. if (!(ohmsize > 0.0001)){
  30. continue;
  31. }
  32. else{
  33. array[i] = ohmsize;
  34. i++;
  35. }
  36. }
  37.  
  38. char input;
  39. int valid_input = 0;
  40.  
  41. while(valid_input == 0){
  42. printf("Connection serial or parallel [S|P]: ");
  43. scanf("%c", &input);
  44. if((input=='S')||(input=='P')||(input=='s')||(input=='p')){
  45. valid_input = 1;
  46. }
  47. }
  48.  
  49. float res = calc_resistance(no_of_components, input, array);
  50.  
  51. printf("Resplacing resistor: %.0fohm\n", res);
  52. float *p_array; //pointer
  53. p_array = malloc(sizeof(float)*3); //arrayen, 3 element
  54.  
  55. //call function that returns no of needed replacement resistors
  56. int x = e_resistance(res, p_array);
  57.  
  58. printf("New resistor 1: %f\n", p_array[0]);
  59. if (x>1) printf("New resistor 2: %f\n", p_array[1]);
  60. if (x>2) printf("New resistor 3: %f\n", p_array[2]);
  61.  
  62. printf("No of new resistors: %d", x);
  63.  
  64. return 0;
  65. }
  66.  
  67. //********************************************************************
  68.  
  69. /*function to replace resistor-values with the closest one
  70. from the e12-series, saved hard-coded above */
  71. int e_resistance(float orig_resistance, float *res_array){
  72.  
  73. float r1 = 0;
  74. float r2 = 0;
  75. float r3 = 0;
  76.  
  77. float tmp;
  78.  
  79. int counter = 0;
  80. if(orig_resistance > 0.0001){
  81. r1 = getClosestValue(orig_resistance);
  82. res_array[0] = r1;
  83. if(r1 > 0.0001){
  84. counter = counter + 1;
  85. }
  86.  
  87. //difference saved in tmp
  88. tmp = orig_resistance - r1;
  89.  
  90. if(tmp > 0.0001){
  91. r2 = getClosestValue(tmp);
  92. res_array[1] = r2;
  93. if(r2 > 0.0001){
  94. counter = counter + 1;
  95. }
  96. }
  97.  
  98. tmp = tmp - r2;
  99.  
  100. if(tmp > 0.0001){
  101. r3 = getClosestValue(tmp);
  102. res_array[2] = r3;
  103. if(r3 > 0.0001){
  104. counter = counter + 1;
  105. }
  106. }
  107. }
  108. //printf("%d\n", counter);
  109.  
  110. return counter; //this counter is usually returned as "0", even though the counter above is for example "2"
  111. }
  112.  
  113. //*******************************************************************
  114.  
  115. //function to return the closest value in the e12-serie to the incoming parameter
  116. float getClosestValue(float ohm){
  117. const float tolerance=1.5;
  118.  
  119. float e12[] = {10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82, 100, 120, 150, 180, 220, 270, 330, 390, 470, 560, 680, 820, 1000, 1200, 1500, 1800, 2200, 2700, 3300, 3900, 4700, 5600, 6800, 8200, 10000, 12000, 15000, 18000, 22000, 27000, 33000, 39000, 47000, 56000, 68000, 82000, 100000, 120000, 150000, 180000, 220000, 270000, 330000, 390000, 470000, 560000, 680000, 820000, 1000000};
  120.  
  121. int i;
  122.  
  123. for(i=0; i<(sizeof(e12)/sizeof(e12[0])); i++){
  124. if(ohm+tolerance > e12[i] && ohm-tolerance < e12[i])
  125. return e12[i];
  126. else if (e12[i] > ohm)
  127. return i?e12[i-1]:0;
  128. }
  129. return 0;
  130. }
  131.  
  132. //**********************************************************************
  133.  
  134. float calc_resistance (int count, char conn, float *array)
  135. {
  136. float res = 0; //resistance
  137. int i = 0; //counter variable
  138.  
  139. if (count > 0)
  140. {
  141. switch(conn)
  142. {
  143. //serial
  144. case 's':
  145. case 'S': {
  146. for (; i < count; i++)
  147. {
  148. res += *(array+i);
  149. }
  150. }
  151. break;
  152.  
  153. //parallel
  154. case 'p':
  155. case 'P': {
  156. for (; i < count; i++)
  157. {
  158. res += 1.0/ *(array+i);
  159. //printf(" %f\n", r);
  160. }
  161.  
  162. res = 1/res;
  163. }
  164. break;
  165.  
  166. default:
  167. return -1;
  168. break;
  169. }
  170.  
  171. return res;
  172. }
  173.  
  174. else
  175. {
  176. return -1;
  177. }
  178. }
  179.  
Success #stdin #stdout 0s 2428KB
stdin
5
3
18
22
220
s
stdout
Power in V: No of components: Component 1 in ohm: Component 2 in ohm: Component 3 in ohm: Connection serial or parallel [S|P]: Connection serial or parallel [S|P]: Resplacing resistor: 260ohm
New resistor 1: 220.000000
New resistor 2: 39.000000
No of new resistors: 2