#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Bar graph for frequency analysis


    Hello. I am currently working on a program in c++, in which the user inputs text in a string, and the program then returns the amount of words, sentences and question marks in the string. A frequency analysis is also carried out and a bar graph is to be produced to represent the occurance of each character by way of stars. This is the code I have so far.

    #include <iostream>
    using namespace std;
    int main()

    {
    int counts[256] = {0};
    char mytext[200];
    int x;
    int i;
    char ch = 'z';
    int words = 0;
    int sentences = 0;
    int questions = 0;



    cout << "enter some text: ";
    gets(mytext);
    x = strlen(mytext);


    cout << "you typed: " << mytext << endl;
    cout << "length: " << x << endl;
    for (i=0;i<=x-1;i++)
    {
    ch = mytext[i];
    switch(ch)
    {
    case ' ':
    words++;
    break;
    case '.':
    sentences++;
    words++;
    break;
    case '?':
    questions++;
    sentences++;
    words++;

    }


    }


    cout << "Words: " << words << endl;
    cout << "Sentences: " << sentences << endl;
    cout << "questions: " << questions << endl;


    while(ch != 'z')
    {
    x = ch;
    counts[x]++;
    }

    for (i=65;i<=122;i++)
    {ch = i;
    cout << " * " << counts[i] << endl;
    }





    system("pause");
    return 0;
    }

    The first part is working fine, but the frequency analysis and bar graph are not been returned to the screen :confused: What am I doing wrong?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    It won't surprise me when we find out that someone else wrote this program for you. I've improved it a little but it still stinks. You've got more work to do.

    In addition to merely fixing the code, please change your input system so that the program can count the words in large texts. The code should handle
    your_wc < Catcher_in_the_Rye.txt
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <iostream>
    
    #ifndef __unix__
    # define SUCK_WAD
    #endif
    
    using namespace std;
    int main() {
      int counts[256] = {0};
      char mytext[200];
      int x;
      int i;
      char ch = 'z';
      int words = 0;
      int sentences = 0;
      int questions = 0;
      cout << "enter some text: ";
      fgets(mytext,200,stdin);////////////////NEVER USE gets.  And don't mix iostream with stdio.  I can't fix that without reading a little.
      x = strlen(mytext);
      cout << "you typed: " << mytext << endl;
      cout << "length: " << x << endl;
      for (i=0;i<=x-1;i++) {
        ch = mytext[i];
        switch(ch) {
        case ' ': // simplistic approach to word counting, but quite sufficient to get your code up and running.
          words++;
          break;
        case '.':
          sentences++;
          words++;
          break;
        case '?':
          questions++;
          sentences++;
          words++;
        }
      }
      cout << "Words: " << words << endl;
      cout << "Sentences: " << sentences << endl;
      cout << "questions: " << questions << endl;
      //while(ch != 'z') {//////////////////ch doesn't change, this loop will run until the end of electricity, or not at all.
      //  x = ch;         //////////////////I removed it.
      //  counts[x]++;
      //}
      for (i=65;i<=122;i++) {
        ch = i;
        cout << " * " << counts[i] << endl;
      }
    #ifdef SUCK_WAD
      system("pause");
    #endif
      return 0;
    }

    Comments on this post

    • dwise1_aol agrees : Indeed. A new-style C++ program that not only uses C input, but also C-style strings, neither of which are even mentioned in C++ books anymore. The only things C++ about it are namespace and cout. Curiously inconsistent.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo