473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

count the frequency of letters in an array with random generated letters

6
Hi everyone. It's me again. The problem is pretty much stated in the title. But I'll expand the goal of the program. The user first inputs the seed and number of characters then a set random letters (both upper and lower cases) are generated based on the user input. That part is generally done, but the second portion is the problem. So after generating the random letters, the program should count the frequency of each generated letter, and it does not matter if the letter is upper or lower case. So as an example, the generated letters are:

akIskwiF
IwoPdkqA <-- this part of the program is done
Beidbdmc

the second output (frequency) will be:

a=2
b=2
c=1
d=3
e=1
f=1
g=0

and so on.
I separated the letter increments from the actual frequency count function. So there is a separate function doing the frequency and the output from this function is placed on the 'output' function. So the syntax of the cout portion of the output function (in theory), is:

Expand|Select|Wrap|Line Numbers
  1. cout<<ch<<" = "<<(cntChar freq. for each letter)<<endl;
  2.  
where ch comes from a loop that increments the letters and cntChar is the frequency of each letter.
Oh well just run the code below to see what I'm talking about. Judging by what I have done though, the problem is referencing the values of the array to the cntChar function (for it to work) and transporting the frequency value of each letter to the output function. I'll find some solutions to this but for the meantime this is it. Thank you very much.

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. const int size = 100;
  7. char val(int& length, int& seed);
  8. int inLgth(int& length, int& seed);
  9. int output(char& input);
  10.  
  11. int main(){
  12.     char a[size], input, ch;
  13.     int seed, length, i;
  14.     bool repeat = false;
  15.     cout<<"Enter the seed number: ";cin>>seed;
  16.     cout<<endl;
  17.     do{
  18.     inLgth(length, seed);
  19.     output(input);
  20.     system("cls");
  21.     } while(input == 'y' || input == 'Y');
  22.     if (input != 'y' || input != 'Y') exit(1);
  23.  system ("pause");
  24.  return 0;
  25.  }
  26.  
  27. int inLgth(int& length, int& seed){
  28.     char a[size];
  29.     cout<<"Number of characters: ";cin>>length;
  30.     cout<<endl
  31.         <<"Generated characters:"<<endl<<endl;
  32.     a[size] = val(length, seed);
  33.     cout<<endl;
  34.     return a[size];
  35. }
  36.  
  37. char val(int& length, int& seed) {
  38.     int i;
  39.     srand(seed);
  40.     for (i = 1; i <= length; i++){
  41.     if(i%10==0)cout<<endl;
  42.     char num = rand() % 52;
  43.     if (num < 26) {
  44.         num += 'a';
  45.     } else {
  46.         num -= 26;
  47.         num += 'A';
  48.     }cout<<num;
  49.     }cout<<endl;
  50. }
  51.  
  52. int cntChar( char a[size] , char& ch, int& length ){
  53.       int cntCh = 0;   
  54.       for (int i = 0; i<=length; i++){
  55.                if ( a[size] == ch )
  56.                      cntCh += 1;
  57.        }
  58.        return cntCh;
  59. }
  60.  
  61. int output(char& input){
  62.    char str, ch, cntCh;
  63.    cout<<"Summary for the characters:"<<endl<<endl;
  64.    for(ch='a';ch<='z';ch++){
  65.    cout<<ch<<" = "<<cntCh<<endl;
  66.    }
  67.    cout<<endl
  68.        <<"Repeat? (same seed) (Y/N) ";cin>>input;
  69.    return input;
  70. }
  71.  
Oct 16 '11 #1
1 3671
weaknessforcats
9,208 Expert Mod 8TB
I would use two arrays like this:

Expand|Select|Wrap|Line Numbers
  1. char data[5];
  2. int frequency[5];
So search data for your character. If you find it at data[i] then add one to frequency[i]. When you have processed all the data, the frequency counts are in the other array. Just display that array.
Oct 16 '11 #2

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

Similar topics

21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
18
by: Rob Meade | last post by:
Hi all, I need to be able to create some license keys for an application. Ideally these wouldn't be too long in length (ie, easier to remember/type in), but I would like them to be auto...
9
by: gl | last post by:
How do I take an array or arraylist, and sort it randomly? Like suppose the items in it are (1,2,3,4,5) and I want to get it to be in a random order (2,3,1,4,5). How do you do that? I think it's a...
1
by: Sluggoman | last post by:
Hi all, Am floundering through a course in which C was not a pre-req, but the assignment is in C - If someone could point out where I am going way off the rails, I'd apprecciate it. Please be...
6
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and...
8
by: WuJianWei | last post by:
here' s the description: ========================================================= You are asked to develop a C++ program that reads a text Żle given by the user (e.g. test.txt), and output the...
3
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
2
by: Mesvak | last post by:
Hi, I have this code which takes in a file, reads it in as a binary array, and prints the array. Is there anyway I can write a code which would print the number of binary values taken and...
4
by: BaseballGraphs | last post by:
Hello, I keep getting the following error when I output the code below: Notice: Undefined property: stdClass::$count $sql = "SELECT COUNT(name), name FROM searches GROUP BY name"; $result...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.