( go to http://www.globeatnight.org/maps.php ): First create a file from a GaN y
ID: 3698125 • Letter: #
Question
( go to http://www.globeatnight.org/maps.php ): First create a file from a GaN year of data that contains a single column of NELM data. Bad NELM values are integers 1-7 and -9999 entries are invalid and need to be removed from the file. Write a program that queries the user for a data file name and reads the data into a one-dimensional integer array called NELM[ ]. Make sure that you specify a maximum array size that does not exceed the C++ limits!
1.Your MAIN program must
2.Call a function that calculates the relative frequency of each value 1-7 and counts the number of invalid entries.
3.Call another function that writes the data array values to a new file if it is not a -9999 value. The function should be called void removeBad. Your function should count the total number of invalid entries and return this number to the main program.
4.Calls on the statistics function provided to calculate the mean value of the NELM for the year. Note that you cannot use data that is -9999 so add a condition into the program that eliminates such data from the calculation.
5.The main program should print the following to screen:
Number of invalid (-9999) entries.
Mean value of NELM for the year.
Relative frequencies of NELMs
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
void cleandata ()
{
int bad_values = 0;
vector<int> NELM;
ifstream file_("e://removebadtext.txt");
ofstream file("e://result.txt");
int number;
while(file_ >> number)
{
if(number == -9999) bad_values++;
else
{
NELM.push_back(number);
file << number<<" "; // writinf numbers to file and and putting a space
}
}
cout << "Number of removed data points: " << bad_values << endl;
file_.close();
file.close();
}
void frequency()
{
ifstream file_("e://removebadtext.txt");
ofstream file("e://frequency.txt");
int number;
int frequency1 = 0;
int frequency2 = 0;
int frequency3 = 0;
int frequency4 = 0;
int frequency5 = 0;
int frequency6 = 0;
int frequency7 = 0;
while(file_ >> number)
{
if(number == 1) frequency1++;
else if(number == 2) frequency2++;
else if(number == 3) frequency3++;
else if(number == 4) frequency4++;
else if(number == 5) frequency5++;
else if(number == 6) frequency6++;
else if(number == 7) frequency7++;
}
double total = frequency1+frequency2+frequency3+frequency4+frequency5+frequency6+frequency7;
file << "Relative Frequency of 1: " << (frequency1/total)*100 << endl;
file << "Frequency of 2: " << (frequency2/total)*100 << endl;
file << "Frequency of 3: " << (frequency3/total)*100<< endl;
file << "Frequency of 4: " << (frequency4/total)*100 << endl;
file << "Frequency of 5: " << (frequency5/total)*100<< endl;
file << "Frequency of 6: " << (frequency6/total)*100 << endl;
file << "Frequency of 7: " << (frequency7/total)*100 << endl;
file_.close();
file.close();
}
int main()
{
cleandata();
frequency();
system("pause");
return 0;
}
Explanation / Answer
Hi, I have made required changes. Please test it with data.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
int cleandata (vector<int> NELM)
{
int bad_values = 0;
ifstream file_("e://removebadtext.txt");
ofstream file("e://result.txt");
int number;
while(file_ >> number)
{
if(number == -9999) bad_values++;
else
{
NELM.push_back(number);
file << number<<" "; // writinf numbers to file and and putting a space
}
}
file_.close();
file.close();
return bad_values;
}
void frequency(vector<int> NELM)
{
int number;
int frequency1 = 0;
int frequency2 = 0;
int frequency3 = 0;
int frequency4 = 0;
int frequency5 = 0;
int frequency6 = 0;
int frequency7 = 0;
unsigned int i=0;
while(i < NELM.size())
{
number = NELM[i];
if(number == 1) frequency1++;
else if(number == 2) frequency2++;
else if(number == 3) frequency3++;
else if(number == 4) frequency4++;
else if(number == 5) frequency5++;
else if(number == 6) frequency6++;
else if(number == 7) frequency7++;
i++;
}
double total = frequency1+frequency2+frequency3+frequency4+frequency5+frequency6+frequency7;
cout << "Relative Frequency of 1: " << (frequency1/total)*100 << endl;
cout << "Frequency of 2: " << (frequency2/total)*100 << endl;
cout << "Frequency of 3: " << (frequency3/total)*100<< endl;
cout << "Frequency of 4: " << (frequency4/total)*100 << endl;
cout << "Frequency of 5: " << (frequency5/total)*100<< endl;
cout << "Frequency of 6: " << (frequency6/total)*100 << endl;
cout << "Frequency of 7: " << (frequency7/total)*100 << endl;
}
double statistics(vector<int> NELM){
double total = 0;
for(unsigned int i=0; i<NELM.size(); i++){
total = total + NELM[i];
}
return total/NELM.size();
}
int main()
{
vector<int> NELM;
int bad_values = cleandata(NELM);
cout << "Number of invalid (-9999) entries: " << bad_values << endl;
cout<<"Mean value of NELM for the year: "<<statistics(NELM)<<endl;
frequency(NELM);
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.