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

📄 soln4_1.cpp

📁 Wrox.Ivor.Hortons.Beginning.Visual.C.Plus.Plus.2008 With sourcecode
💻 CPP
字号:
// Soln4_1.cpp
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;


int main()
{
  int arraySize = 5;
  double* values = new double[arraySize];    // Initial array to store values
  double* temp = 0;                          // Temporary store for pointer to new array
  double inputValue = 0.0;                   // Current input value
  int index = 0;                             // Index to the values array
  for(;;)
  {
    // Read the next value
    cout << "Enter a value or 0 to end: ";
    cin >> inputValue;

    // If it is 0 we are done so exit the loop
    if(inputValue == 0.0)
      break;

    values[index++] = inputValue;            // Store the value and increment index

    if(index == arraySize)                   // If index reaches arraySize
    {                                        // We need a bigger array...
      arraySize += 5;                        // Increase the array size value
      temp = new double[arraySize];          // Allocate the new array

      for(int i = 0 ; i<index ; i++)         // Copy old array elements to new
        temp[i] = values[i];

      delete[] values;                       // Delete the old array
      values = temp;                         // Store address of new array
      temp = 0;                              // Reset temp to null
    }
  }
  // Calcuate the average and output the values
  double average = 0.0;                      // Store for the average
  for(int i = 0 ; i<index ; i++)
  {
    average += values[i];                    // Add value
    cout << setw(10) << values[i];           // Output current value
    if((i+1)%5 == 0)                         // If it's multiple of 5
      cout << endl;                          // start a new line
  }
  cout << endl                               // Output the average
       << "Average is " << average/index
       << endl;
  delete[] values;                           // Release memory for array

  return 0;
}

⌨️ 快捷键说明

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