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?
15 6116
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
@gpraghuram
There is no need in two-dimensional array, one-dimensional array is enough. No need in 2d array during print phase as well.
@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)
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.
Try puting the sentence into an array like: - char dog[]={"THE QUICK BROWN FOX etc"};
-
//Now use a for loop to traverse the array like:
-
int j=char(65);
-
while(j<91)
-
for(int i=0;i<27;i++)
-
{
-
if(array[i]==char( j ))cout<<"*"<<"\n";//prints a new line after each splat
-
if(array[i]==26)cout<<"\t";//prints a tab after each pass
-
j++;
-
}
Now print the uppercase alphbet on one line with a tab between each.
Might work.
I think the following would produce an histogram based on the y axis -
int j=65;//the char A
-
while(j<91)
-
{
-
for(int i=0;i<44;i++)//the number of letters and spaces in the sentence
-
{if(i<1)cout<<"\n";//staarts a new line before every traverse of fox[]
-
if(fox[i]==j)cout<<char (j)<<" ";//prints the letter not *
-
else cout<<""; }
-
j++;
-
-
}
-
Thx Everybody This my histrogram -
-
#include <iostream>
-
#include <iomanip>
-
#include <string>
-
using namespace std;
-
-
int main ( void )
-
{start:
-
int num[26] = {};
-
int height=0,;
-
int y1,x1;
-
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,
-
r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0,z=0;
-
string text;
-
cout<<"Input 0 to exit or Type your text : ";
-
getline(cin,text);
-
if(text=="0")goto finish;
-
cout<<'\n';
-
for(int count=0;count<text.length();++count)
-
{
-
if(text.substr(count,1)=="A"||text.substr(count,1)=="a"){++a;num[0]=a;}
-
if(text.substr(count,1)=="B"||text.substr(count,1)=="b"){++b;num[1]=b;}
-
if(text.substr(count,1)=="C"||text.substr(count,1)=="c"){++c;num[2]=c;}
-
if(text.substr(count,1)=="D"||text.substr(count,1)=="d"){++d;num[3]=d;}
-
if(text.substr(count,1)=="E"||text.substr(count,1)=="e"){++e;num[4]=e;}
-
if(text.substr(count,1)=="F"||text.substr(count,1)=="f"){++f;num[5]=f;}
-
if(text.substr(count,1)=="G"||text.substr(count,1)=="g"){++g;num[6]=g;}
-
if(text.substr(count,1)=="H"||text.substr(count,1)=="h"){++h;num[7]=h;}
-
if(text.substr(count,1)=="I"||text.substr(count,1)=="i"){++i;num[8]=i;}
-
if(text.substr(count,1)=="J"||text.substr(count,1)=="j"){++j;num[9]=j;}
-
if(text.substr(count,1)=="K"||text.substr(count,1)=="k"){++k;num[10]=k;}
-
if(text.substr(count,1)=="L"||text.substr(count,1)=="l"){++l;num[11]=l;}
-
if(text.substr(count,1)=="M"||text.substr(count,1)=="m"){++m;num[12]=m;}
-
if(text.substr(count,1)=="N"||text.substr(count,1)=="n"){++n;num[13]=n;}
-
if(text.substr(count,1)=="O"||text.substr(count,1)=="o"){++o;num[14]=o;}
-
if(text.substr(count,1)=="P"||text.substr(count,1)=="p"){++p;num[15]=p;}
-
if(text.substr(count,1)=="Q"||text.substr(count,1)=="q"){++q;num[16]=q;}
-
if(text.substr(count,1)=="R"||text.substr(count,1)=="r"){++r;num[17]=r;}
-
if(text.substr(count,1)=="S"||text.substr(count,1)=="s"){++s;num[18]=s;}
-
if(text.substr(count,1)=="T"||text.substr(count,1)=="t"){++t;num[19]=t;}
-
if(text.substr(count,1)=="U"||text.substr(count,1)=="u"){++u;num[20]=u;}
-
if(text.substr(count,1)=="V"||text.substr(count,1)=="v"){++v;num[21]=v;}
-
if(text.substr(count,1)=="W"||text.substr(count,1)=="w"){++w;num[22]=w;}
-
if(text.substr(count,1)=="X"||text.substr(count,1)=="x"){++x;num[23]=x;}
-
if(text.substr(count,1)=="Y"||text.substr(count,1)=="y"){++y;num[24]=y;}
-
if(text.substr(count,1)=="Z"||text.substr(count,1)=="z"){++z;num[25]=z;}
-
}
-
for (x1=0; x1<26; x1++) height = max(height, num[x1]);
-
for (y1=height; y1>=1; y1--)
-
{
-
cout << setw(3) << y1 << "|";
-
for (x1=0; x1<26; x1++)
-
if (num[x1]>=y1) cout << " *"; else cout << " ";
-
cout << endl;
-
}
-
cout << "---+" << string(26*2, '-') << endl;
-
cout << " |";
-
for (x1=0; x1<26; x1++)
-
{
-
if (x1<26)
-
cout << " " << char('A'+x1);
-
-
}
-
cout << endl << endl;goto start;
-
finish:
-
-
system("pause");
-
}
-
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().
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?
@whodgson
Nope; the OP wants that histogram rotated 90 degrees.
kind regards,
Jos
ok JosAH
how about this: - int jcount=0;
-
//get the quantity of each letter and record in dog array.
-
j=65;//'A"
-
while(j<91) //'Z'
-
{
-
jcount=0;
-
for(int i=0;i<80;i++)//number of letters & spaces in sentence
-
{ if(fox[i]==j)jcount++;
-
dog[j]=jcount;
-
}
-
j++;
-
}
-
-
for(int j=65;j<91;j++)//print the count of each letter
-
{ cout<<dog[j]<<" ";}
-
cout<<"\n";
-
int count = 0;
-
while(count<8)//print the rows of asterisks
-
{for(int j=65;j<91;j++)
-
if(dog[j]>count)cout<<"* ";
-
else cout<<" ";
-
if(j>=90)cout<<"\n";
-
count++;
-
if(count==8)break;
-
}
-
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!
. .
@whodgson
But now it is upside down; you have to flip it 180 degrees ;-)
kind regards,
Jos
ok Jos
180° flip cummin up; - int count = 8;
-
while(count>-1)//print the histogram asterisks
-
{for(int j=65;j<91;j++)
-
if(dog[j]> count)cout<<"* ";
-
else cout<<" ";
-
if(j>=90)cout<<"\n";
-
count--;
-
if(count==-1)break;
-
}
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?
@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
@JosAH
Many thanks your patience (& exactitude!) also compliments of new season.
w.
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
|
5 posts
views
Thread by Enigma Curry |
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
| | | | | | | | | | | |