arrays

1.Input characters from the user until a '-' character is reached. The input will all be characters, though some characters may not be letters; your program should ignore all non-letter characters input.
2.Count the number of each letter that appear. Your program must treat upper and lower case characters as being the same. i.e. the input "aA-" will count 2 a's rather than 1 'a' and 1 'A'.
3.Use an array to store the letter counts.
4.For each letter of the alphabet, output the percentage of the input (not counting non-letter input) that was that letter.


char letter = 'M';
int position = letter - 'A';


How would I go about trying this? I have never worked with arrays before and am not sure where to get started. My main issue is how to output the percentage of each letter. Thank you!
ok you would make an int array of size 27 and set each position in the array to 0. then you will use a while loop to loop the program until the user enters'-'.

inside the while loop you will have to evaluate what the user entered .... aka is the char an 'a' or a 'b' or 'c' or an 'A' etc. you can use if else statements here or even better i would use a switch statemetnt. but in the swith statemetn or ifs whatever you decide to use you will have to add a number to the respective count. so for example the char entered is an 'a', under the a section you would have something like this

array[1] = array[1] + 1; // postion 1 being that of 'a'

// if it were a 'b'
array[2] = array[2] + 1;

ok so the loop will run this until the user enters the '-' char. but you have been keeping count all along. so for each letter you have to something like this;

the float is used so we get a decimal like .25 etc
if we just divided below it wouldnt be correct due to integer division

cout << "The % of 'A' = " << float(array[1])/27 << endl;
cout << "The % of 'B' = " << float(array[2])/27 << endl;
cout << "The % of 'C' = " << float(array[3])27 << endl;

you should be able to solve the problem now. Hope this helped
Thank you very much!
ok so my program is as follows but it says my letters aren't declared.

char letters;
int letcount;
int total;
float array[27];
do
{
cout << "Please print a series of letters followed by a '-' symbol" << endl;
cin >> letters;
if (letters==a||A)
{
array[1] = array[1] + 1;
}
for (int i = 0; i < letters; i++)
for (int j =0; j < letcount; j++)
array [i][j]= letcount/total;
cout << array << endl;
} while (letters |= -)
Topic archived. No new replies allowed.