473,508 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help understand probems - Binary Search and Sequenital Search

The bigger problem is with the Binary Search. The program crashes when it's
excuted. and Visual Studio 2005 indicates stack over flow and shows a break
at that function.

Sequential search is working, but I am trying to have it display the number
of comparisons it took to find the number. It keeps displaying "2."
Thank you folks kindly,

T

#include <iostream>
#include <fstream>
#include<iomanip>
#include<cstdlib>

using namespace std;

ifstream inFile;
ofstream outFile;

void SequentialSearch(int isorted[],int size, int target, int &scompares,
bool &found);
void insertionSort(int isort[], int isorted[],int size);
void BinarySearch(int isorted[],int &compares, int first, int last, bool
&found, int target);
int main()
{
const int arraySize = 15;
int unSort[arraySize];
int sorted[arraySize];
int count=0;
int targets[6];
int compareB;
int compareS;
bool findS;
bool findB;
int firstN = 0;
int lastN = arraySize-1;

inFile.open("input3.txt"); //opening input file
if (!inFile)
{
cerr << "Error Opening File" << endl;
system("pause");
EXIT_FAILURE; //stop program on failure
}

outFile.open("output.txt"); //opening input file
if (!outFile)
{
cerr << "Error Opening File" << endl;
system("pause");
EXIT_FAILURE; //stop program on failure
}
for (int i =0;i < arraySize;i++) //Read in the file with the unsorted
numbers
{
inFile >unSort[i];

}

for (int j=0;j< 6;j++) // get the search keys
{
inFile >targets[j];
}
insertionSort(unSort, sorted, arraySize); //call the insertion sort the here

for (int y=0;y < 6; y++)
{
SequentialSearch(sorted, arraySize, targets[y], compareS, findS);
// void SequentialSearch(int isorted[],int size, int target, int
&scompares, bool &found);
BinarySearch(sorted, compareB, firstN, lastN, findB, targets[y]);
// void BinarySearch(int isorted[],int &compares, int first, int last,
int &found, int target);

cout << setw(15)<< "Unsorted Array" << setw(15) << "Sorted Array" <<
endl;
for (int z=0; z< arraySize; z++){
cout << setw(15) << unSort[z] << setw(15)<< sorted[z] << endl;

}
cout << endl << "Target: " << targets[y] << endl;

cout << endl << "Binary Search Found: ";
if (findB == true)
cout << compareB;
else
cout << " Nothing" << endl;

cout << endl << "Sequential search Found: ";
if (findS == true)
cout << compareS << endl;
else cout << " Nothing" << endl << endl;
}
system("pause");

return EXIT_SUCCESS;
}

//insertion sort starts here
void insertionSort(int isort[], int isorted[],int size)
{
int i,j;
bool Done;

for (int k=0; k < size; k++) isorted[k] = isort[k]; // copy numArray(main)
into isort1

for(i=1;i<size;i++)
{

j=i;
Done = false;
while(j>=1 && !Done)

{

if (isorted[j-1] isorted[j])
swap(isorted[j-1], isorted[j]);
else
Done = true;

j-- ;
}
// end of WHILE loop

} // end of FOR loop
} // end of insertion sort
void SequentialSearch(int isorted[],int size, int target, int &scompares,
bool &found)

{
scompares = 0;
int index;
if (size ==0)
found = false;

else
scompares = 1;
index = 0;

while ((index <size -1) && (isorted[index] != target))
index++;
scompares++;

if (isorted[index] == target)
found = true;
else
found = false;

}
void BinarySearch(int isorted[],int &compares, int first, int last, bool
&found, int target)
{
compares=0;
int middle;
if(first last)
found = false;

else
compares++;
middle = (first + last)/2;

if (middle == target)
found = true;

else if (middle < target)
BinarySearch(isorted,compares, middle++ , last, found,
target);

else
BinarySearch(isorted,compares, first, middle--, found,
target);

}// end of Binary Search
/*void print(int isort[], int isorted[], int size, int count, int bfound,
int sfound, int target)
{
outFile << " Unsorted List Insertion Sorted" << endl;

for (int k =0;k < size;k++)
outFile << setw(15) <<isort[k] << setw(16) << << setw(20) << isorted[k]
<< endl;
outFile << endl << "Target: " << setw(23) << target << endl;
outFile << "Binary Search Found: " << found << endl;
outFile << "Sequential Search Found: " <<

}// end of print function */
Jul 7 '07 #1
2 2522
On 2007-07-07 04:32, Timmy wrote:
The bigger problem is with the Binary Search. The program crashes when it's
excuted. and Visual Studio 2005 indicates stack over flow and shows a break
at that function.

Sequential search is working, but I am trying to have it display the number
of comparisons it took to find the number. It keeps displaying "2."
Try stepping through the code to find out what is going on.

--
Erik Wikström
Jul 7 '07 #2

"Erik Wikström" <Er***********@telia.comwrote in message
news:Lv****************@newsb.telia.net...
On 2007-07-07 04:32, Timmy wrote:
>The bigger problem is with the Binary Search. The program crashes when
it's excuted. and Visual Studio 2005 indicates stack over flow and shows
a break at that function.

Sequential search is working, but I am trying to have it display the
number of comparisons it took to find the number. It keeps displaying
"2."

Try stepping through the code to find out what is going on.

--
Erik Wikström
thanks for the input

T
Jul 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2722
by: kazack | last post by:
I posted a similiar question in this newsgroup already and got an answer which I already knew but didn't get the answer I was looking for so I am reposting the code and question differently in the...
4
1963
by: Vinc_get31 | last post by:
Hi everyone! I have 2 matrices : The 1 is smaller than the 2. I would like to select in the 2nd only lines which correspond to couples of 1st... Is that possible? And how? However, I need a...
12
2295
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful....
4
9002
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
0
1294
by: J. | last post by:
Hello all, I need some assistance. I've been out of the C++ game way too long and I need some help getting back up to speed. I'm taking a class where STL is mostly covered...I know alot of...
36
3434
by: felixnielsen | last post by:
What i really wanna do, is defining my own types, it doesnt really matter why. Anyway, i have run into some problems 1) typedef unsigned short U16; U16 test = 0xffffffff; // There should be a...
66
5279
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
4
1896
memoman
by: memoman | last post by:
Can any body help me in that program ??? mail me if anybody could reach any -helpfull- thing Write a C++ program that namely insert, delete, and search in a fixed record length file (containing...
3
1478
by: juan brown | last post by:
I am new to usenet, need help on how to use it.
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.