I need to write a C++ program to analyze three different data files, and try to confirm Benford’s law.I need to 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 is the link to following text files :-

  1. http://joehummel.net/uploads/citypopulations.txt
  2. http://joehummel.net/uploads/librarybooks.txt
  3. http://joehummel.net/uploads/sunspots.txt

Here’s what the output from your program should look like:

https://www.facebook.com/photo.php?fbid=3521048202971&set=a.1063646609467.8762.1775059752&type=1&relevant_count=1&ref=nf

Program Requirements

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:

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

return 0;
}

Recommended Answers

All 7 Replies

Do you honestly expect someone to give you an answer based on the attempt that you've done? Seriously.. Do some work and if you get stuck on something in particular then someone will help.

proper technique for opening files

Who has stated a "proper technique" by who's standards?

Here’s what the output from your program should look like

Links do not work.

@Ancient Dragon, you can see the attachment for the result...

You need to do the work. We will not do it for you. What code have you written so far?

@NathanOliver, that is what I have achieved so far...Its works perfectly but I'm trying to make my code more efficient & trying to reduce LOC.Have a look at my code at let me now about your logic.
#include <iostream>
#include <fstream>

using namespace std;

int analyzeData(string fname);
int analyzeData2(string fname);
int analyzeData3(string fname);
int first(int num);

int main()
{
    if(analyzeData("citypopulations.txt") != 0)
        cout << "Error reading citypopulations.txt\n";
    if(analyzeData2("librarybooks.txt") != 0)
        cout << "Error reading librarybooks.txt";
    if(analyzeData3("sunspots.txt") != 0)
        cout << "Error reading sunspots.txt\n";

    system("PAUSE");
    return EXIT_SUCCESS;
    return 0;
}

int analyzeData(string fname)
{
    ifstream infile("C:\\Dev-Cpp\\Benford\\citypopulations.txt");
    int tmp,count = 0;
    float percents[9];
    int nums[9] = { 0 };
    if(!infile.good())
        return 1;
    while(!infile.eof())
    {
        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 << fname << "\ncount: " << count << "\n\n";
    for(int i = 0; i < 9; i++)
        cout << i + 1 << " : " << nums[i] << ", " << percents[i]*100 << "\n";
    cout << "\n";
    return 0;
}

int first(int num)
{
    int rt;
    if(num == 0)
        return -1;
    while( 1 )
    {
        if(num / 10 == 0)
        {
            rt = num;
            return rt;
        }
        num /= 10;
    }
}
/**/
int analyzeData2(string fname)
{
    ifstream infile("C:\\Dev-Cpp\\Benford\\librarybooks.txt");
    int tmp,count = 0;
    float percents[9];
    int nums[9] = { 0 };
    if(!infile.good())
        return 1;
    while(!infile.eof())
    {
        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 << fname << "\ncount: " << count << "\n\n";
    for(int i = 0; i < 9; i++)
        cout << i + 1 << " : " << nums[i] << ", " << percents[i]*100 << "\n";
    cout << "\n";
    return 0;
}
/* 
int first(int num)
{
    int rt;
    if(num == 0)
        return -1;
    while( 1 )
    {
        if(num / 10 == 0)
        {
            rt = num;
            return rt;
        }
        num /= 10;
    }
}

*/
int analyzeData3(string fname)
{
    ifstream infile("C:\\Dev-Cpp\\Benford\\sunspots.txt");
    int tmp,count = 0;
    float percents[9];
    int nums[9] = { 0 };
    if(!infile.good())
        return 1;
    while(!infile.eof())
    {
        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 << fname << "\ncount: " << count << "\n\n";
    for(int i = 0; i < 9; i++)
        cout << i + 1 << " : " << nums[i] << ", " << percents[i]*100 << "\n";
    cout << "\n";
    return 0;
}

Well the first couple of things that I see right off the bat are

  1. You didn’t write your main function like you are instructed to.

  2. You pass the filename to your functions but never use it and instead you hard coded the file in each function.

  3. You have three separate analyze functions when you should only have one.

Did you notice that all three of your functions are exactly the same except for the file name? The reason the function prototype your instructor gave you has it taking in the filename is that the one function should be able to handle any number of files as long as the files are formatted correctly. To use the file name from the functions parameters all you need to do is this

void analyzeData(string fname)
{
    ifstream infile(fname.c_str());
    // do stuff
}

@NathanOliver, got it!
thx a lot for the help
Regards :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.