I am trying to create a simple program to read in words from a file, count the words, and output a histogram of word lengths normalised to 1. I am having trouble reading in the words from the file. If anyone could help, it would be greatly appreciated.

Regards
Kevin

Recommended Answers

All 3 Replies

It sounds like homework....post what you have done till now and ask specific question regarding that code

Ok, yes, it is homework, and I've been tearing my hair out trying to get it to work, but eventually got there. The final thing I need to do is adjust the output that is sent to the console. It is supposed to be a histogram displaying the frequency of word lengths. I can get it to display, but its very bunched up. I think I'm supposed to use the setw() function, but am not sure where it is supposed to be used. The code is below, and any help would be appreciated.

int Displayhist (int scaledfrequency[])
{
    cout << setw(4);
    for (int row = 20 ; row >= 0; row--)
    {
        if (row == 20)
        {
           cout << "1.0 ";
        }
        else if (row == 0)
        {
             cout << "0.0 ";
        }
        else if (row == (20 / 2))
        {
             cout << "0.5 ";
        }
        else
        {
            cout << " .  ";
        }

        for (int k = 0; k < 20; k++)
        {

            if (scaledfrequency[k] >= row) 
            {
               cout << "*";
            }
            else
            {
                cout << " ";
            }
        }
    
    cout << endl;
    }
    for (int j = 0; j <= 20; j++)
    {
        cout << j;
    }
    cout << endl;
}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

Regards
Kevin

You need to make room for the numbers on the bottom, I'd guess. And that would mean you'd need to space out your columns:

for (int k = 0; k < 20; k++)
        {

            if (scaledfrequency[k] >= row)
            {
               cout << " * ";
            }
            else
            {
                cout << "   ";
            }
        }

    cout << endl;
    }
    cout << "    ";
    for (int j = 0; j <= 20; j++)
    {
        cout << setw(2) << j << ' ';
    }
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.