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

📄 pr0614.cpp

📁 practice c++, it is from the book http://www.amazon.com/Schaums-Outline-Programming-John-Hubbard
💻 CPP
字号:
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill, 2000
//  Problem 6.14 on page 144
//  Removing elements from an array

#include <iostream>  // defines the cout object
using namespace std;

void removeAll(float a[], int& n, float x);
void print(float a[], int n);

int main()
{ // tests the removeFirst() function:
  float a[] = { 6.6, 5.5, 3.3, 7.7, 5.5, 2.2, 5.5, 4.4 };
  int size=8;
  print(a,size);
  cout << "removeAll(a,size,5.5):" << endl;
  removeAll(a,size,5.5);
  print(a,size);
  cout << "removeAll(a,size,2.2):" << endl;
  removeAll(a,size,2.2);
  print(a,size);
  cout << "removeAll(a,size,5.5):" << endl;
  removeAll(a,size,5.5);
  print(a,size);
}

void print(float a[], int n)
{ for (int i=0; i<n-1; i++)
    cout << a[i] << ", ";
  cout << a[n-1] << endl;
}

void removeAll(float a[], int& n, float x)
{ for (int i=0; i<n; i++)
    if (a[i] == x)
    { for (int j=i+1; j<n; j++)
        a[j-1] = a[j];
      --n;
    }
}

⌨️ 快捷键说明

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