The program will open a file, read the numbers from the file into the array, sort the numbers into ascending order, and use a binary search to see if a value is in the array.Parameters and return types are given and must be followed.
Functions:
The first function will read the file into the array. The function has two parameters, a double array and the size of the array, and returns nothing. Remember that arrays pass by reference and therefore no return value is necessary. In the function:
Create a file stream object
Open the file to read.
Test that the file opened,
if the file failed to open
output a message to the user
pause the program
exit the program using the exit() function using a return value of 1,
otherwise,
using a loop read each value in the file into the elements in the array.
Close the file.
The second function sorts the array. Sort the values into ascending order. You may copy the code for this function from the bubble sort lab or from Chapter 8. You will need to change data types from int to double for the array. The function has 2 parameters, the array and the size of the array and returns nothing.
The third function uses a binary search to determine if a user entered a value that is in the array. It returns the position of the value if it is found and -1 if it is not found. Again, the code for this is in Chapter 8. Be sure you write your code to work with an array of type double.
The last function does output to the screen. You will need to set a field width of 8 and set the precision right of the decimal to 2. Your output should look like that given below with 6 values per line. The function has 2 parameters, the array, and the size of the array. It returns nothing.
Main Line
The main function has the following variables,
a constant integer for the size of the array,
an array of data type double with 100 elements,
a variable of data type double that will hold the value for which the user wants to search.
a variable of data type char that will be used to determine if the user wants to search for another value.
an integer to hold the position of a value found in the array.
main() calls the first two functions described above.
Now start a loop.
Ask the user to input a search value.
Call the binary search function and assign the returned value to your integer variable. Send the array, the size of the array, and the variable with the search value as arguments.
if the value returned is -1 output a message to the user that the value is not in the array.
else output a message to the user that includes the value and the position at which it was found.
ask the user if they would like to search for another value and get their input
continue to loop when the user enters 'y' or 'Y'.
End of Loop
Call the function to output the contents of the array as shown below.
The file contains in order from highest to lowest:
--------------------------------------------------
1.70 7.97 8.89 13.14 13.54 17.69
20.56 22.12 29.27 35.89 39.05 41.21
58.49 69.06 70.34 73.81 80.62 83.61
89.42 92.13 93.60 95.35 98.44 98.80
106.08 112.82 113.46 115.73 117.10 120.36
124.48 125.68 127.23 129.33 129.65 133.22
133.67 134.06 135.05 136.66 138.07 139.36
147.38 151.18 151.44 152.33 161.22 166.57
167.16 168.73 170.15 173.08 174.30 176.49
178.37 180.92 187.57 191.14 193.08 196.38
198.76 205.08 205.87 214.42 216.61 229.83
230.53 231.23 232.16 235.34 235.71 236.24
240.46 245.70 252.74 255.83 257.84 261.31
263.55 268.78 272.20 272.66 273.10 274.09
277.80 279.81 280.38 284.58 292.55 292.74
293.11 296.31 303.95 305.12 305.69 308.65
313.74 315.30 321.50 326.04
Press any key to continue . . .
Here is what I have so far- I am working on the loop in the first function now.
The input file is given to us
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int SIZE = 0;
double elements[100];
double search;
char searchAgain;
int valuePosition;
//call the first two functions
readFile;
system("pause");
return 0;
}
int readFile(double elements[100] , const int SIZE)
{
fstream filestr;
filestr.open("mrfile.txt", fstream::in | fstream::out | fstream::app);
if(!filestr)
{
cout << "Error Opening File" << endl;
system("pause");
exit(0);
}
filestr.close();
//returns nothing
}
int sortArray()
{
//The second function sorts the array. Sort the values into ascending order.
//two parameters, array and size of the array
//returns nothing.
}
int search()
{
//uses a binary search to determine if a user entered a value that is int he array. It returns the
//position of the value if it is found and -1 if it is not found.
}
int screenOutput()
{
//set field width of 8 and set precision right of the decimal to 2.
//Output should look like that given below with 6 values per line. Function has two
//parameters, array, and size of the array.
//return nothing
}
|