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
***********
i used the following code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int MAXSIZE = 100;
int fillarray(string name, string names[], int ages[]){
fstream file;
string word, t, q, filename;
filename = name;
file.open(filename.c_str());
int i=0;
while (file >> names[i]>>ages[i])
{
i++;
}
return i;
}
void sorting(string name[], int age[], int l){
int temp=0;
string n;
for(int k=0;k<l;k++)
{
for(int j=k+1;j<l;j++)
{
if(age[k]>age[j]){
temp = age[j];
age[j]=age[k];
age[k]=temp;
n = name[j];
name[j]=name[k];
name[k]=n;
}
}
}
}
double findAverageAge(int age[], int l){
double avg=0;
int sum =0 ;
for(int i=0; i<l;i++){
sum = sum + age[i];
}
avg = (sum/(double)l);
return avg;
}
void printArray(string name[],int age[], int l)
{
for(int i=0;i<l-1;i++){
cout<<name[i]<<" "<<age[i]<<endl;
}
}
double determineMedian(int age[],int l){
double median=0;
if (l % 2 != 0)
median = (double)age[l/2];
median = (double)(age[(l-1)/2] + age[l/2])/2.0;
return median;
}
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;
int length = fillarray(fileName,names,ages);
cout << "Before Sort" << endl;
printArray(names,ages,length);
sorting(names,ages,length);
cout << "After Sort" << endl;
printArray(names,ages,length);
cout.setf(ios:: fixed);
cout.precision(2);
averageAge = findAverageAge(ages,length);
cout << "The average age is: " << averageAge << endl;
median = determineMedian(ages,length);
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;
}
**********
this is what i get:
Given below is the code for the question. Please do rate the answer if it helped. Thank you.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int MAXSIZE = 100;
int fillArray(string name, string names[], int ages[]);
void sortArrays(string name[], int age[], int l);
double findAverageAge(int age[], int l);
void printArray(string name[],int age[], int l);
double determineMedian(int age[],int l);
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;
int length =
fillArray(fileName,names,ages);
cout << "Before Sort" << endl;
printArray(names,ages,length);
sortArrays(names,ages,length);
cout << "After Sort" << endl;
printArray(names,ages,length);
cout.setf(ios:: fixed);
cout.precision(2);
averageAge = findAverageAge(ages,length);
cout << "The average age is: " << averageAge << endl;
median =
determineMedian(ages,length);
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;
}
int fillArray(string name, string names[], int ages[]){
fstream file;
string word, t, q, filename;
filename = name;
file.open(filename.c_str());
int i=0;
while (file >> names[i]>>ages[i])
{
i++;
}
return i;
}
void sortArrays(string name[], int age[], int l){
int temp=0;
string n;
for(int k=0;k<l;k++)
{
for(int j=k+1;j<l;j++)
{
if(age[k]>age[j]){
temp = age[j];
age[j]=age[k];
age[k]=temp;
n = name[j];
name[j]=name[k];
name[k]=n;
}
}
}
}
double findAverageAge(int age[], int l){
double avg=0;
int sum =0 ;
for(int i=0; i<l;i++){
sum = sum + age[i];
}
avg = (sum/(double)l);
return avg;
}
void printArray(string name[],int age[], int l)
{
for(int i=0;i<l-1;i++){
cout<<name[i]<<"
"<<age[i]<<endl;
}
}
double determineMedian(int age[],int l){
double median=0;
if (l % 2 != 0)
median = (double)age[l/2];
else
median = (double)(age[(l-1)/2] +
age[l/2])/2.0;
return median;
}
First create the two text file given below. Then complete the main that is given. There...
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...
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...
I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...
I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...
Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...
In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){ ifile.open(filename.c_str(), ios::in); if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); } } void...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...
C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record { int age; string name; }; int check(record r[], int n, string nm) { for (int i = 0; i < n;...