I need Help with my code Please Help.

Here is the description:
Consider lists of numbers from real-life data
sources, for example, a list containing the number of students enrolled in different
course sections, the number of comments posted for different Facebook status
updates, the number of books in different library holdings, the number of votes per
precinct, etc. It might seem like the leading digit of each number in the list should
be 1–9 with an equally likely probability. However, Benford’s Law states that the
leading digit is 1 about 30% of the time and drops with larger digits. The leading
digit is 9 only about 5% of the time. Write a program that tests Benford’s Law. Collect a list of at least one hundred numbers from some real-life data source and enter them into a text file.
Your program should loop through the list of numbers and count how many times 1 is the
first digit, 2 is the first digit, etc. For each digit, output the percentage it appears
as the first digit. If you read a number into the string variable named strNum then you can access
the first digit as a char by using strNum[0] .

And Here is how I got so far:
Programming Language C++ (I just take this class this F2013)

#include
#include
#include
#include
#include
#include

int main(int argc, char** [0])
{
std::string strNum
//this just a random # I put in here, I want digit 1-9 NOT ZERO
("15157,83049,1193049090,1289727392,9075,54984,5419981,231540,1498056");

std::map digit_frequency;

for(int i = 0; i < strNum.size(); ++i)
{
std::stringstream ss;
ss << strNum[i];
std::string str = ss.str();
if(isdigit(str[0]))
{
++digit_frequency[str[0]];
}
else if(isdigit(str[1]))
{
++digit_frequency[str[1]];
}
}

std::map::iterator it;
for(it = digit_frequency.begin(); it != digit_frequency.end(); it++)
{
std::cout << "Leading Digit " << it->first << ": repeated " << it->second << " time(s).\n";
}

return 0;
}


output:

click the link to see the output.

http://www.facebook.com/#!/photo.php?fbid=10200531325265379&set=a.1645092726942.2077529.1225598765&type=1&theater

KingLord
http://www.cplusplus.com/articles/jEywvCM9/
Thanks.

1
2
3
4
5
6
#include
#include
#include
#include
#include
#include  


What are you including?
#include<facebook>
@ Chriscpp - lol

But seriously, please tell us your problem exactly and what you are including
I want the leading digit 1-9 ONLY not start with 0, for example ("1564,5978,15633, 50010,068664,4587,94982",264,3635,15,747,8486,97")

we count the above numbers and count how many times each repeat or occur
As we see above 1564 has a 1 leading digit... 15633...15...
2 leading digit ... 264 and so on till reach leading digit 9.

digit: repeated :
0:-----> 4 -----> I want to get rid of leading 0.
1:-----> 3
2:-----> 1
3:-----> 2
4:-----> 4
5:-----> 5
6:-----> 5
7:-----> 2
8:-----> 4
9:-----> 3

And the probability:
1 about 30%
......... 9 about 5%


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cctype>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

int main(int argc, char** [0])
{
    std::string strNum
("15157,83049,1193049090,1289727392,9075,54984,5419981,231540,1454");

    std::map<char, int> digit_frequency;

    for(int i = 0; i < strNum.size(); ++i)
    {
        std::stringstream ss;
        ss << strNum[i];
        std::string str = ss.str();
        if(isdigit(str[0]))
        {
            ++digit_frequency[str[0]];
        }
        else if(isdigit(str[1]))
        {
            ++digit_frequency[str[1]];
        }
    }

    std::map<char, int>::iterator it;
    for(it = digit_frequency.begin(); it != digit_frequency.end(); it++)
    {
        std::cout << "Leading Digit " << it->first << ": repeated " << it->second << " time(s).\n";
    }

    return 0;
}
Last edited on
closed account (28poGNh0)
A new version!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# include <cctype>
# include <iostream>
# include <map>
# include <string>

int main()
{
    std::string strNum("15157,83049,1193049090,1289727392,9075,54984,5419981,231540,1454");

    std::map<char, int> digit_frequency;
    std::map<char, int>::iterator it;

    size_t totalNbrs = 0;

    for(size_t i = 0; i < strNum.size(); ++i)
        if(isdigit(strNum[i]))
        {
            totalNbrs++;
            digit_frequency[strNum[i]]++;
        }

    totalNbrs -= digit_frequency['0'] ;
    digit_frequency.erase(digit_frequency.begin());

    for(it = digit_frequency.begin(); it != digit_frequency.end(); it++)
        std::cout << "Leading Digit " << it->first << ": repeated " << it->second << " time(s) with "
        << (float)it->second*100/totalNbrs << "%\n";

    return 0;
}


hope that helps
Last edited on
Thanks for spent your time review my code and do a good job on, BUT my point is that the:

strNum("15157,83049,1193049090,1289727392,9075,54984,5419981,231540,1454");

leading digit of one = 15157, 1193049090, 1289727392 as the leading digit... all the first digit that has one as their first digit...
so cout for repeated = 3 which:(15157, 1193049090, 1289727392 )

leading digit of two = 1 which: (231540)

leading digit of three = 0, since there's no 3 as a leading digit above the strNum

leading digit of four = 0, since there's no 4 as a leading digit above the strNum

leading digit of five= 2 which(54984, 5419981)

leading digit of six = 0 repeated....

leading digit of seven = 0 repeated....

leading digit of eight = 1 which (83049)

leading digit of nine = 1 which (9075)


Thanks hope some one nice help me edited (^_^)



How 'bout this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
...
int times[9]{0,0,0,0,0,0,0,0,0};

//Note: this has to be pretty much Exactly as in the format of your strNum example. Otherwise, it  wont work
char temp = ' ';
bool nextgroup = true;
for(unsigned int i = 0; i < strNum.size(), i++)
{
        temp = strNum[i];
        if(nextgroup)
        { 
                if(isdigit(temp))
                {
                        switch(temp)
                       {
                        case 1:
                                ++times[1];
                                 break;
                        case 2:
                                ++times[2];
                                 break;
                         //Don't want to type the rest, but you get the idea.
                      }
                 }
                 nextgroup = false;
        {
        else if(temp == ',')
               nextgroup = true;
        
        else
               nextgroup = false;
       
}



Thanks hope some one nice help me edited (^_^)


For the ones, you missed 1454 (the last one)
Topic archived. No new replies allowed.