如何在循环之外设置初始值?C++
how can I set initial values outside of my loop? c++
嗨,我想知道是否有人可以告诉我如何为我的最小值设置初始值,以便它们不会偏向 0 初始值。该程序将查看运行二进制搜索与线性搜索所需的时间比较。这是家庭作业,当我意识到我做了什么时,我正要上交它。 我现在已经彻底搞砸了我的代码试图修复,但我会尝试将其恢复原样。 感谢您给我的任何帮助。
如果需要,将包含更多代码,但不想因为包含太多代码而收到负面评论,但决定这是最好的,然后如果需要,我可以添加更多代码。我敢肯定它是如此明显,比如你应该使用一个while循环或其他东西,但我的大脑很累,今晚我看不到它。所以请不要笑!!再次感谢。
using namespace std;
int main()
{
const int arraySize = 1000;
int numberArray[arraySize];
int searchKey = 0, linearCount = 0, binaryCount = 0,
linearMin = 0, linearMax = 0, linearTotal = 0,
binaryMin = 0, binaryMax = 0, binaryTotal = 0;
double linearAverage = 0 , binaryAverage = 0;
srand (time(NULL));
for (int loopCount = 0; loopCount < arraySize; loopCount++)
{
// gets the random number array populated with 1000 elements
getRandomNumber(numberArray, arraySize);
// gets the search key from one of the random numbers in the array
searchKey = getSearchKey(numberArray, arraySize);
// begins the comparisons for the linear Count
linearCount = doLinearSearch(numberArray, arraySize, searchKey);
// Sort method used was bubbleSort for the requirement of binary search
bubbleSort(numberArray, arraySize);
binaryCount = doBinarySearch(numberArray, arraySize, searchKey);
//linearMin = linearCount; HERE'S WHAT I ORIGINALLY HAD BUT NOW KNOW I GOOFED
/* sets the linear Minimums and Maximums and keeps a running total for average
puts the smallest number of times in linearMin and loops through until
it finds the next smallest if any and assigns it this count number*/
if (linearCount < linearMin) linearMin = linearCount;
if (linearCount > linearMax) linearMax = linearCount;
linearTotal = linearTotal + linearCount;
linearAverage = 1 * linearTotal / arraySize;
// sets the binary Minimums and Maximums and keeps a running total for average
if (binaryCount < binaryMin) binaryMin = binaryCount;
if (binaryCount > binaryMax) binaryMax = binaryCount;
binaryTotal = binaryTotal + binaryCount;
binaryAverage = 1 * binaryTotal / arraySize;
}
// Display results
cout << "After 1000 tests on " << arraySize << " element arrays, n";
"The linear search results were:n";
cout << "The minimum number of comparisons to find the key was: " << linearMin << endl;
cout << "The maximum number of comparisons to find the key was: " << linearMax << endl;
cout << "The average number of comparisons to find the key was: " << linearAverage << endl;
cout << "After 1000 tests on " << arraySize << " element arrays, n";
"The binary search results were:n";
cout << "The minimum number of comparisons to find the key was: " << binaryMin << endl;
cout << "The maximum number of comparisons to find the key was: " << binaryMax << endl;
cout << "The average number of comparisons to find the key was: " << binaryAverage << endl;
}//结束主
有人可以帮我吗
如果我冒犯了这里的任何人,我很抱歉,但是...意识到我在流泪,是的,泪水,因为最初我写了一个正在工作的程序,是的,这是家庭作业。我看到是因为在星期天之前,我正在发表一些最后一分钟的评论,当时我突然想到返回的值不可能是正确的,因为我认为每次都根据循环的最后一次找到 minimumValue。请注意,我在第 2 部分也遇到了两个部分的问题,但我认为至少我可以在第 1 部分进行一些学分,直到我的发现,如果我没有注意到,至少我会比 0 更好。我不希望任何人为我做功课!但是对于所有的论坛,我开始认为谁可以对新手最粗鲁,或者谁可以谈论足够的技术喋喋不休,或者谁可以获得最多的分数并试图给人们留下深刻印象已经变得比向前支付的概念更重要手段。 我相信没有人在乎那里,但截至今天,自周日以来我已经睡了 5 个小时,我碰巧发现一些短信确认我的丈夫(时间不长(再次欺骗我,我已经向很多人寻求帮助,他们后来导致了我的一些工作,至少类似于一个程序,现在看起来弗雷迪克鲁格一直在学习如何写C++。 在这一点上,我很确定我的 3.87 GPA 正在被踢到大厅里,但底线是我是否得到一个奇迹来上交它现在真的无关紧要,因为如果我无法弄清楚,那么我现在应该放弃。大多数作业都是建立在所学知识的基础上的,所以如果我不能做到这一点,我将做什么下一个作业。对不起,但我在这方面有点慢,但当我最终掌握这些概念时,我只需要一些原因和方式有时和现在这篇文章,我只需要一个可能对他们的声誉不那么感兴趣或让我感觉比我已经做的更糟糕的人和一个可以告诉我为什么这不起作用的人。你说的论坛是为了帮助,有人可以帮助我吗?
我应该完成的是这个 用随机数填充 1000 个元素数组 从该数组创建一个搜索关键字并为其分配所述值 做线性搜索并计算完成它所需的#次 按顺序对数组进行排序 对二分搜索做同样的事情 在 main 调用循环中的所有 5 个函数以获取 1000 次迭代的数据 显示循环的结果,并显示最小值比较,每个循环的最大值和平均值
随机生成器和搜索键部分确实单独工作,如果需要,我会发布,但这是搜索和排序的代码
int doLinearSearch(const int randomNumberArray[], const int arraySize, const int key)
{
int index = 0,
position = -1,
ctComparisons = 0;
bool keyfound = false;
while (index < arraySize && !keyfound)
{
if (randomNumberArray[index] == key)
{
keyfound = true;
position = index;
}
index++;
}
ctComparisons = index + 1;
return ctComparisons;
}
void bubbleSort(int randomNumberArray[], const int arraySize)
{
bool swapFlag;
int tempHolder;
do
{
swapFlag = false;
for (int count = 0; count < (arraySize - 1); count++)
{
if (randomNumberArray[count] > randomNumberArray[count + 1])
{
tempHolder = randomNumberArray[count];
randomNumberArray[count] = randomNumberArray[count + 1];
randomNumberArray[count + 1] = tempHolder;
swapFlag = true;
}
}
} while (swapFlag);
}
int doBinarySearch(const int randomNumberArray[], const int arraySize, const int key)
{
int firstElement = 0,
lastElement = arraySize - 1,
middleSearch,
position = -1,
ctComparisons = 0;
bool keyFound = false;
while (!keyFound && firstElement <= lastElement)
{
middleSearch = (firstElement + lastElement) / 2;
if (randomNumberArray[middleSearch] == key)
{
keyFound = true;
position = middleSearch;
}
else if (randomNumberArray[middleSearch] > key)
lastElement = middleSearch -1;
else
{
firstElement = middleSearch + 1;
}
ctComparisons ++;
}
return ctComparisons;
}
我已经尝试了每个人都给出的建议,然后是一些,但这不是我的问题,我并不是说我只是想学习的所有需要或害虫,并且会真正感激,并承诺有一天也会付出代价。
我运行它,它似乎并没有真正将我的比较计数放入除了循环的最后一次迭代之外的任何内容。 我得到的最大值都是全面的。所以你能告诉我是什么原因导致这是一场噩梦吗? 我知道这很简单,可能很可笑,但请.... 如果你愿意,会发布所有这些,我只是觉得我已经被太多的信息大喊大叫了......
您的问题似乎是:如何让我的"最小"值从循环内部检测值。
int array[5] = { 5, 4, 3, 2, 1 };
int minVal = /* what */;
for (size_t i = 0; i < 5; ++i) {
if (array[i] < minVal)
minVal = array[i];
}
// minVal should be 1, not 0.
答案是:将 minVal 设置为>= 您的最大值。如果您知道约束是什么,那就很容易了:
int minVal = LARGEST_POSSIBLE_VALUE;
您可以使用以下语法实现此目的:
unsigned int minVal = ~0;
0 的 1s 补码,这将是所有位设置为 1 时允许的最大值。但是,如果您使用的是已签名的它,它会将其设置为 -1。当然,你可以在那里更具体地做
int minVal = -1;
然后进行检查
if (minVal == -1 || array[i] < minVal)
minVal = array[i];
除非您绝对需要优化额外的测试。
或者你可以使用限制
#include <climits>
int minVal = INT_MAX;
现在,您的 minVal 将尽可能大,直到找到更小的值。由于在循环结束时,可以通过分配或未找到匹配项来INT_MAX它,因此请确保您有某种方法来确定它是哪种情况。
声明范围Static
的变体 ..这样它就不会一次又一次地初始化。.
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 嵌套在类中时无法设置成员数据
- 需要帮助设置在C++中使用的Potrace
- 如何在自删除后将对象设置为nullptr
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何在Ubuntu中使用cmake设置qt4
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 如何在boost beast http请求中设置http头
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 如何在24位SDL_Surface上设置像素的颜色
- std::设置自定义比较器
- 如何设置一个范围来提取我想要获得的信息
- 如何在C/C++中用FD_set Unix设置套接字文件描述符
- 通过选项卡的文本设置QTabWidget顺序
- 将特征矩阵的向量设置为0
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?