Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3.  
  4. using namespace std;
  5. int main(){
  6.  
  7. int array[8];
  8. int power = 1;
  9. int powered_num = 1;
  10. int decimal_number = 0;
  11.  
  12. //это цикл, который генерирует двоичное число(массив 1 и 0)
  13. //и превращает его в десятичное число
  14. //00010011 = 19
  15. for (int i = 0; i < 8; i++){
  16. array[i] = rand()%2;
  17. cout << array[i];
  18. //внутренний цикл считает степень, в которую нужно возвести
  19. // i-й эл-т массива
  20. for (int j = 0; j < i; j++){
  21. powered_num *= 2;
  22. }
  23.  
  24. array[i] *= powered_num;
  25. decimal_number += array[i];
  26. powered_num = 1;
  27. }
  28. cout << "\nDecimal number is: " << decimal_number << "\n\n";
  29. decimal_number = 0;
  30.  
  31. //11111100 = 271
  32. for (int i = 0; i < 8; i++){
  33. array[i] = rand()%2;
  34. cout << array[i];
  35. for (int j = 0; j < i; j++){
  36. powered_num *= 2;
  37. }
  38.  
  39. array[i] *= powered_num;
  40. decimal_number += array[i];
  41. powered_num = 1;
  42. }
  43. cout << "\nDecimal number is: " << decimal_number << "\n\n";
  44. decimal_number = 0;
  45.  
  46. //10010101 = 149
  47. for (int i = 0; i < 8; i++){
  48. array[i] = rand()%2;
  49. cout << array[i];
  50. for (int j = 0; j < i; j++){
  51. powered_num *= 2;
  52. }
  53.  
  54. array[i] *= powered_num;
  55. decimal_number += array[i];
  56. powered_num = 1;
  57. }
  58. cout << "\nDecimal number is: " << decimal_number << "\n\n";
  59. }
  60.  
  61.  
  62.  
  63.  
  64. /*
  65. генерациа и вывод бинарного числа
  66. int array[8];
  67. int power = 1;
  68. int decimal_number = 0;
  69.  
  70. for (int i = 0; i < 8; i++){
  71. array[i] = rand()%2;
  72. cout << array[i];
  73. }*/
  74.  
  75.  
  76. // возведение числа(number) в степень(power)
  77. // работает до 2^64 = ~ 1.8446744*10^19
  78. // unsigned long long number = 9;
  79. // unsigned long long power = 13;
  80. // unsigned long long powered_num = 1;
  81.  
  82. // for (int i = 0; i < power; i++){
  83. // powered_num *= number;
  84. // }
  85. // cout << powered_num;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement