C++顺序搜索没有找到最后一个元素

C++顺序搜索没有找到最后一个元素

问题描述:

所以我有一个输入文件。它由40个数字组成。前20个数字被输入到一个数组中(我已经检查过,他们实际上在那里)。然后关闭并重新打开输入文件。我使用顺序搜索将输入文件中的前20个数字与我的数组进行比较。这意味着他们都应该成功。然后,我将下面的20个数字与我数组中的数字进行比较,它们都应该是不成功的搜索。我的数组在这一点上是未排序的。C++顺序搜索没有找到最后一个元素

我遇到的问题是,成功的最后一个号码永远不会使用顺序找到。我不知道如何解决这个问题。

这里是顺序搜索功能:

length = 19; 

void Search::sequential(ItemType item, bool& found) 
{ 
    int place = 0; 
    while (place < length && item != list[place]) 
    place++; 
    found = (place < length); 
} 

这里是我的成功/失败的循环

outFile << "\n\n ************Sequential Successful ********** \n"; 
outFile << endl << "ID" << endl; 

inFile >> num; 
for(int i=0; i<=length && inFile; i++) 
{ 
    search.sequential(num, found); 
    if (found) 
    outFile << num << endl; 

    inFile >> num; 
} 


//sequential unsuccessful 
outFile << "\n\n ************Sequential unsuccessful ********** \n"; 
outFile << endl << "ID" << endl; 

for(int i=0; i<=length && inFile; i++) 
{ 
    search.sequential(num, found); 
    if (!found) 
    outFile << num << endl; 

    inFile >> num; 
} 

然而,我的输出是:

************Sequential Successful ********** 

ID 
1111 
3352 
4567 
5678 
6789 
7890 
8901 
9012 
1223 
2113 
8546 
2374 
4723 
9573 
3284 
7474 
8594 
3589 
5858 
//THERE SHOULD BE 1925 HERE BUT THERE ISN'T 

    ************Sequential unsuccessful ********** 

ID 
9456 
3584 
2222 
4319 
4477 
5710 
5497 
1502 
1599 
1504 
1506 
9943 
8833 
9944 
6678 
5555 
5660 
9911 
6130 
1613 

如果我删除“如果(找到)”声明一切正常,但我如何解决这个问题而不删除?

在此先感谢

---------------编辑---------------

好吧,当我改变长度到20它似乎仍然没有工作。我很迷茫。

这里就是我创建阵列

inFile >> num; 
for (int i=0; i<length && inFile; i++) 
{ 
    search.addToList(num); 
    inFile >> num; 
} 

这里是addToList功能

void Search::addToList(ItemType num) 
{ 
    if (index < length) //ive tried taking out this statement just to see if it makes a difference and it didn't 
    { 
    list[index] = num; 
    index++; 
    } 
} 

我在构造函数初始化索引0

这是我的声明数组

ItemType list[length]; 

IT WORKS !!!!非常感谢你们!我非常感激。

+1

你打印你数组不正确:在当前形式中,您正在打印实际数组末尾的元素。我通常使用`for(int i = 0; i Lars 2010-12-09 16:42:58

+0

是的,这表明我的数组保存了正确的值,谢谢。 – 2010-12-09 16:55:24

有2级的解决方案: 长度应该得到20值

length = 20; 

使用 “< =” 代替 “<”(在这种情况下 “长度” 应被命名为 “lastIndex的” )

void Search::sequential(ItemType item, bool& found) 
{ 
    int index = 0; 
    while (index <= length && item != list[index]) 
    index++; 
    found = (index <= length); 
} 

看看你的搜索功能,当你试图找到第20个号码时,索引有什么价值?

如果你有20个数字,那么你为什么将长度设置为19?这是非常直观的。

经典Off-By-One问题。请参阅@ Kipotlov的代码更正答案。

索引顺序搜索有效使用C

这个代码适用于所有的情况下,即如果我们在一个阵列 这个代码将工作找到最后一个元素...

#include<stdio.h> 
void main() 
{ 
    int d[100],kin[20],pin[20],temp,k,i,j=0,n,n1=0,start,end; 
    printf("Enter the number of elements:"); 
    scanf("%d",&n); 
    for(i=0;i<n;i++) 
    scanf("%d",&d[i]); 
    printf("Enter the number to be searched:"); 
    scanf("%d",&k); 
    for(i=0;i<n;i+=3) 
    { 
    kin[n1]=d[i]; 
    pin[n1]=i; 
    n1++; 
    } 
    if(k < kin[0]) 
    { 
    printf("element not found"); 
    exit(0); 
    } 
    else 
    { 
    for(i=1;i<=n1;i++) 
     if(k < kin[i]) 
     { 
     start=pin[i-1]; 
     end=pin[i]; 
     break; 
     } 
     else 
     { 
     start=n1; 
     end=n-1; 
     } 
    } 
    for(i=start;i<=end;i++) 
    { 
    if(k==d[i]) 
    { 
     j=1; 
     break; 
    } 
    } 
    if(j==1) 
    printf("element found at position %d",i); 
    else 
    printf("element not found"); 
}