(c++) Modify your program so that the data is entered from a file rather than fr
ID: 3683248 • Letter: #
Question
(c++) Modify your program so that the data is entered from a file rather than from the keyboard. The first line of the file should be the size of the integer array. The second line should contain the integer searched for in the data set. Finally, the array elements are to start on the third line. Make sure you separate each array element with a space. The output, as described in iii), should be sent to a file.
I don't know how to do this, can someone please help! Please if you can also, explain how to do this.
Here is my code based upon the assignment (Write a program that prompts the user to enter the number of elements and the numbers themselves to be placed in an integer array that holds a maximum of 50 elements. The program should then prompt the user for an integer which will be searched for in the array using a binary search.)
#include
#include
using namespace std;
void input(int[], int);
void print(int[], int, string);
void bubbleSortArray(int[], int);
int binarySearch(int[], int, int);
double mean(int[], int);
int main()
{
int location,
num,
array[50],
total,
value;
cout << "How many numbers do you have? (You may only have a max of 50 numbers)";
cin >> num;
while (num > 50) //while loop to run when the number of elements added is over 50
{
cout << "Sorry, but the max input is 50 numbers, please try again. ";
cout << "How many numbers do you have? ";
cin >> num;
}
input(array, num);
print(array, num, "unsorted");
bubbleSortArray(array, num);
print(array, num, "sorted");
cout << "The mean of the numbers in your array is " << mean(array, num) << endl;
cout << "Enter number to search for: ";
cin >> value;
location = binarySearch(array, num, value);
if (location < 0)
cout << value << " is not in the array ";
else
cout << value << " is at array location " << location << endl;
system("pause");
return 0;
}
void input(int array[], int num)
{
int i;
for (i = 0; i < num; i++)
{
cout << "Enter element " << i + 1 << ": ";
cin >> array[i];
}
}
void print(int array[], int n, string list)
{
int i;
cout << "The " << n << " elements in your " << list << " array are: ";
for (i = 0; i < n; i++)
{
cout << array[i] << " ";
if ((i + 1) % 10 == 0)
cout << endl;
}
cout << endl;
}
void bubbleSortArray(int array[], int num)
{
bool swap;
int temp;
int bottom = num - 1; // bottom indicates the end part of the
// array where the largest values have
// settled in order
do
{
swap = false;
for (int count = 0; count < bottom; count++)
{
if (array[count] < array[count + 1])
{ // the next three lines do a swap
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true; // indicates that a swap occurred
}
}
bottom--; // bottom is decremented by 1 since each pass through
// the array adds one more value that is set in order
} while (swap != false);
//loop repeats until a pass through the array with
// no swaps occurs
}
int binarySearch(int array[], int numElems, int value) //function heading
{
int first = 0; // First element of list
int last = numElems - 1; // last element of the list
int middle; // variable containing the current
// middle value of the list
while (first <= last)
{
middle = first + (last - first) / 2;
if (array[middle] == value)
return middle; // if value is in the middle, we are done
else if (array[middle] < value)
last = middle - 1; // toss out the second remaining half of
// the array and search the first
else
first = middle + 1; // toss out the first remaining half of
// the array and search the second
}
return -1; // indicates that value is not in the array
}
double mean(int array[], int total)
{
int i = 0;
int sum = 0;
for (i = 0; i < total; i++)
sum += array[i];
return (double)sum / total;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void input(int[], int, ifstream&);
void print(int[], int, string, ofstream&);
void bubbleSortArray(int[], int);
int binarySearch(int[], int, int);
double mean(int[], int);
int main()
{
int location,
num,
array[50],
total,
value;
ifstream infile;
ofstream outfile;
string fileinput, fileoutput;
cout<<"Enter name of the input file: ";
cin>>fileinput;
infile.open(fileinput.c_str());
if (!infile.is_open()) {
cerr<<"cannot open this file";
}
cout<<"Enter name of the output file: ";
cin>>fileoutput;
outfile.open(fileoutput.c_str());
if (!outfile.is_open()) {
cerr<<"Not able to open this file";
outfile.close();
return 0;
}
infile >> num;
infile >> value;
while (num > 50) //while loop to run when the number of elements added is over 50
{
cout << "Sorry, but the max input is 50 numbers, please try again. ";
return 0;
}
input(array, num, infile);
print(array, num, "unsorted", outfile);
bubbleSortArray(array, num);
print(array, num, "sorted", outfile);
outfile << "The mean of the numbers in your array is " << mean(array, num) << endl;
location = binarySearch(array, num, value);
if (location < 0)
outfile << value << " is not in the array ";
else
outfile << value << " is at array location " << location << endl;
// system("pause");
return 0;
}
void input(int array[], int num, ifstream& infile)
{
int i;
for (i = 0; i < num; i++)
infile >> array[i];
}
void print(int array[], int n, string list, ofstream& outfile)
{
int i;
outfile << "The " << n << " elements in your " << list << " array are: ";
for (i = 0; i < n; i++)
{
outfile << array[i] << " ";
if ((i + 1) % 10 == 0)
outfile << endl;
}
outfile << endl;
}
void bubbleSortArray(int array[], int num)
{
bool swap;
int temp;
int bottom = num - 1; // bottom indicates the end part of the
// array where the largest values have
// settled in order
do
{
swap = false;
for (int count = 0; count < bottom; count++)
{
if (array[count] < array[count + 1])
{ // the next three lines do a swap
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true; // indicates that a swap occurred
}
}
bottom--; // bottom is decremented by 1 since each pass through
// the array adds one more value that is set in order
} while (swap != false);
//loop repeats until a pass through the array with
// no swaps occurs
}
int binarySearch(int array[], int numElems, int value) //function heading
{
int first = 0; // First element of list
int last = numElems - 1; // last element of the list
int middle; // variable containing the current
// middle value of the list
while (first <= last)
{
middle = first + (last - first) / 2;
if (array[middle] == value)
return middle; // if value is in the middle, we are done
else if (array[middle] < value)
last = middle - 1; // toss out the second remaining half of
// the array and search the first
else
first = middle + 1; // toss out the first remaining half of
// the array and search the second
}
return -1; // indicates that value is not in the array
}
double mean(int array[], int total)
{
int i = 0;
int sum = 0;
for (i = 0; i < total; i++)
sum += array[i];
return (double)sum / total;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.