Jump to content
  • Advertisement
Sign in to follow this  
drjulio2002

Problem understanding instructions!

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello Everyone! I don't clearly understand the following instructions from "The C Programming Language": "Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging." I know the histogram, but...What do you understand from it? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Essentially you want to do this:

1. Loop through all the inputs
2. Create an array that stores the length of each of those inputs
3. Print out your histogram


//Assuming you have lengths between 1 and 10
int g_nArray[10]; // index 0 is unused

int main(int argc, char *argv[])
{
// argc == # of parameters
// argv == the parameters in array form

for(int i=1; i<argc; i++)
{
if(strlen(argv) < 10)
{
g_nArray[ strlen(argv) ]++;
}
else
{
continue;
}
}

//Print the histogram

for(int j=0; j<10; j++)
{
cout<<"-"<<j<<" ";
for(int z=0; z<g_nArray[j]; z++)
{
cout<<"*";
}
cout<<endl;
}
}


g/l

EDIT:
Here are the results from running:
cmd> histogram.exe the cat ran from the dog

-0
-1
-2
-3 *****
-4 *
-5
-6
-7
-8

Share this post


Link to post
Share on other sites
making a vertical one just helps you to develop problem solving skills, as it's not as straightforward as a horizontal one.

Share this post


Link to post
Share on other sites
Quote:
Original post by Undergamer
//Assuming you have lengths between 1 and 10
int g_nArray[10]; // index 0 is unused

This way you'll only be able to count words up to the length of 9, not 10.

Share this post


Link to post
Share on other sites
Hello Everyone!

Thanks for your replies, I'll make a version of this program later, and show it to you, so that you can see my way of thinking of this problem. and I think the above user is right 0-9 of an array I think, that's what they don't like to see, but thanks for your reply!

Again Thanks to all!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!