const int DECLARED_SIZE = 20;
void fillArray(int a[], int size, int& numberUsed) {
cout << "Enter up to " << size << " nonnegative whole numbers.\n"
<< "Mark the end of the list with a negative number.\n";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a[index] = next;
index++;
cin >> next;
}
numberUsed = index;
}
int search(const int a[], int numberUsed, int target)
{
int index = 0;
bool found = false;
while ((!found) && (index < numberUsed))
if (target == a[index])
found = true;
else
index++;
if (found)
return index;
else
return -1;
}
由於某種原因它始終輸出0。我怎樣才能創建一個函數,在這個代碼中的數組中輸出0的數量?
int findZero(const int a[], int target, int numberUsed, int zeroes)
{
for (int index = 0; index < numberUsed; index++)
{
if (target == a[index])
zeroes++;
}
return zeroes;
}
這是主碼。
int main()
{
int arr[DECLARED_SIZE], listSize, target;
貌似後大多代碼 請添加更多細節
fillArray(arr, DECLARED_SIZE, listSize);
char ans;
int result;
int zeroes;
int numberUsed;
do
{
cout << "Enter a number to search for: ";
cin >> target;
int numberOfZeroes = findZero(arr, target, zeroes, numberUsed);
cout << "The amount of zero = " << zeroes << endl;
還有更多的代碼,但我只包含這部分,我需要輸出0的量在陣列。
你知道爲什麼我最後的功能不斷輸出0嗎?我試圖讓它計數並加總陣列中的總量0。太令人沮喪了 – kdasdaskl