469,354 Members | 2,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,354 developers. It's quick & easy.

Histogram question

2
To student:



The this the new assignment.



Ex7 VERTICAL HISTOGRAM

Write a program that reads text and prints a vertical histogram (bar graph) representing the occurrences of each letter of the alphabet (A-Z) in the text. Characters other than the letters (A-Z) are to be ignored. You can assume that no lower case letters appear in the text.



Text to be tested:



"THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. THIS IS AN EXAMPLE OF HOW TO TEST YOUR HISTOGRAM

PROGRAM. YOU CAN USE THIS EXAMPLE."



Sample Run

*

*

* *

* * *

* * * **

* * * * ***

* * ** * * ****

* * ** * ** ****

* * *** ***** **** **

* * ***** ***** **** ***

**************************

ABCDEFGHIJKLMNOPQRSTUVWXYZ


End of problem.



I don't know .How do i start?
Dec 4 '08 #1
15 5746
gpraghuram
1,275 Expert 1GB
First have a 2 dimensional array which stores the occurances of every alphabet.
Increment the value depending on the character u read.
Finally you need to print it

Raghu
Dec 4 '08 #2
newb16
687 512MB
@gpraghuram
There is no need in two-dimensional array, one-dimensional array is enough. No need in 2d array during print phase as well.
Dec 4 '08 #3
JosAH
11,448 Expert 8TB
@OP: I changed your thread title. Next time use a proper title please; text speak is not much appreciated in this forum.

kind regards,

Jos (moderator)
Dec 4 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
When you do your display, to get the image correctly, make a pass of the array and find the largest number.

Then for the actual print, you make a pass of the array and print a space for each element whose value is less than the largest number. Only the element equal to the largest number is printed as an *.

Repeat the above after reducing the largest value by 1.

When yo get ot zero, print the letters corresponding to your array elements.
Dec 4 '08 #5
whodgson
542 512MB
Try puting the sentence into an array like:
Expand|Select|Wrap|Line Numbers
  1. char dog[]={"THE QUICK BROWN FOX etc"};
  2. //Now use a for loop to traverse the array like:
  3. int j=char(65);
  4. while(j<91)    
  5.   for(int i=0;i<27;i++)
  6.      {
  7.        if(array[i]==char( j ))cout<<"*"<<"\n";//prints a new line after each splat
  8.        if(array[i]==26)cout<<"\t";//prints a tab after each pass
  9.        j++;
  10.     } 
Now print the uppercase alphbet on one line with a tab between each.
Might work.
Dec 6 '08 #6
whodgson
542 512MB
I think the following would produce an histogram based on the y axis
Expand|Select|Wrap|Line Numbers
  1. int j=65;//the char A
  2. while(j<91)
  3.     {
  4.      for(int i=0;i<44;i++)//the number of letters and spaces in the sentence
  5.      {if(i<1)cout<<"\n";//staarts a new line before every traverse of fox[]
  6.      if(fox[i]==j)cout<<char (j)<<" ";//prints the letter not *
  7.      else cout<<""; }
  8.       j++;
  9.  
  10.     }
  11.  
Dec 8 '08 #7
zaturn
2
Thx Everybody This my histrogram

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main ( void )
  8. {start:
  9. int num[26] = {};
  10. int height=0,;
  11. int y1,x1;
  12. int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,
  13. r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0,z=0;
  14. string text;
  15. cout<<"Input 0 to exit or Type your text : ";
  16. getline(cin,text);
  17. if(text=="0")goto finish;
  18. cout<<'\n';
  19. for(int count=0;count<text.length();++count)
  20. {
  21. if(text.substr(count,1)=="A"||text.substr(count,1)=="a"){++a;num[0]=a;}
  22. if(text.substr(count,1)=="B"||text.substr(count,1)=="b"){++b;num[1]=b;}
  23. if(text.substr(count,1)=="C"||text.substr(count,1)=="c"){++c;num[2]=c;}
  24. if(text.substr(count,1)=="D"||text.substr(count,1)=="d"){++d;num[3]=d;}
  25. if(text.substr(count,1)=="E"||text.substr(count,1)=="e"){++e;num[4]=e;}
  26. if(text.substr(count,1)=="F"||text.substr(count,1)=="f"){++f;num[5]=f;}
  27. if(text.substr(count,1)=="G"||text.substr(count,1)=="g"){++g;num[6]=g;}
  28. if(text.substr(count,1)=="H"||text.substr(count,1)=="h"){++h;num[7]=h;}
  29. if(text.substr(count,1)=="I"||text.substr(count,1)=="i"){++i;num[8]=i;}
  30. if(text.substr(count,1)=="J"||text.substr(count,1)=="j"){++j;num[9]=j;}
  31. if(text.substr(count,1)=="K"||text.substr(count,1)=="k"){++k;num[10]=k;}
  32. if(text.substr(count,1)=="L"||text.substr(count,1)=="l"){++l;num[11]=l;}
  33. if(text.substr(count,1)=="M"||text.substr(count,1)=="m"){++m;num[12]=m;}
  34. if(text.substr(count,1)=="N"||text.substr(count,1)=="n"){++n;num[13]=n;}
  35. if(text.substr(count,1)=="O"||text.substr(count,1)=="o"){++o;num[14]=o;}
  36. if(text.substr(count,1)=="P"||text.substr(count,1)=="p"){++p;num[15]=p;}
  37. if(text.substr(count,1)=="Q"||text.substr(count,1)=="q"){++q;num[16]=q;}
  38. if(text.substr(count,1)=="R"||text.substr(count,1)=="r"){++r;num[17]=r;}
  39. if(text.substr(count,1)=="S"||text.substr(count,1)=="s"){++s;num[18]=s;}
  40. if(text.substr(count,1)=="T"||text.substr(count,1)=="t"){++t;num[19]=t;}
  41. if(text.substr(count,1)=="U"||text.substr(count,1)=="u"){++u;num[20]=u;}
  42. if(text.substr(count,1)=="V"||text.substr(count,1)=="v"){++v;num[21]=v;}
  43. if(text.substr(count,1)=="W"||text.substr(count,1)=="w"){++w;num[22]=w;}
  44. if(text.substr(count,1)=="X"||text.substr(count,1)=="x"){++x;num[23]=x;}
  45. if(text.substr(count,1)=="Y"||text.substr(count,1)=="y"){++y;num[24]=y;}
  46. if(text.substr(count,1)=="Z"||text.substr(count,1)=="z"){++z;num[25]=z;}
  47. }
  48. for (x1=0; x1<26; x1++) height = max(height, num[x1]);
  49. for (y1=height; y1>=1; y1--)
  50. {
  51. cout << setw(3) << y1 << "|";
  52. for (x1=0; x1<26; x1++)
  53. if (num[x1]>=y1) cout << " *"; else cout << " ";
  54. cout << endl;
  55. }
  56. cout << "---+" << string(26*2, '-') << endl;
  57. cout << " |";
  58. for (x1=0; x1<26; x1++)
  59. {
  60. if (x1<26)
  61. cout << " " << char('A'+x1);
  62.  
  63. }
  64. cout << endl << endl;goto start;
  65. finish:
  66.  
  67. system("pause");
  68. }
  69.  
Dec 18 '08 #8
newb16
687 512MB
Use text[count] and compare it with char literal ( single quotes, 'A' instead of "A" )
Do no unroll inner loop 'A' .. 'Z' - i.e. use loop from 'A' to 'Z' and make variables a .. z an array ( oh wait.. there is already num[], why do you need another set of variables? )
use toUpper().
Dec 18 '08 #9
whodgson
542 512MB
This is the output from thread#7
Forms a histogram of letters in a sentence

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG

A
B
C
D
E E E
F
G
H H
I
J
K
L
M
N
O O O O
P
Q
R R
S
T T
U U
V
W
X
Y
Z
*/
Satisfactory?
Dec 19 '08 #10
JosAH
11,448 Expert 8TB
@whodgson
Nope; the OP wants that histogram rotated 90 degrees.

kind regards,

Jos
Dec 20 '08 #11
whodgson
542 512MB
ok JosAH
how about this:
Expand|Select|Wrap|Line Numbers
  1.  int jcount=0;
  2.   //get the quantity of each letter and record in dog array.
  3.     j=65;//'A"
  4.     while(j<91) //'Z'
  5.     { 
  6.       jcount=0;
  7.       for(int i=0;i<80;i++)//number of letters & spaces in sentence
  8.          { if(fox[i]==j)jcount++;
  9.            dog[j]=jcount;
  10.          }
  11.       j++;
  12.      }
  13.  
  14.     for(int j=65;j<91;j++)//print the count of each letter   
  15.        { cout<<dog[j]<<" ";}
  16.         cout<<"\n";
  17.     int count = 0;
  18.     while(count<8)//print the rows of asterisks
  19.        {for(int j=65;j<91;j++) 
  20.          if(dog[j]>count)cout<<"* ";
  21.          else cout<<"  ";
  22.          if(j>=90)cout<<"\n";
  23.          count++;
  24.          if(count==8)break;
  25.        }
  26.  
which produces this:
Forms a vertical histogram of the quantity of each
letter in a sentence

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
WHICH IS A GOOD WAY TO FRIGHTEN DOGS





A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
3 1 2 3 4 2 4 5 4 1 1 1 1 2 8 1 1 3 3 4 2 1 3 1 2 1
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * *
* *
*
*
*


.............never again!

. .
Dec 29 '08 #12
JosAH
11,448 Expert 8TB
@whodgson
But now it is upside down; you have to flip it 180 degrees ;-)

kind regards,

Jos
Dec 29 '08 #13
whodgson
542 512MB
ok Jos
180 flip cummin up;
Expand|Select|Wrap|Line Numbers
  1.  int count = 8;
  2.     while(count>-1)//print the histogram asterisks
  3.        {for(int j=65;j<91;j++) 
  4.          if(dog[j]> count)cout<<"* ";
  5.          else cout<<"  ";
  6.          if(j>=90)cout<<"\n";
  7.          count--;
  8.          if(count==-1)break;
  9.        }
which produces this:
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
WHICH IS A GOOD WAY TO FRIGHTEN DOGS
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
3 1 2 3 4 2 4 5 4 1 1 1 1 2 8 1 1 3 3 4 2 1 3 1 2 1

*
*
*
* *
* * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
ok?
Jan 1 '09 #14
JosAH
11,448 Expert 8TB
@whodgson
Almost; according to the OP's example the 'A B ... Z' line should be at the bottom, but that change is futile; well done and happy New Year!

kind regards,

Jos
Jan 1 '09 #15
whodgson
542 512MB
@JosAH
Many thanks your patience (& exactitude!) also compliments of new season.
w.
Jan 5 '09 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Oracle3001 | last post: by
1 post views Thread by bleh | last post: by
27 posts views Thread by ext_u | last post: by
12 posts views Thread by KraftDiner | last post: by
2 posts views Thread by Daniel Nogradi | last post: by
5 posts views Thread by arnuld | last post: by
35 posts views Thread by rajash | last post: by
1 post views Thread by avenger3200 | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.