当前位置:   article > 正文

09 C++ for循环练习_c++for循环题目

c++for循环题目
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4. #include <cctype>
  5. using namespace std;
  6. void forin() {
  7. // 简单例子
  8. cout << "求打印每个元素" << endl;
  9. for (int i = 0; i < 10; i++) {
  10. cout << i << endl;
  11. }
  12. cout << "\n求每个元素乘于2的积" << endl;
  13. int my_array[5] = { 1,2,3,4,5 };
  14. // 不改变数组中的值
  15. // x 将使用 my_array 数组的副本
  16. for (int x : my_array) {
  17. x *= 2;
  18. cout << x << endl;
  19. }
  20. cout << "---" << endl;
  21. for (int i = 0; i < 5; i++) {
  22. cout << my_array[i] << endl;
  23. }
  24. cout << "---" << endl;
  25. // 改变数组中的值
  26. // 符号& 表示 x 是一个引用变量,将使用 my_array 数组的原始数据
  27. for (int& x : my_array) {
  28. x *= 2;
  29. cout << x << endl;
  30. }
  31. cout << "---" << endl;
  32. for (int i = 0; i < 5; i++) {
  33. cout << my_array[i] << endl;
  34. }
  35. cout << "---" << endl;
  36. cout << "\n求每个元素的平方" << endl;
  37. int my_array2[5] = { 6,7,8,9,10 };
  38. for (auto& y : my_array2) {
  39. int z = pow(y,2);
  40. cout << z << endl;
  41. }
  42. cout << my_array2[0] << endl;
  43. cout << "\n使每个元素变为大写" << endl;
  44. string str("some string");
  45. for (auto &x:str)
  46. {
  47. x = toupper(x);
  48. }
  49. cout << str << endl;
  50. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/864360
推荐阅读
相关标签
  

闽ICP备14008679号