请选择 进入手机版 | 继续访问电脑版
楼主: Nicolle
2175 12

Practical C++ [推广有奖]

Lisrelchen 发表于 2016-2-7 05:45:27 |显示全部楼层 |坛友微信交流群
  1. Listing 6.2. A Program That Demonstrates Iterating Through an Array of Five float Values with a Loop

  2. Code View: Scroll / Show All
  3. 1: //////////////////////////////////////////////////////
  4. 2: //  Module  : array2.cpp
  5. 3: //
  6. 4: //  Purpose : Shows the simple usage of loops with a
  7. 5: //            one-dimensional array
  8. 6: //////////////////////////////////////////////////////
  9. 7:
  10. 8: #include <iostream.h>
  11. 9:
  12. 10: void main()
  13. 11: {
  14. 12:    // Create an array of five integers
  15. 13:    //
  16. 14:    float num[5];
  17. 15:
  18. 16:    // Iterate through the array elements, initializing
  19. 17:    // them with float values
  20. 18:    //
  21. 19:    for (int i = 0; i < 5; i++)
  22. 20:       num[i] = i * 2.25f;
  23. 21:
  24. 22:    // Iterate through the array elements,
  25. 23:    //   displaying their values...
  26. 24:
  27. 25:    for (i = 0; i < 5; i++)
  28. 26:       cout << "num[" << i <<"] == " << num[i] << '\n';
  29. 27: }
复制代码

使用道具

Lisrelchen 发表于 2016-2-7 05:48:41 |显示全部楼层 |坛友微信交流群
  1. Listing 6.3. A Program That Demonstrates Creating and Initializing Arrays Simultaneously

  2. Code View: Scroll / Show All
  3. 1: //////////////////////////////////////////////////////
  4. 2: //  Module  : array3.cpp
  5. 3: //
  6. 4: //  Purpose : Shows the declaration and initialization
  7. 5: //            of a one-dimensional array
  8. 6: //////////////////////////////////////////////////////
  9. 7:
  10. 8: #include <iostream.h>
  11. 9: #include <string.h>
  12. 10:
  13. 11: void main()
  14. 12: {
  15. 13:    // Create and initialize two arrays
  16. 14:    //
  17. 15:    float num[5] = { 3.4f, 7.88f, 29.5f, 56.9f, 34.2f };
  18. 16:    char space[] = "I'm spacing out...";
  19. 17:
  20. 18:    // Iterate through the array elements,
  21. 19:    //   displaying their values...
  22. 20:
  23. 21:    for (int i = 0; i < 5; i++)
  24. 22:       cout << "num[" << i <<"] == " << num[i] << '\n';
  25. 23:
  26. 24:    // Space out a little
  27. 25:    //
  28. 26:    cout << '\n' << space << '\n';
  29. 27:
  30. 28:    for (i = 0; i < int(strlen(space)); i++)
  31. 29:       cout << space[i] << " ";
  32. 30: }
复制代码

使用道具

Kurtxie 发表于 2019-6-5 21:21:08 |显示全部楼层 |坛友微信交流群
xiexie.................
daim
m4ed
Yes
If +

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 02:04