-2

My programs for the most part but there are two issues that I can't seem to solve with my program

  1. When characters for number_1 have spaces such as ("do be do bo") is imputed my program only responds with the 1st 2 letters and stops immediately after the space
  2. Another issue is my program is supposed to rank by frequency of characters but I cant think of a way to sort the frequency integers while also keeping track of which letter they are tied too in addition be able to produce results by order of frequency rather than alphabetically

    #include <iostream> 
    #include <cmath>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include <sstream> 
    #include <string>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm> 
    
    
    int search(const long ab[], int number_used, int target, int &counter);
    using namespace std;
    stringstream ss;
    
    int main()
    {
    int a=97;
    int b=98;
    int c=99;
    int d=100;
    int e=101;
    int f=102;
    int g=103;
    int h=104;
    int i=105;
    int j=106;
    int k=107;
    int l=108;
    int m=109;
    int n=110;
    int o=111;
    int p=112;
    int q=113;
    int r=114;
    int s=115;
    int t=116;
    int u=117;
    int v=118;
    int w=119;
    int x=120;
    int y=121;
    int z=122;
    int aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as,at,au,av,aw,ax,ay,az=0;
    int bl=0;
    char number_1[432];
    cout << "Enter text:\n";
    cin >> number_1;
    long ca[432];
    for (long bi=0; bi<432; bi++)
    {
    ca[(bi)]=number_1[bi];
    }
    char da[14]={' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',};
    search(ca,432,32,bl);
    for (long bi=0; bi<432; bi++)
    {
    if(number_1[bi]!=da[bi] || number_1[bi]!=0)
    {
    ca[(bi-bl)]=number_1[bi];
    }
    else if(number_1[bi]==da[bi])
    {
    bl==bl+1;
    }
    else
    {
    }
    }
    long xb;
    xb=strlen(number_1);
    long ci=0;
    long bruh[xb];
    for (long bi=0; bi<xb; bi++)
    {
    bruh[(bi-bl)]=number_1[bi];
    }
    
    
    sort(bruh, bruh+xb);
    
    
    search(bruh,xb,a,aa);
    search(bruh,xb,b,ab);
    search(bruh,xb,c,ac);
    search(bruh,xb,d,ad);
    search(bruh,xb,e,ae);
    search(bruh,xb,f,af);
    search(bruh,xb,g,ag);
    search(bruh,xb,h,ah);
    search(bruh,xb,i,ai);
    search(bruh,xb,j,aj);
    search(bruh,xb,k,ak);
    search(bruh,xb,l,al);
    search(bruh,xb,m,am);
    search(bruh,xb,n,an);
    search(bruh,xb,o,ao);
    search(bruh,xb,p,ap);
    search(bruh,xb,q,aq);
    search(bruh,xb,r,ar);
    search(bruh,xb,s,as);
    search(bruh,xb,t,at);
    search(bruh,xb,u,au);
    search(bruh,xb,v,av);
    search(bruh,xb,w,aw);
    search(bruh,xb,x,ax);
    search(bruh,xb,y,ay);
    search(bruh,xb,z,az);
    
    
    cout << "Frequencies:\n";
    if(aa!=0 && aa<50)
    {
    cout << "a " << aa << "\n";
    }
    if(ab!=0 && ab<50)
    {
    cout << "b " << ab << "\n";
    }
    if(ac!=0 && ac<50)
    {
    cout << "c " << ac << "\n";
    }
    if(ad!=0 && ad<50)
    {
    cout << "d " << ad << "\n";
    }
    if(ae!=0 && ae<50)
    {
    cout << "e " << ae << "\n";
    }
    if(af!=0 && af<50)
    {
    cout << "f " << af << "\n";
    }
    if(ag!=0 && ag<50)
    {
    cout << "g " << ag << "\n";
    }
    if(ah!=0 && ah<50)
    {
    cout << "h " << ah << "\n";
    }
    if(ai!=0 && ai<50)
    {
    cout << "i " << ai << "\n";
    }
    if(aj!=0 && aj<50)
    {
    cout << "j " << aj << "\n";
    }
    if(ak!=0 && ak<50)
    {
    cout << "k " << ak << "\n";
    }
    if(al!=0 && al<50)
    {
    cout << "l " << al << "\n";
    }
    if(am!=0 && am<50)
    {
    cout << "m " << am << "\n";
    }
    if(an!=0 && an<50)
    {
    cout << "n " << an << "\n";
    }
    if(ao!=0 && ao<50)
    {
    cout << "o " << ao << "\n";
    }
    if(ap!=0 && ap<50)
    {
    cout << "p " << ap << "\n";
    }
    if(aq!=0 && aq<50)
    {
    cout << "q " << aq << "\n";
    }
    if(ar!=0 && ar<50)
    {
    cout << "r " << ar << "\n";
    }
    if(as!=0 && as<50)
    {
    cout << "s " << as << "\n";
    }
    if(at!=0 && at<50)
    {
    cout << "t " << at << "\n";
    }
    if(au!=0 && au<50)
    {
    cout << "u " << au << "\n";
    }
    if(av!=0 && av<50)
    {
    cout << "v " << av << "\n";
    }
    if(aw!=0 && aw<50)
    {
    cout << "w " << aw << "\n";
    }
    if(ax!=0 && ax<50)
    {
    cout << "x " << ax << "\n";
    }
    if(ay!=0 && ay<50)
    {
    cout << "y " << ay << "\n";
    }
    if(az!=0 && az<50)
    {
    cout << "z " << az << "\n";
    }
    
    }
    
    int search(const long ab[], int number_used, int target, int &counter)
    {
    int a=97;
    int b=98;
    int c=99;
    int d=100;
    int e=101;
    int f=102;
    int g=103;
    int h=104;
    int i=105;
    int j=106;
    int k=107;
    int l=108;
    int m=109;
    int n=110;
    int o=111;
    int p=112;
    int q=113;
    int r=114;
    int s=115;
    int t=116;
    int u=117;
    int v=118;
    int w=119;
    int x=120;
    int y=121;
    int z=122;
        int index=0;
        int count=0;
    counter=0;
        bool found=false;
        while ((!found) && (index < number_used))
        {   if(target==ab[index])
            {
                count++;
                index++;
                counter++;
            }
            else
            {
            index++;
            }
        }
        if(counter==0)
        {
    
        }
        else if(counter>0)
        {
    
        }
    }
    
Biswajit_86
  • 3,661
  • 2
  • 22
  • 36

1 Answers1

0
  1. cin is designed to stop after it finds whitespace (typically space, tab, end of line) and return everything up to that whitespace. This allows you to use it as an easy tokenizer. This is answered a number of ways here. Pick one.Also answered here, and this answer better targets your specific case.
  2. Make an array of 26 integers. Every time you find a letter, increment the appropriate entry in the array. Hint: lowercase all input so you don't count 'A' and 'a' as different letters. Hint frequencies[letter-'a'];

Extra notes:

your list of int a = 97;int b = 98; is unnecessary. the numeric value for char 'a' is 97 and char will promote itself to int as needed so search(bruh,xb,a,aa); can be search(bruh,xb,'a',aa);

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • I use the series of integer variable (aa,ab,ac,ad,ae,af,ag,ah) to keep tally of the amount of times a character is used. should i place these integers in said array or should I make unique array. Once I input the array I would just have the frequencies If i were to sort the array. How would I be able to connect the new array to the given charcter. I dont understand what you mean by the last part of point 2. How do I tie the array with 26 integers to the specific letter so that when I announce the frequency that the correct letter follows no matter with there are 200 as or all d? – Bruhtastic May 11 '15 at 08:06
  • @Bruhtastic Make a class or structure with two members: the letter and the count. – user4581301 May 11 '15 at 15:36