our task is to show the histogram in a vertical orientation like this:

Sample Input/Output
Enter a string: waka waka fifa
Histogram for waka waka fifa
*
*
* * * *
* * * * *
a f i k w

I have here my code, but the output of this is in horizontal orientation.. what am i going to do to make this vertical? the sanple output of this is:

Sample Input/Output
Enter a string: waka waka fifa
Histogram for waka waka fifa
a * * * * *
f * *
i *
k * *
w * *

# include <iostream>
# include <string>
using namespace std;
main()
{ 
  string letters="abcdefghijklmnopqrstuvwxyz";
  string sentence;
  string::size_type slen;
  int name[26];
  char ans;

do{  
  for(int s=0; s < 26; s++)
    name[s] = 0;
  
  cout<<"Enter a string (no space please): ";
  cin>>sentence;
  cout<<endl;
  cout<<"Histogram for "<<sentence<<endl;
  
//  slen = sentence.length();
  
  for(int t=0; t<26; t++){
    for(int sl=0; sl<slen; sl++){
      if (letters[t] == sentence[sl])
        name[t]++;
    }
  }
  
  for (int r=0; r < 26; r++){
    if (name[r] > 0){
      cout<<letters[r]<<" ";
    for(int i=0; i <name[r]; i++)
      cout<<"*";
    cout<<endl;
    } 
  } 
  
      cout << endl << "another? [y/n]: ";
      cin >> ans; 
      cout<<endl;
       
}while(ans != 'n'); 


      cout<<"\n\nPress any key and ENTER to EXIT! ";         
      char response;
      cin>>response;
      
      return 0;
}

You have a set number of lines corresponding to the maximum number. If the count in your int array exceeds a certain criteria print a star, doing this for each index of the array. Take a stab at it, you'll probably come pretty close and post back if you have problems.

From looking at the original problem, it may be required, and it makes the "drawing vertically" problem a bit easier, to sort from most often occurring letter to least occurring letter. But as jonsca said you can do it without sorting too.

I was able to do it just using the maximum of the data set but sorting certainly works for that too. Someone gave the OP a turnkey solution in one of his other threads :( (I did not test it) so we'll see what he/she reports back.

This article has been dead for over six months. Start a new discussion instead.