Guest User

Untitled

a guest
Sep 24th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. // --- Your code here
  2. #include <iostream>
  3. #include <string>
  4. #include <iomanip>
  5. #include <cmath>
  6.  
  7. // repeatString function
  8. // std::string repeatString(const std::string &some_string, int num){
  9. // for(int i = 1; i < num; i++){
  10. // std::cout << some_string;
  11. // }
  12. // return some_string;
  13. // }
  14.  
  15. std::string repeatString(std::string &some_string, int num){
  16. if (num == 0){
  17. return " ";
  18. }
  19.  
  20. else{
  21. for(int i = 0; i < num-1; i++){
  22. std::cout << some_string;
  23. }
  24. return some_string;
  25. }
  26.  
  27. }
  28.  
  29. //multiply function
  30. long double multiply(const long long unsigned int &large_number, const long long unsigned int &even_larger_number){
  31. long double result = large_number * even_larger_number;
  32. return result;
  33. }
  34.  
  35. // ---
  36. // include the standard library files that defines the std:: functions
  37. int main() {
  38. // --- Your code here
  39.  
  40. double some_number;
  41. std::string some_string;
  42. unsigned long long int large_number;
  43. unsigned long long int even_larger_number;
  44. int negative_number;
  45.  
  46.  
  47.  
  48. // declare the variables used below
  49. some_number = 5.5;
  50. // std::endl and '\n' are equivalent
  51. std::cout << some_number << ' ' << std::round(some_number + M_PI) << '\n';
  52.  
  53. // this one may be a bit tricky since you can't assign a string literal to char* in C++
  54. some_string = "my string";
  55. std::cout << some_string << '\n';
  56.  
  57. // implement this function
  58. // should print empty string
  59. std::cout << repeatString(some_string, 0) << std::endl;
  60. // should print it 5 times, with nothing separating them
  61. std::cout << repeatString(some_string, 5) << std::endl;
  62.  
  63. // there are many numerical types, each with different signedness and size
  64. // declare the appropriate types for them such that none of them overflow
  65. // note that 2^32 ~= 4 billion
  66. large_number = 1000000;
  67. large_number *= 3;
  68. std::cout << large_number << std::endl;
  69.  
  70. even_larger_number = large_number * large_number;
  71. std::cout << even_larger_number << std::endl;
  72.  
  73. // implement this multiply function
  74. std::cout << std::fixed << std::setprecision(1) << multiply(large_number, even_larger_number) << std::endl;
  75. // beware of numeric types' signedness
  76. negative_number = -250;
  77. std::cout << negative_number << std::endl;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment