Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

heres what i have already: #include <iostream> #include <fstream> #include <stri

ID: 3543274 • Letter: H

Question

heres what i have already:

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

void analyzeData(string fileName) ////function
{

ifstream infile(fileName);

int tmp = 0;
int count = 0;
int nums[9] = { 0 };
float percents[9]; /////hold 9 floats

if (!infile.good() )
return;

while(!infile.eof()) // if didnt reach end of file
{
infile >> tmp;
tmp = first(tmp);
if(tmp > 0)
{
nums[tmp - 1] ++;
count++;
}

}

for(int i = 0; i < 9; i++) //////

percents[i] = (float)nums[i]/count;
cout << fileName << " count: " << count << " ";

for(int i = 0; i < 9; i++) //////

cout << i + 1 << " : " << nums[i] << ", " << percents[i] << " ";
cout << " ";

return;
}

Once you have completed part 1, the goal now is to write a C++ program to analyze three different data files, and try to confirm Benford's law. You will create a console application that opens each file, counts the number of values that start with '1', '2', '3', etc., and then outputs the percentages of each digit. Based on the output, we can test the validity of Benford's law. Here's what the output from your program should look like: For example, in the "sunspots.txt" data file, there were a total of 3074 values in the file. The '1' digit was the first digit in 868 of those values, for a percentage of 28.23%. This adheres to Benford's law. [Note: your total count and digit counts should match exactly the output above; you percentages may differ slightly.] You are free to solve the problem as you see fit, but there are 2 requirements. First, you must use the proper technique for opening files: check that the open was successful using the file object's good() method, and if not, output an error message and exit the program immediately with an error code. Second, the main program *MUST* be written as follows: In other words, your program must contain at least one function called analyzeData, defined as follows: You can define additional functions if you wish, but it is not required. Note that the function you wrote for part 1 - countOnes - can be used directly, or you might consider using that function as a template for your analyzeData function. Entirely up to you.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include<sstream>
using namespace std;
void analyzeData(string fileName) ////function
{
ifstream infile(fileName);
if (!infile.good())
{
cout << "Unable to open file " <<fileName << " so Exiting from program. " << endl;
return;
}
int tmp = 0;
int count = 0;
int nums[9] = { 0 };
float percents[9]; /////hold 9 floats
while(!infile.eof()) // if didnt reach end of file
{
infile >> tmp;
stringstream ss;
ss << tmp;
int first_digit = ss.str()[0]-'0';
if(first_digit > 0)
{
nums[first_digit - 1] ++;
count++;
}
}
for(int i = 0; i < 9; i++) //////
percents[i] = (float)nums[i]/count;
cout << fileName << " count: " << count << " ";
for(int i = 0; i < 9; i++) //////
cout << i + 1 << " : " << nums[i] << ", " << percents[i] << " ";
cout << " ";
return;
}

int main()
{
analyzeData("citypopulations.txt");
analyzeData("librarybooks.txt");
analyzeData("sunspots.txt");
return 0;
}