I have an array codes[26] which contains numbers inside {25,4646,23,12,.....29}

Each position in the array corresponds to a letter of the alphabet. code[0] is a, code[1] is b, etc...

I'd like to print the occurrences of each letter, something like this.

If there are 12 A's, 5 B's

AAAAAAAAAAAAA
BBBBB
.... so on for the rest of the letters.

Which approach can I take to accomplish this ?

have you written any line of code so far ?? If yes, then post it.

as far as an approach is concerned....if you know about arrays, then its a simple (homework) problem...

You say that indexes of the array represents chars. Since indexes are unique there will be only one char for the corresponding index. How can there be 15 A where there is only one array[0] ?

And what those array elements are for? Where do you use them?

Sure, the array code[] does not store the letters, it stores the occurrences of the letters.

Example
code = {23, 45, 2, 5, ...} up to 26 values in the array
Now I know that
code[0] = a (from above there are 25 A's)
code[1] = b (45 B's)
code[2] = c (2 C's)
etc...

From the problem you helped me solved earlier, that code populates the array code. It reads from a text file and counts the occurrences of letters and stores the final count onto code[]. Does that help?

Thanks again

@tellalca:

IMHO, I think what he means is that a[0] is 'A' and hence if the value of a[0] is 5(say) then the code must print AAAAA (i.e. 5 A's)

Correct me if I am wrong.

I was right :) ( i didn't see his explanation first, otherwise I wouldn't have posted the previous one)

Well as far as solution is concerned....

A small hint:

Use the value of your array in a loop condition and print A's or B's or whatever.

Yeah I'm thinking that but in my head looks like a really messy code.

Say I have to go through each member and just specify that code[0] = 'a' for every character? Seems rather long to me, I'm looking for an easier way to do this

Ok, I got it know :)

You did the big part of the program then :) You just need to design a loop that runs for every alphabet( 26 times ). Then in it design another loop that prints alphabets. Inner loop must print the current alphabet as much as it's occurence.

If you got stuck let us know.

Nope. You dont have to. you already know that right. You can fool your computer anytime. If I'm thinking it right then you need 2 loops. One will iterate through your code array. That is it must look like:

``for (int i = 0; i<26; i++)``

and the next comes nested under the first one and goes something like:

``for (int j = 0; j<code[i]; j++)``

in this 2nd loop you do the printing.

hahaaa....it seems we almost gave you the same idea, at the same time....:)

One more thing, you can use a char variable to print your alphabets

``char myChar = 'A';``

and then inside the 2nd loop you print this (as many times the loop iterates) and then you INCREMENT it so that it now becomes 'B';

``myChar++    //will make it 'B' if it was 'A'``

Hope it helps.

Great stuff! I had no clue this existed, very very handy! Thanks a bunch!

I have a quick question.

I'm getting aaaabbbbbcccc, how can I get a line break in that loop to separate the letters from each other?
aaaa
bbbbbbb
ccc

Inside the first loop just prints
a
a
a
a
b
b
b

as expected. Inside the second loop gives me a lot of white space

for newline you can use \n (thats an escape sequence and is used for line breaks) after the inner loop.

Oops silly me, solved!

Yeah I just had it on the wrong loop

great...in that case just mark this thread solved. And if any of my answers impressed you, add a reputation...:)

Done and done :)

Thanks

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.