Thread: Is my solving to the homework true?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Question Is my solving to the homework true?

    Hi there,

    I would like to ask you whether if my answer to the homework is right or not?

    The homework requirment is:

    Delivery method
    Please archive and compress your file(s) using the tar command
    pa6.tgz (all lowercase) for your compress and archived file and
    account at the email account for the class. Attach this file to the
    class account. Put PA6 in the subject field of the email and your
    email. Follow the guidelines for program style and electronic
    the web. You must adhere to this policy to receive credit for
    archive and compress your files. It is assumed that you are at
    have logged on to Ouray, and that all your files are on the current
    tar –zcvf pa6.tgz pa6.cpp pa6functions.h pa6functions.
    Program objectives
    The objectives of this assignment are as follows.
    1. Test your ability to design software and/or hardware to meet desired needs
    (measurable outcome (d)).
    2. Test your ability to identify, formulate, and solve computer
    engineering problems (measurable outcome (f)).

    Problem
    Write a program that prompts for and gets a sentence from the user, parses the sentences
    into words, finds the lexicographically largest and smallest words in the sentence and
    computes the frequency of the characters in the string.

    Input
    A string (possibly with space characters and punctuation) entered from the keyboard.

    Output
    The original string, the formatted list of the words in the string, the lexicographically
    largest and smallest words in the string and a list of the frequency of occurrence of the
    alphabetic characters in the string.

    Program requirements
    1. Present the user with a greeting.
    2. Prompt the user and get a string from the keyboard.
    3. Parse the string into words and store the words in an array.
    4. Compute the largest and smallest words in the string.
    5. Display the following with annotations.
    a. The original sentence.
    b. The individual words.
    c. The smallest and largest words in the string.
    d. The frequency of occurrence of the alphabetic characters. The formatting
    for this part of the display should look like this.
    a 23
    b 13
    …..
    z 0
    The columns must be evenly spaced, so you will need to use an
    appropriate field width.

    Notes
    1. You must write global functions.
    2. The input string may contain uppercase and lowercase characters, spaces and
    possibly punctuation.

    What I did is:

    ----------------------------
    pa6.cpp
    ----------------------------

    Code:
    #include <iostream>
    #include <iomanip>
    #include "pa6functions.h"
    using namespace std;
    
    int main()
    {
    string str;
    pa6 mypa6;
    
    cout << "************************************"<<endl;
    cout << "       Program Assignment 6         "<<endl;
    cout << "************************************"<<endl;
    
    cout << "Please Write a sentence -> ";
    getline(cin,str);
    cout << "Count Word = " << mypa6.CountWord(str)<<endl;
    cout << "Count Smallest = " << mypa6.CountSmallest(str)<<endl;
    cout << "Count Largest = " <<
    mypa6.CountWord(str)-mypa6.CountSmallest(str) <<endl;
    // cout<<mypa6.CountLetter (str,'a')<<endl;
    cout << "-------------------" <<endl;
    cout << "| Letter |" << " | Count |"<<endl;
    cout << "-------------------" <<endl;
    for(int i=int('a'); i<=int('z');i++)
    cout << "|      "<<char(i)<<" |"<<"| "<<setw(5)<<setfill('0')<<mypa6.CountLetter(str,char(i))<<" |"<<endl;
    for(int i=int('A');i<=int('Z');i++)
    cout <<"|       "<<char(i)<<" |"<<"| "<<setw(5)<<setfill('0')<<mypa6.CountLetter(str,char(i))<<" |"<<endl;
    cout <<"|       "<<"."<<" |"<<"| "<<setw(5)<<setfill('0')<<mypa6.CountLetter(str,'.')<<" |"<<endl;
    
    return 0;
    }
    ----------------------------
    pa6functions.h
    ----------------------------

    Code:
    #include <string>
    using std::string;
    class pa6
    {
    public:
    
    int CountWord(string);
    int CountLetter(string,char);
    int CountSmallest(string);
    
    };
    ----------------------------
    pa6functions.cpp
    ----------------------------

    Code:
    #include <cctype>
    #include "pa6functions.h"
    
    int pa6::CountWord(string str)
    { int t=0;
    for(int i=0; i<str.length();i++)
    {if(str[i]==' ' && str[i+1]!=' ') t++;}
    if(str[0]==' ') t--;
    return (++t);
    }
    
    int pa6::CountSmallest(string str)
    {int t=0;
    for(int i=0; i<str.length(); i++)
    {if( isspace(str[i]) && (islower(str[i+1]) )) t++;}
    if(islower(str[0])) t++;
    
    return t;
    
    }
    
    int pa6::CountLetter(string str, char a)
    {
    int t=0;
    for(int i=0; i<str.length(); i++)
    if(str[i]==a) t++;
    
    return t;
    
    }
    ----------------------------------
    The output is
    ----------------------------------

    Code:
    [username@ouray ~]$ ./a.out
    ************************************
            Program Assignment 6         
    ************************************
    Please Write a sentence -> Today is a wonderful day!      
    Count Word = 5
    Count Smallest = 4
    Count Largest = 1
    -------------------
    | Letter | | Count |
    -------------------
    |       a || 00003 |
    |       b || 00000 |
    |       c || 00000 |
    |       d || 00003 |
    |       e || 00001 |
    |       f || 00001 |
    |       g || 00000 |
    |       h || 00000 |
    |       i || 00001 |
    |       j || 00000 |
    |       k || 00000 |
    |       l || 00001 |
    |       m || 00000 |
    |       n || 00001 |
    |       o || 00002 |
    |       p || 00000 |
    |       q || 00000 |
    |       r || 00001 |
    |       s || 00001 |
    |       t || 00000 |
    |       u || 00001 |
    |       v || 00000 |
    |       w || 00001 |
    |       x || 00000 |
    |       y || 00002 |
    |       z || 00000 |
    |       A || 00000 |
    |       B || 00000 |
    |       C || 00000 |
    |       D || 00000 |
    |       E || 00000 |
    |       F || 00000 |
    |       G || 00000 |
    |       H || 00000 |
    |       I || 00000 |
    |       J || 00000 |
    |       K || 00000 |
    |       L || 00000 |
    |       M || 00000 |
    |       N || 00000 |
    |       O || 00000 |
    |       P || 00000 |
    |       Q || 00000 |
    |       R || 00000 |
    |       S || 00000 |
    |       T || 00001 |
    |       U || 00000 |
    |       V || 00000 |
    |       W || 00000 |
    |       X || 00000 |
    |       Y || 00000 |
    |       Z || 00000 |
    |       . || 00000 |
    so please help me what's the missing?

    Thanks!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The specification seems to say a-z, not a-z and A-Z and '.'. You also didn't follow the formatting specified.

    Part of the task is to test how well you follow the specifications, so I would go over the specifications in detail and really understand what each one means and what the homework is asking for, then check each one with your code to see if you satisfy the requirement.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Quote Originally Posted by Daved View Post
    The specification seems to say a-z, not a-z and A-Z and '.'. You also didn't follow the formatting specified.

    Part of the task is to test how well you follow the specifications, so I would go over the specifications in detail and really understand what each one means and what the homework is asking for, then check each one with your code to see if you satisfy the requirement.
    Thank you for your fast responed, but the instructer later on told us:

    Please list the frequency of characters as one listing that groups uppercase and lowercase occurrences. Thus, the frequency of 'a' is for 'A' and 'a', etc.

    Thanks!

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Unhappy I need to get some help and some hint urgently. It's due after two hour !!

    My out put looks like this:

    Code:
    Please write a sentence -> This is a wonderful day!
    Count Word = 5
    Count Smallest = 4
    The Smallest : a
    Count largest = 1
    The largest : wonderful
    
     List Word :
    This
    is
    a
    wonderful
    day!
    
    -------------------
    | Letter || Count |
    -------------------
        a      0002
        b      0000
        c      0000
        d      0002
        e      0001
        f      0001
        g      0000
        h      0001
        i      0002
        j      0000
        k      0000
        l      0001
        m      0000
        n      0001
        o      0001
        p      0000
        q      0000
        r      0001
        s      0002
        t      0000
        u      0001
        v      0000
        w      0001
        x      0000
        y      0001
        z      0000
        A      0000
        B      0000
        C      0000
        D      0000
        E      0000
        F      0000
        G      0000
        H      0000
        I      0000
        J      0000
        K      0000
        L      0000
        M      0000
        N      0000
        O      0000
        P      0000
        Q      0000
        R      0000
        S      0000
        T      0001
        U      0000
        V      0000
        W      0000
        X      0000
        Y      0000
        Z      0000
        !      0001
        "      0000
        #      0000
        $      0000
        %      0000
        &      0000
        '      0000
        (      0000
        )      0000
        *      0000
        +      0000
        ,      0000
        -      0000
        .      0000
    -----------------
    How can I make it looks like this:

    Code:
    Enter a String -> I hope you work!! I am really sick of doing homework.
    
    You typed ->
    I hope you work!! I am really sick of doing homework.
    
    Your words separated are ->
    I              I
    am            doing
    homework      hope
    of            really
    sick          work
    you
    
      Smallest Word = I
        Largest Word = you
    
    Letters used and frequency amount ->
    Char    Frequent #
    A      2
    C      1
    D      1
    E      3
    F      1
    G      1
    H      2
    I      4
    K      3
    L      2
    M      2
    N      1
    O      7
    P      1
    R      3
    S      1
    U      1
    W      2
    Y      2
    What's missing please?

    Thanks!

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need to use tolower or toupper to convert a letter to lower or uppercase when counting it, and of course only look for all the lower case letters.

    As for removing the extra 0's in the output, you might try setting the fill to be ' ' instead of '0' with setfill.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Smile

    Quote Originally Posted by Daved View Post
    You need to use tolower or toupper to convert a letter to lower or uppercase when counting it, and of course only look for all the lower case letters.

    As for removing the extra 0's in the output, you might try setting the fill to be ' ' instead of '0' with setfill.
    Thank you very much for your fast answer:

    I got this out put

    Code:
    Please write a sentence -> This is a wonderful day
    Count Word = 5
    Count Smallest = 4
    The Smallest : a
    Count largest = 1
    The largest : wonderful
    
     List Word :
    This
    is
    a
    wonderful
    day
    
    -------------------
    | Letter || Count |
    -------------------
        a         2
        b         0
        c         0
        d         2
        e         1
        f         1
        g         0
        h         1
        i         2
        j         0
        k         0
        l         1
        m         0
        n         1
        o         1
        p         0
        q         0
        r         1
        s         2
        t         0
        u         1
        v         0
        w         1
        x         0
        y         1
        z         0
        A         0
        B         0
        C         0
        D         0
        E         0
        F         0
        G         0
        H         0
        I         0
        J         0
        K         0
        L         0
        M         0
        N         0
        O         0
        P         0
        Q         0
        R         0
        S         0
        T         1
        U         0
        V         0
        W         0
        X         0
        Y         0
        Z         0
        !         0
        "         0
        #         0
        $         0
        %         0
        &         0
        '         0
        (         0
        )         0
        *         0
        +         0
        ,         0
        -         0
        .         0
    -----------------
    How can I remove the zeros that aren't used in the sentence like what it was here in this out put

    Code:
    Enter a String -> I hope you work!! I am really sick of doing homework.
    
    You typed ->
    I hope you work!! I am really sick of doing homework.
    
    Your words separated are ->
    I              I
    am            doing
    homework      hope
    of            really
    sick          work
    you
    
      Smallest Word = I
        Largest Word = you
    
    Letters used and frequency amount ->
    Char    Frequent #
    A      2
    C      1
    D      1
    E      3
    F      1
    G      1
    H      2
    I      4
    K      3
    L      2
    M      2
    N      1
    O      7
    P      1
    R      3
    S      1
    U      1
    W      2
    Y      2
    and how can I add this thing in my out put

    Code:
    You typed ->
    This is a wonderful day!
    as it was in the previous out put.

    Thanks!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> How can I remove the zeros that aren't used in the sentence
    If the value is zero, don't output that line. That means you have to get the count first before you start writing out the rest of the line (your for loop will be a little longer).

    >> how can I add this thing in my out put
    Assuming you used getline to get the line, you have that whole line in a string. Just output that string.

    Also, after the other stuff is working, I'd suggest trying a sentence with a character that appears at least 10 times to make sure your spacing is correct for double digit numbers.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Quote Originally Posted by Daved View Post
    >> How can I remove the zeros that aren't used in the sentence
    If the value is zero, don't output that line. That means you have to get the count first before you start writing out the rest of the line (your for loop will be a little longer).

    >> how can I add this thing in my out put
    Assuming you used getline to get the line, you have that whole line in a string. Just output that string.

    Also, after the other stuff is working, I'd suggest trying a sentence with a character that appears at least 10 times to make sure your spacing is correct for double digit numbers.

    Can you please give me the commend for the orignal string?

    Thank you,

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What do you mean? In your code, you have the original string stored in the variable str. Just output that to cout.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Quote Originally Posted by Daved View Post
    What do you mean? In your code, you have the original string stored in the variable str. Just output that to cout.
    Thank you very much Mr. Daved,

    Do you think now evreything is correct and I met the requiremnet in the homework?

    Code:
    Please write a sentence -> This is a wonderful day!
    The original string is -> This is a wonderful day!
    Count Word = 5
    Count Smallest = 4
    The Smallest : a
    Count largest = 1
    The largest : wonderful
    
     List Word :
    This
    is
    a
    wonderful
    day!
    
    -------------------
    | Char || Frequent |
    -------------------
        a         2
        b         0
        c         0
        d         2
        e         1
        f         1
        g         0
        h         1
        i         2
        j         0
        k         0
        l         1
        m         0
        n         1
        o         1
        p         0
        q         0
        r         1
        s         2
        t         0
        u         1
        v         0
        w         1
        x         0
        y         1
        z         0
        A         0
        B         0
        C         0
        D         0
        E         0
        F         0
        G         0
        H         0
        I         0
        J         0
        K         0
        L         0
        M         0
        N         0
        O         0
        P         0
        Q         0
        R         0
        S         0
        T         1
        U         0
        V         0
        W         0
        X         0
        Y         0
        Z         0
        !         1
        "         0
        #         0
        $         0
        %         0
        &         0
        '         0
        (         0
        )         0
        *         0
        +         0
        ,         0
        -         0
        .         0
    -----------------
    Thank you for you help!

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Do you think now evreything is correct and I met the requiremnet in the homework?
    No, you are still outputting the capital letters. The counts for uppercase letters should be combined with lower case letters as the example and your instructor's comments explained. You are also outputting punctuation marks, but the original instructions only seem to mention alphabetic characters. You also didn't remove the rows that had a value of 0.

    There may be other issues, but those are just a few things that I see that have been mentioned in this thread.

    Fix each of those things on at a time, then when they are all fixed go through the original assignment text and see if you can verify each part of the assignment yourself.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Quote Originally Posted by Daved View Post
    >> Do you think now evreything is correct and I met the requiremnet in the homework?
    No, you are still outputting the capital letters. The counts for uppercase letters should be combined with lower case letters as the example and your instructor's comments explained. You are also outputting punctuation marks, but the original instructions only seem to mention alphabetic characters. You also didn't remove the rows that had a value of 0.

    There may be other issues, but those are just a few things that I see that have been mentioned in this thread.

    Fix each of those things on at a time, then when they are all fixed go through the original assignment text and see if you can verify each part of the assignment yourself.
    Thank you very much
    Last edited by Computer Guy; 04-01-2008 at 02:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Applied philosophy refresher
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 04-28-2009, 09:55 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Dostris (a complete game in 3 hours)
    By Jeremy G in forum Game Programming
    Replies: 8
    Last Post: 08-30-2003, 02:17 PM
  4. MCI CD Player
    By soutine in forum Windows Programming
    Replies: 0
    Last Post: 11-02-2001, 05:03 PM
  5. True or False Quiz (Need help)
    By Twiggy in forum C Programming
    Replies: 9
    Last Post: 10-12-2001, 04:25 AM