⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex5_12.cpp

📁 Visual C++ 2005的源代码
💻 CPP
字号:
// Ex5_12.cpp
// Returning a reference
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;

double& lowest(double values[], int length); // Prototype of function
                                             // returning a reference

int main(void)
{
   double array[] = { 3.0, 10.0, 1.5, 15.0, 2.7, 23.0,
                      4.5, 12.0, 6.8, 13.5, 2.1, 14.0 };
   int len = sizeof array/sizeof array[0];   // Initialize to number
                                             // of elements

   cout << endl;
   for(int i = 0; i < len; i++)
      cout << setw(6) << array[i];

   lowest(array, len) = 6.9;                 // Change lowest to 6.9
   lowest(array, len) = 7.9;                 // Change lowest to 7.9

   cout << endl;
   for(int i = 0; i < len; i++)
      cout << setw(6) << array[i];

   cout << endl;
   return 0;
}

double& lowest(double a[], int len)
{
   int j = 0;                                // Index of lowest element
   for(int i = 1; i < len; i++)
      if(a[j] > a[i])                        // Test for a lower value...
         j = i;                              // ...if so update j
   return a[j];                              // Return reference to lowest 
                                             // element
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -