gpt4 book ai didi

c++ - 前或后测试循环?

转载 作者:行者123 更新时间:2023-11-30 01:56:10 28 4
gpt4 key购买 nike

<分区>

我正在尝试确定前测试循环还是后测试循环是最佳方法,以便用户可以继续输入将要搜索的值,直到输入标记值以结束程序。另外,我的循环参数是什么样的?这是我的代码,我需要包含循环。另外,我知道至少执行一次后测试循环。提前致谢!

#include<iostream>
using namespace std;

int searchList( int[], int, int); // function prototype
const int SIZE = 8;

int main()
{
int nums[SIZE]={3, 6, -19, 5, 5, 0, -2, 99};
int found;
int num;

// The loop would be here
cout << "Enter a number to search for:" << endl;
cin >> num;

found = searchList(nums, SIZE, num);
if (found == -1)
cout << "The number " << num
<< " was not found in the list" << endl;
else
cout << "The number " << num <<" is in the " << found + 1
<< " position of the list" << endl;

return 0;

}


int searchList( int List[], int numElems, int value)
{
for (int count = 0;count <= numElems; count++)
{
if (List[count] == value)
// each array entry is checked to see if it contains
// the desired value.
return count;
// if the desired value is found, the array subscript
// count is returned to indicate the location in the array
}
return -1; // if the value is not found, -1 is returned
}

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com