Question

In C++ First create the two text file given below. Then complete the main that is given. There ar...

In C++

First create the two text 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: data.txt (remember blank line at end)

Mickey 90
Minnie 85
Goofy 70
Pluto 75
Daisy 63
Donald 80

Create this text file: data0.txt (remember blank line at end)

PeterPan 18
Wendy 32
Michael 28
John 21
Nana 12

Main

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

const int MAXSIZE = 100;

// Prototypes





int main()
{
    string names[MAXSIZE];
    int ages[MAXSIZE];
    int numElements;
    string fileName;
    double averageAge;
    double median;
    char again;

    do
    {

        cout << "Enter the file name: ";
        cin >> fileName;

        // Call the function fillArray. It has the fileName, the name array and
        // the age array passed in (in that order). It returns the number of people read in
        // Description of function given below



        cout << "Before Sort" << endl;
        // Call the function printArray. It has the name array, age array and
        // the number of people passed in (in that order). 
        // Description of function given below




        // Call the function sort array. It has the name array, age array,
        // and the number of people passed in (in that order).
        // Description of function given below




        cout << "After Sort" << endl;
        // Call the function printArray. It has the name array, age array and
        // the number of people passed in (in that order). 
        // Description of function given below




        cout.setf(ios:: fixed);
        cout.precision(2);
        // Call the function findAverageAge. It has the age array and
        // the number of people passed in (in that order). It stores the value
        // that is returned in the averageAge variable declared above.
        // Description of function given below




        cout << "The average age is: " << averageAge << endl;

        // Call the function determineMedian. This function has the age array
        // and the number of people passed in (in that order). I returns
        // the median age and stores it into the variable median declared
        // above





        cout << "The median age is: " << median << endl;

        cout << endl;
        cout << "Do you want to do this again? (Y/N): ";
        cin >> again;
    } while (toupper (again) == 'Y');
    return 0;
}



// Function: sortArrays
// This function has the name array, the age array and the number 
// of elements passed in. It sorts the data by putting the ages in 
// numerical order. (Hint: the names have to stay with the person,
// so you have to do something with the name array too)





// Function: findAverageAge
// This function has the age array and the number of elements passed in.
// It computes the average of the ages in the array and returns it.





// Function: determineMedian
// This function has the ages array (which is sorted) and the number 
// of elements passed in. It returns the median grade






// Function: printArray
// This function has the name array, the age array and the number 
// of elements passed in.
// It prints the arrays in neat columns (see output)






// Function: fillArray
// This function should open the file with the name that is passed into it. It should
// then read in the names and ages and load them into the appropriate arrays. 
// Make sure you check that you don't exceed the array size.
// If the file has too many names and numbers, your program should not put the 
// extra people in the array, the array will just be full.
// This function determines the number of names and ages in the arrays
// and should return the number.
// This function should not call any other user defined functions.

Sample Output

Enter the file name: data.txt
Before Sort
Mickey         90
Minnie         85
Goofy          70
Pluto          75
Daisy          63
Donald         80
After Sort
Daisy          63
Goofy          70
Pluto          75
Donald         80
Minnie         85
Mickey         90
The average age is: 77.17
The median age is: 77.50

Do you want to do this again? (Y/N): Y
Enter the file name: data0.txt
Before Sort
PeterPan       18
Wendy          32
Michael        28
John           21
Nana           12
After Sort
Nana           12
PeterPan       18
John           21
Michael        28
Wendy          32
The average age is: 22.20
The median age is: 21.00

Do you want to do this again? (Y/N): n
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

const int MAXSIZE = 100;

// Prototypes
int fillArray(string fileName, string names[MAXSIZE], int ages[MAXSIZE]);
void printArray(string names[MAXSIZE], int ages[MAXSIZE], int numElements);
void sortArray(string names[MAXSIZE], int ages[MAXSIZE], int numElements);
double findAverageAge(int ages[MAXSIZE], int numElements);
double determineMedian(int ages[MAXSIZE], int numElements);

int main()
{
   string names[MAXSIZE];
   int ages[MAXSIZE];
   int numElements;
   string fileName;
   double averageAge;
   double median;
   char again;

   do
   {

       cout << "Enter the file name: ";
       cin >> fileName;
       numElements = fillArray(fileName, names, ages);
       // Call the function fillArray. It has the fileName, the name array and
       // the age array passed in (in that order). It returns the number of people read in
       // Description of function given below

       cout << "Before Sort" << endl;
       printArray(names, ages, numElements);
       // Call the function printArray. It has the name array, age array and
       // the number of people passed in (in that order).
       // Description of function given below


       sortArray(names, ages, numElements);
       // Call the function sort array. It has the name array, age array,
       // and the number of people passed in (in that order).
       // Description of function given below


       cout << "After Sort" << endl;
       printArray(names, ages, numElements);
       // Call the function printArray. It has the name array, age array and
       // the number of people passed in (in that order).
       // Description of function given below


       cout.setf(ios::fixed);
       cout.precision(2);  
       // Call the function findAverageAge. It has the age array and
       // the number of people passed in (in that order). It stores the value
       // that is returned in the averageAge variable declared above.
       // Description of function given below

       averageAge = findAverageAge(ages, numElements);
       cout << "The average age is: " << averageAge << endl;
       // Call the function determineMedian. This function has the age array
       // and the number of people passed in (in that order). I returns
       // the median age and stores it into the variable median declared
       // above


       median = determineMedian(ages, numElements);
       cout << "The median age is: " << median << endl;

       cout << endl;
       cout << "Do you want to do this again? (Y/N): ";
       cin >> again;
   } while (toupper(again) == 'Y');
   return 0;
}

// Function: sortArrays
// This function has the name array, the age array and the number
// of elements passed in. It sorts the data by putting the ages in
// numerical order. (Hint: the names have to stay with the person,
// so you have to do something with the name array too)

void sortArray(string names[MAXSIZE], int ages[MAXSIZE], int numElements)
{
   for (int i = 0; i < numElements; i++) // goto every element in the array.
   {
       for (int j = 0; j < numElements - 1; j++) // compare that element with every other element in the array, j < numElements - 1 is important here.
       {
           if (ages[j] > ages[j + 1]) // if first element is greater than second element.
           {
               swap(ages[j], ages[j + 1]); // swap the ages.
               swap(names[j], names[j + 1]); // and swap the name indexes too.
           }
           else if (ages[j] == ages[j + 1]) // if ages are equal.
           {
               if (names[j].compare(names[j + 1]) > 0) // compare with respect to name lexicographically.
               {
                   swap(ages[j], ages[j + 1]); // swap the ages.
                   swap(names[j], names[j + 1]); // and swap the name indexes too.
               }
           }
       }
   }
}


// Function: findAverageAge
// This function has the age array and the number of elements passed in.
// It computes the average of the ages in the array and returns it.

double findAverageAge(int ages[MAXSIZE], int numElements)
{
   double totalAge = 0;
   for (int i = 0; i < numElements; i++)
   {
       totalAge += ages[i]; // sums all the ages in a variable and takes average by divinding it with total elements.
   }
   return totalAge / numElements;
}


// Function: determineMedian
// This function has the ages array (which is sorted) and the number
// of elements passed in. It returns the median grade

double determineMedian(int ages[MAXSIZE], int numElements) // in the even sample, sum of two integers / 2 will not give a double value in c++. i.e (77.50 will be printed 77.00)
{
   double ret = 0;
   if (numElements % 2 == 0) // if array size is even.
   {
       ret = ((double)ages[(numElements / 2) - 1] + (double)ages[numElements / 2]) / 2; // the sum of two middle ages divided by 2 is the median age.
   }
   else // if array size is odd.
   {
       ret = (double)ages[numElements / 2]; // middle index of sorted array contains median age.
   }
   return ret;
}

// Function: printArray
// This function has the name array, the age array and the number
// of elements passed in.
// It prints the arrays in neat columns (see output)

void printArray(string names[MAXSIZE], int ages[MAXSIZE], int numElements)
{
   for (int i = 0; i < numElements; i++)
   {
       cout << names[i] << setw(15) << ages[i] << endl; // printing the data loaded from file.
   }
}


// Function: fillArray
// This function should open the file with the name that is passed into it. It should
// then read in the names and ages and load them into the appropriate arrays.
// Make sure you check that you don't exceed the array size.
// If the file has too many names and numbers, your program should not put the
// extra people in the array, the array will just be full.
// This function determines the number of names and ages in the arrays
// and should return the number.
// This function should not call any other user defined functions.

int fillArray(string fileName, string names[MAXSIZE], int ages[MAXSIZE])
{
   int count = 0;
   ifstream fin(fileName);
   while (!fin.eof()) // while end of file is not reached.
   {
       fin >> names[count] >> ages[count]; // keep taking input.
       count++; // increment count, used for indexing and also for numElements;
       if (count == MAXSIZE) // if count reaches maxsize, that means array is full, so stop taking more input.
       {
           cout << "Array Is Full!";
           break;
       }
   }
   return count - 1;
}

Enter the file name: data.txt Before Sort Mickey Minnie Goofy Pluto Daisy Donald After Sor t Daisy Goofy Pluto Donald Minnie

please note the comment in determineMedian function defination.

for any query leave a comment here ill get back to you as soon as possible :)

PLEASE DO NOT FORGET TO LEAVE A THUMBS UP! THANKS! :)

Enter the file name: data.txt Before Sort Mickey Minnie Goofy Pluto Daisy Donald After Sor t Daisy Goofy Pluto Donald Minnie Mickey The average age 1s. /P.T The median age is: 77.00 90 85 63 80 63 80 90 Do you want to do this again? (Y/N): Enter the file name: data0. txt Before Sort PeterPan Wendy Michael John Nana After Sor t Nana PeterPan John Michael Wendy The average age is: 22.20 The median age is: 21.00 28 28 32 Do you want to do this again? (Y/N): rn

Add a comment
Know the answer?
Add Answer to:
In C++ First create the two text file given below. Then complete the main that is given. There ar...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • First create the two text file given below. Then complete the main that is given. There...

    First create the two text 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: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12 Main #include...

  • looking for in Java Write the method loadArray. The method will declare and open a file...

    looking for in Java Write the method loadArray. The method will declare and open a file stream and connect it to the external file passed in. It will then read patient names into the patients array and their temperatures into the temps array. The method should compute/determine the number of patients in the file. Make sure you do all steps of file VO] A sample file, and some declarations and a sample function call are given Sample File Mickey 102.5...

  • 17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int...

    17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int array1[20] = {3, 18, 1, 25, 4, 7, 30, 9, 80, 16, 17}; int numElements = 11; cout << "Part 1" << endl; // Part 1 // Enter the statement to print the numbers in index 4 and index 9 // put a space in between the two numbers    cout << endl; // Enter the statement to print the numbers 3 and 80...

  • I'm not getting out put what should I do I have 3 text files which is...

    I'm not getting out put what should I do I have 3 text files which is in same folder with main program. I have attached program with it too. please help me with this. ------------------------------------------------------------------------------------ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the...

  • c++, we have to write functions for the code given below and other instructions for it...

    c++, we have to write functions for the code given below and other instructions for it to compile. I am having issues understanding how to confront the problem and how to write functions and read the program so it can eventually be solved so it can be compiled 7/ * INSTRUCTIONS: Write two functions in the space // * indicated below. // * // * #1 => Find index of maximum value: Write a function that will // * find...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • C++ Programming - Design Process I need to create a flow chart based on the program...

    C++ Programming - Design Process I need to create a flow chart based on the program I have created. I am very inexperienced with creating this design and your help will be much appreciated. My program takes a string of random letters and puts them them order. Our teacher gave us specific functions to use. Here is the code: //list of all the header files #include <iomanip> #include <iostream> #include <algorithm> #include <string> using namespace std; //Here are the Function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT