rp.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //Stephen Stengel <stephen.stengel@cwu.edu>
  2. //30 October 2019
  3. //Program to test my russian peasant algorithm
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <limits.h>
  7. #define TRUE 1
  8. #define FALSE 0
  9. #define MAX_TEST_NUM 46340
  10. int rp(int n, int m);
  11. int rpBit(int n, int m);
  12. _Bool isCorrectOutput(int output, int n, int m);
  13. int testFunction();
  14. int main(int argc, char **argv)
  15. {
  16. if (argc != 3)
  17. {
  18. printf("Wrong number of inputs!\n");
  19. return 1;
  20. }
  21. printf("size of int: %lu\n", sizeof(int) );
  22. printf("INT_MAX: %d\n", INT_MAX);
  23. int intn = atoi(argv[1]);
  24. int intm = atoi(argv[2]);
  25. printf("Russian Peasant Test!\n");
  26. printf("Testing n = %d, m = %d\n", intn, intm);
  27. int output = rp(intn, intm);
  28. printf("The output was: %d\n", output);
  29. printf("This output is ");
  30. if ( isCorrectOutput(output, intn, intm) )
  31. {
  32. printf("correct!\n");
  33. }
  34. else
  35. {
  36. printf("WRONG!\n");
  37. }
  38. printf("\n\nNow the bit shift version!\n");
  39. printf("The bitShift version output was: %d\n", rpBit(intn, intm));
  40. printf("\n\nNow for a series of tests.\n");
  41. if ( testFunction() )
  42. {
  43. printf("Test passed!\n");
  44. return 0;
  45. }
  46. else
  47. {
  48. printf("Test FAILED!\n");
  49. return -1;
  50. }
  51. }
  52. //Russian Peasant algorithm using integer division.
  53. int rp(int n, int m)
  54. {
  55. int mTemp = 0;
  56. while (n > 1)
  57. {
  58. if (n % 2 == 1)
  59. {
  60. mTemp += m;
  61. }
  62. n = n / 2;
  63. m = m * 2;
  64. }
  65. return m + mTemp;
  66. }
  67. //Russian Peasant Algorithm using bit shifting
  68. //Compiler probably converts division into shifting automatically so useless.
  69. int rpBit(int n, int m)
  70. {
  71. int mTemp = 0;
  72. while (n > 1)
  73. {
  74. if (n % 2 == 1)
  75. {
  76. mTemp += m;
  77. }
  78. n = n >> 1;
  79. m = m << 1;
  80. }
  81. return m + mTemp;
  82. }
  83. //Checks if the output number is correct.
  84. _Bool isCorrectOutput(int output, int n, int m)
  85. {
  86. if (output == (n * m))
  87. {
  88. return TRUE;
  89. }
  90. else
  91. {
  92. return FALSE;
  93. }
  94. }
  95. //My testing function.
  96. int testFunction()
  97. {
  98. _Bool noErrors = TRUE;
  99. //can go up to 46340 n and m on my computer before rollover.
  100. int n = 1;
  101. int m = 1;
  102. for ( ; (n <= MAX_TEST_NUM) && noErrors; n++)
  103. {
  104. for ( ; (m <= MAX_TEST_NUM) && noErrors; m++)
  105. {
  106. noErrors = isCorrectOutput( rp(n, m), n, m);
  107. }
  108. }
  109. if (noErrors)
  110. {
  111. printf("No errors found!\n");
  112. //printf("final n: %d\tfinal m: %d\n", n, m);
  113. }
  114. else printf("Error found for n: %d and m: %d!\n", n, m);
  115. return noErrors;
  116. }