fork download
  1. #include <stdio.h>
  2. int e_resistance(float orig_resistance, float *res_array);
  3. float getClosestValue(float ohm);
  4.  
  5. int main(void) {
  6.  
  7. int i;
  8. float ohm = 1239;
  9.  
  10. float *p_array;
  11. float r_array[3] = { };
  12.  
  13. p_array = r_array;
  14.  
  15. printf("Integer returned: %d\n ", e_resistance(ohm, p_array));
  16.  
  17. for (i = 0; i < 3; i++){
  18. printf("Ohm-value %d: %f\n ", i+1, p_array[i]);
  19. }
  20. return 0;
  21.  
  22. }
  23.  
  24. //********************************************************************
  25.  
  26. /*function to replace resistor-values with the closest one
  27. from the e12-series, saved hard-coded above */
  28. int e_resistance(float orig_resistance, float *res_array){
  29.  
  30. float r1 = 0;
  31. float r2 = 0;
  32. float r3 = 0;
  33.  
  34. float tmp;
  35.  
  36. int counter = 0;
  37. if(orig_resistance > 0){
  38. r1 = getClosestValue(orig_resistance);
  39. res_array[0] = r1;
  40. if(r1 > 0){
  41. counter = counter + 1;
  42. }
  43.  
  44. //difference saved in tmp
  45. tmp = orig_resistance - r1;
  46.  
  47. if(tmp != 0){
  48. r2 = getClosestValue(tmp);
  49. res_array[1] = r2;
  50. if(r2 > 0){
  51. counter = counter + 1;
  52. }
  53. }
  54.  
  55. tmp = tmp - r2;
  56.  
  57. if(tmp !=0){
  58. r3 = getClosestValue(tmp);
  59. res_array[2] = r3;
  60. if(r3 > 0){
  61. counter = counter + 1;
  62. }
  63. }
  64. }
  65. printf("Printing just to see the value of counter in e_resistance: %d\n", counter);
  66.  
  67. return counter; //this counter is usually returned as "0", even though the counter above is for example "2"
  68. }
  69.  
  70. //*******************************************************************
  71.  
  72. //function to return the closest value in the e12-serie to the incoming parameter
  73. float getClosestValue(float ohm){
  74.  
  75. 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};
  76.  
  77. int i;
  78.  
  79. for(i=0; i<(sizeof(e12)/sizeof(e12)[0]); i++){
  80. if(ohm == e12[i])
  81. return e12[i];
  82. else if (e12[i] > ohm)
  83. return e12[i-1];
  84. }
  85. return 0;
  86. }
  87.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
Printing just to see the value of counter in e_resistance: 2
Integer returned: 2
 Ohm-value 1: 1200.000000
 Ohm-value 2: 39.000000
 Ohm-value 3: 0.000000