I'm creating a code where...
- Numbers from a text file are being read into an array
- User can search for a number and the program tells you if it's in the text file.
- Numbers are sorted and output into another file
- I do a binary search from the sorted data files from 3
Issues I'm Facing...
- When I search for '0' is works. But if it's ANY other number it comes back not found.
- When outputting numbers in another file, a random huge number is added to the list. (i.e. 10 9 8 7 6 5 4 3 2 1 -8723641187 (-8723641187 is suppose to be 0))
I have 4 text files with 10 small digits each. 3 of them have 0's in different sections. One of them doesn't have any 0's.
Code (#define len 10 where suited):
function # 1
void simpleSearch(std::string inputFile, int value)
{
bool found = false ;
int num,
count = 0 ;
std::ifstream file(inputFile.c_str()) ;
while(!file.eof())
{
file >> num ;
count++ ;
if (num == value)
{
std::cout << "Target Is Found\n" ;
found = true ;
break ;
}
}
if(found == false)
{
std::cout << "Target Not Found\n" ;
}
}
function #2
void sorting(std::string inputFile, std::string outputFile)
{
int num,
first,
temp,
i = 0,
size = len + 1,
contents[len + 1];
std::ifstream file(inputFile.c_str()) ;
while(!file.eof())
{
file >> num ;
contents[i++] = num ;
}
for (i = size - 1; i > 0; i--)
{
first = 0 ;
for(int j = 1; j <= i; j++)
{
if(contents[j] < contents[first])
{
first = j ;
}
}
temp = contents[first] ;
contents[first] = contents[i] ;
contents[i] = temp ;
}
std::ofstream outFile(outputFile.c_str()) ;
for(i = 0; i < len + 1 ; i++)
{
outFile << contents[i] << " " ;
}
std::cout << "Sorted Values Stored In '" << outputFile << "'\n" ;
return ;
}
function #3
int binarySearch(std::string inputFile, int value)
{
int middle,
num,
array[len],
first = 0,
size = len + 1,
last = size - 1,
position = - 1 ;
bool found = false ;
std::ifstream file(inputFile.c_str()) ;
while(!file.eof())
{
file >> num ;
size++ ;
middle = (first + last) / 2 ;
if(array[middle] == value)
{
found = true ;
position = middle ;
}
else if (array[middle] > value)
{
last = middle - 1 ;
}
else
{
first = middle + 1 ;
}
}
if(position == value)
{
std::cout << inputFile + ": Target Is Found\n" ;
found = true ;
}
else if (position == -1)
{
std::cout << inputFile + ": Target Is Not Found\n" ;
}
return position ;
}
main
int main()
{
int target,
num = 0,
count = 0;
std::string outputFile ;
std::ofstream file1("random.txt") ;
for(int i = 0; i < len + 1; i++)
{
file1 << (i + 1) << " " ;
}
file1.close() ;
std::ofstream file2("early.txt") ;
std::ofstream file3("middle.txt") ;
std::ofstream file4("end.txt") ;
std::ifstream file("random.txt") ;
file2 << num << " " ;
while(!file.eof())
{
file >> num ;
if (count == len / 2)
{
file3 << "0" << " " ;
}
file2 << num << " " ;
file3 << num << " " ;
file4 << num << " " ;
count++ ;
}
file4 << "0" << " ";
file.close() ;
std::cout << "1. Reading Value From File\n" ;
std::cout << "----------------------------\n" ;
std::cout << " Testing the files have been read by testing if\n" ;
std::cout << " the listed files have '0'\n\n" ;
simpleSearch("random.txt", 0 ) ;
simpleSearch("early.txt", 0 ) ;
simpleSearch("middle.txt", 0) ;
simpleSearch("end.txt", 0) ;
std::cout << "\n2. Simple Search\n" ;
std::cout << "----------------------------\n" ;
std::cout << "Enter a Target Number to Find: \n" ;
std::cin >> target ;
simpleSearch("random.txt", target ) ;
simpleSearch("early.txt", target) ;
simpleSearch("middle.txt", target) ;
simpleSearch("end.txt", target) ;
std::cout << "\n3. Sorting\n" ;
std::cout << "----------------------------\n" ;
std::cout << "Enter a File Name to Store Your Numbers: \n" ;
std::cin >> outputFile ;
sorting("random.txt", outputFile) ;
std::cout << "\nEnter a File Name to Store Your Numbers: \n" ;
std::cin >> outputFile ;
sorting("early.txt", outputFile) ;
std::cout << "\nEnter a File Name to Store Your Numbers: \n" ;
std::cin >> outputFile ;
sorting("middle.txt", outputFile) ;
std::cout << "\nEnter a File Name to Store Your Numbers: \n" ;
std::cin >> outputFile ;
sorting("end.txt", outputFile) ;
std::cout << "\n4. Binary Search\n" ;
std::cout << "----------------------------\n" ;
std::cout << " Testing the files with binary search\n" ;
std::cout << "Enter a Target Number to Find: \n" ;
std::cin >> target ;
binarySearch("random.txt", target) ;
binarySearch("early.txt", target) ;
binarySearch("middle.txt", target) ;
binarySearch("end.txt", target) ;
return 0 ;
}
Feel like I'm so close...then anything I've been wrong before. Thank you.