| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Array help!
I need to ask the user to input a bunch of letters until a '-' symbol is entered. I then need to total up how many letters of each letter there are and find a percentage presented in 2 columns.
eg. input addfhb- letter frequency a 0.17 b 0.17 c 0 d 0.33 e 0 f 0.17 g 0 h 0.17 . . . I am not sure where to get started on my code and how I would intialize or define an array. Thank you |
|
#2
|
||||||
|
||||||
|
Normally, I say "don't use array, use vector". In this case, however, using either of them is not really appropriate.
In this particular use case, I suggest using a map instead. It is a far better tool for the job. cpp Code:
No data exists for the letters that haven't been entered, so it won't show them. I don't recommend it, but without the iterators you can still do Code:
const char letters[]="abcdefghij";
for (int j=0; j<strlen(letters); j++)
cout << letters[j] << '\t' << table[letters[j]]/(float)total << '\n';
}
If you absolutely have to use arrays, such as because you're using a compiler from the early 80s or a teacher for the same period, create an array int table[256]; and make sure to set each of them to 0 first. Then you can use it similarly to the map with the non-iterator version of the read.
__________________
Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Array help! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|