Question

In: Computer Science

In C++ First create the txt file given below. Then complete the main that is given....

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

Solutions

Expert Solution

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:


Related Solutions

First create the text file given below. Then complete the main that is given. There are...
First create the text file given below. Then complete the main that is given. There are comments to help you. An output is also given Create this text file: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 ` Create a project and a Main class and copy the Main class and method given below. First declare the array below the comments that tell you to declare it. Then there...
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
1. create a .txt file and call it fruits.txt, add the following items to the file...
1. create a .txt file and call it fruits.txt, add the following items to the file Apple Banana Peach Strawberry Watermelon Kiwi Grape _______ 2. Write a program that does the following: - Reads the fruit.txt - Converts the contents into all uppercase - Writes the uppercased values into a text file called "UpperFruit.txt" OUTPUT SHOULD BE: APPLE BANANA PEACH STRAWBERRY WATERMELON KIWI GRAPE
how to create a script file on puTTy script pp1.txt
how to create a script file on puTTy script pp1.txt
Create a C++ file with given instructions below- Analyze random numbers Create two arrays, one int...
Create a C++ file with given instructions below- Analyze random numbers Create two arrays, one int one double. They should have 10 elements each Ask the user how many random numbers to generate. Then generate that many random numbers between 1 and 10. Use a for loop. Keep track of how many of each number in the range was generated (1 to 10) in the int array. Be sure to initialize the array to all 0's to start. Then, send...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT