In: Computer Science
In C++
First create the txt file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it
Create this text file: data1.txt
-59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42
Main
#include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; const int MAXSIZE = 100; // Prototypes int main() { int nums[MAXSIZE]; int searchFor; int indexFound = -1; int numElems; double average; string fileName; char again; do { cout << "Enter the file name: "; cin >> fileName; // Call the function fillArray. It has the fileName, the nums array and // the numElem passed in (in that order). It will calculate the numElems // Description of function given below // Call the function printArray. It has the nums array and // the numElem passed in (in that order). // Description of function given below // Call the function findAverage. It has the nums array and // the numElem passed in (in that order). It stores the value // that is returned in the average variable declared above. // Description of function given below // Asks the user what number they want to search for cout << endl << endl; cout << "Enter a number between -100 and 100 to search for: "; cin >> searchFor; // Call the function findValue. It has the number being searched for // the nums array and the numElem passed in (in that order). // It stores the index of the position in the array where the // number was found in the indexFound variable declared above. // Description of function given below // Right the if statement to print whether the number was found. // If it was found, it will print the inde of where it was found. // (See output for what should be printed cout.setf(ios:: fixed); cout.precision(2); cout << "The average of all the numbers in the array is " << average << endl; cout << endl; cout << "Do you want to do this again? (Y/N): "; cin >> again; } while (toupper (again) == 'Y'); return 0; } // Function: findValue // This function has the value being serachedd for, the array and the number of // elements passed in. I searches the array and when it first finds it, it // stops searching and returns the index of where it was found. If it is not // in the array, it returns a -1 // Function: findAverage // This function has the array and the number of elements passed in. // It computes the average of the numbers in the array and returns it. // Function: printArray // This function has the array and the number of elements passed in. // It prints the array in neat columns, with 7 numbers per line // Function: fillArray // This function should open the file with the name that passed into it. If the file does // not open correctly it should exit the program. It should // then read in the numbers and load them into the array. // make sure you check that you don't exceed the array size. // If the file has too many numbers, your program should not put the // extra numbers in the array, the array will just be full. // This function determines the number of elements in the array. // This function should not call any other user defined functions.
Sample Output
Enter the file name: data.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 Enter a number between -100 and 100 to search for: 62 62 was found in index 8 The average of all the numbers in the array is 13.83 Do you want to do this again? (Y/N): Y Enter the file name: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 61 -9 95 42 -73 -64 91 -96 2 53 -8 82 -79 16 18 -5 -53 26 71 38 -31 12 -33 -1 -65 -6 3 -89 22 33 -27 -36 41 11 -47 -32 47 -56 -38 57 -63 -41 23 41 29 78 16 -65 90 -58 -12 6 -60 42 -36 -52 -54 -95 -10 29 70 50 -94 1 93 48 -71 -77 -16 54 56 -60 66 76 31 8 44 -61 -74 23 37 38 18 -18 29 41 Enter a number between -100 and 100 to search for: 52 The number 52 was not found in the array The average of all the numbers in the array is 1.48 Do you want to do this again? (Y/N): y Enter the file name: data.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 Enter a number between -100 and 100 to search for: 95 95 was found in index 16 The average of all the numbers in the array is 13.83 Do you want to do this again? (Y/N): n
Program:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int MAXSIZE = 100;
// Prototypes
void fillArray(string fileName, int nums[], int *numElems);
void printArray(int nums[], int numElems);
double findAverage(int nums[], int numElems);
int findValue(int nums[], int numElems, int value);
int main()
{
int nums[MAXSIZE];
int searchFor;
int indexFound = -1;
int numElems = 0;
double average;
string fileName;
char again;
do
{
cout << "Enter the
file name: ";
cin >>
fileName;
fillArray(fileName,
nums, &numElems);
printArray(nums, numElems);
average = findAverage(nums,
numElems);
// Asks the user what
number they want to search for
cout << endl
<< endl;
cout << "Enter a
number between -100 and 100 to search for: ";
cin >>
searchFor;
indexFound = findValue(nums,
numElems, searchFor);
if(indexFound != -1)
cout <<
searchFor << " was found in index " << indexFound
<< endl;
else
cout <<
"The number " << searchFor << " was not found in the
array " << endl;
cout.setf(ios::
fixed);
cout.precision(2);
cout << "The
average of all the numbers in the array is " << average
<< endl;
cout <<
endl;
cout << "Do you
want to do this again? (Y/N): ";
cin >>
again;
} while (toupper (again) == 'Y');
return 0;
}
void fillArray(string fileName, int nums[], int *numElems)
{
FILE* fp = fopen(fileName.c_str(), "r");
if( fp == NULL )
{
cout<<"file cannot be
opened"<<endl;
exit(0);
}
else
{
int i = 0;
int num;
while( fscanf(fp, "%d", &num)
!= EOF && i < MAXSIZE )
{
nums[i] =
num;
i++;
}
*numElems = i;
}
}
void printArray(int nums[], int numElems)
{
int i = 0;
cout << endl;
while( i < numElems )
{
cout << std::setw(7) <<
nums[i] ;
if( (i+1) % 7 == 0)
cout <<
endl;
i++;
}
cout<<endl;
}
double findAverage(int nums[], int numElems)
{
int i = 0;
double sum = 0;
while( i < numElems )
{
sum += nums[i];
i++;
}
return sum / numElems;
}
int findValue(int nums[], int numElems, int value)
{
int i = 0, index = -1;
while( i < numElems )
{
if( nums[i] == value )
{
index = i;
break;
}
i++;
}
return index;
}
Execution and Output: