Thread: Is this programming exercise possible (don't answer with code, just is it possible?)

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    161

    Is this programming exercise possible (don't answer with code, just is it possible?)

    Okay, I asked this over in C++ forum, but I think it's just too much C for them there.
    Maybe you guys can answer better.
    In C++ Primer Plus, Chapter 5 question 8 it asks to:

    Write a program that uses an array of char and a loop to read one word at a time
    until the word ‘done’ is entered. The program should then report the number of
    words entered (not counting done). A sample run could look like this:


    Enter words (to stop, type the word done):
    anteater birthday category dumpster
    envy finagle geometry done for sure
    You entered a total of 7 words.


    You should include the cstring header file and use the strcmp() function to
    make the comparison test.
    I can get it, but it's cheating basically because I'm using && and that hasn't been covered yet. Neither has if/else.
    So basically only do-while, while, and for statements have been covered.
    So is the solution to this problem doable given the limited tools?

    working code:

    Code:
    #include <iostream>
    #include <cstring>
    
    
    using namespace std;
    
    
    int main() {
        int x,y = 0,done = 1;
        unsigned long z = 0;
        int Numwords = 0;
        int set = 0;
        char temp_input_line[80];
        char current_word [80] = "";
        
        do {
            cin.getline(temp_input_line,79);
            x=0;  // Set beginning array count to 0
            z = strlen(temp_input_line);
            while (x <= z ){        //While less than or equal to string lenght
                while (temp_input_line[x] != ' ' && x <= z){        //Check for Space or end of string
                    current_word [y] = temp_input_line [x];
                    x++;
                    y++;
                    set = done;                                //Set Flag that word started
                }
                x++;
                                                        //End check for space or string end
                Numwords += set;                            //Increase word count
                set = 0;  //Reset word count flag
                current_word [y + 1] = '\0';
                y = 0;
                done = done && (strcmp("done",current_word));             //If 'done' found, result is 0
            }                                       //End check less than string length
        }while (done);
        
        cout << "Number of words is : " << Numwords - 1 << endl;
        
        
        
        
        
        
        
        
        return 0;
    }


    Output:

    one two three four
    five six done eight nine ten
    Number of words is : 6

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Of course it's possible.

    You can start by actually using C instead of C++.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by Salem View Post
    Of course it's possible.

    You can start by actually using C instead of C++.

    This is C except for the cout.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    cout along with:
    Code:
    #include <iostream>
    #include <cstring>
     
    using namespace std;
    // ...
    cin.getline(temp_input_line,79);
    You need to replace them with the appropriate C header files, and C I/O functions.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by rstanley View Post
    cout along with:
    Code:
    #include <iostream>
    #include <cstring>
     
    using namespace std;
    // ...
    cin.getline(temp_input_line,79);
    You need to replace them with the appropriate C header files, and C I/O functions.

    I'm going to deem this question not possible to answer with the tools supplied and put it in my errata sheet, and move on. None of these replies deal with the logic of answering the question.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by FloridaJo View Post
    I'm going to deem this question not possible to answer with the tools supplied and put it in my errata sheet, and move on. None of these replies deal with the logic of answering the question.
    The simplest implementation I can think of is:
    Code:
    read a word
    while (word is not "done") {
       increase the count
       read another word
    }
    print the count
    Sorry if you felt we weren't helping. You only need to read one word at a time though, and you weren't doing that, so everything was more complicated than it really needed to be. Additionally, sorry if you think this is a handout.
    Last edited by whiteflags; 10-03-2017 at 06:06 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by FloridaJo View Post
    I'm going to deem this question not possible to answer with the tools supplied and put it in my errata sheet, and move on. None of these replies deal with the logic of answering the question.
    Your house is built on sand.
    I was fixing your foundations before helping you to fix the walls.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by whiteflags View Post
    The simplest implementation I can think of is:
    Code:
    read a word
    while (word is not "done") {
       increase the count
       read another word
    }
    print the count
    Sorry if you felt we weren't helping. You only need to read one word at a time though, and you weren't doing that, so everything was more complicated than it really needed to be. Additionally, sorry if you think this is a handout.

    This doesn't take into account multiple lines entered.
    It is read a line, read the words in that line, then read another line IF 'done' is not found.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by Salem View Post
    Your house is built on sand.
    I was fixing your foundations before helping you to fix the walls.

    sorry, but it looked like you were nit picking.
    So to understand what you are saying is this can't be done in C++, only C?
    I don't think so.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    No, he's not telling you it can't be done in C++ or C. He's telling you if you want to try to solve the problem in C then use C constructs not C++ constructs.

    This problem can be easily solved in either C or C++. You just need to arrange the logic elements in the proper order.

    Also the book you're trying to use is not a very good book if you want to learn C++, IMO the design of this book is not really the best way to learn C++. Also, IMO, relying only on "covered" material is not really the best way to learn any language, use what you know as well.

    Jim

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If it can be done in any Turing-complete language, it can be done in ALL Turing-complete languages.

    People write things like operating systems and web browsers in C and C++.
    Makes me wonder why the OP should thing why this small problem might be impossible - impossible only to someone with limited skills perhaps.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by Salem View Post
    If it can be done in any Turing-complete language, it can be done in ALL Turing-complete languages.

    People write things like operating systems and web browsers in C and C++.
    Makes me wonder why the OP should thing why this small problem might be impossible - impossible only to someone with limited skills perhaps.

    Obviously you didn't read the post completely.
    Let me repeat for you.
    Using only do-while, while, and or for statements and not using && II or any if-else statements. ONLY using what has been learned up until this moment in the book which is summarized above; this question is not able to be programmed with the ABOVE stated tools.
    Now, what the hell does Turing have to do with this post?
    Maybe instead of speed reading maybe try comprehensive reading.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by FloridaJo View Post
    Using only do-while, while, and or for statements and not using && II or any if-else statements. ONLY using what has been learned up until this moment in the book which is summarized above;
    One problem is that we don't know what has been taught up to that point in the book. I doubt only loops have been covered over the course of five chapters.

    cin >> reads characters until whitespace is encountered. Maybe this will prove useful.
    strcmp() returns a value that can be used to control a loop.

    Does that inspire any ideas?

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by Matticus View Post
    One problem is that we don't know what has been taught up to that point in the book. I doubt only loops have been covered over the course of five chapters.
    ...
    This may give one an idea. Steve is fairly verbose which I like. He explains thing via many different angles.

    Is this programming exercise possible (don't answer with code, just is it possible?)-img_1831-jpg
    Is this programming exercise possible (don't answer with code, just is it possible?)-img_1832-jpg
    Is this programming exercise possible (don't answer with code, just is it possible?)-img_1833-jpg

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by FloridaJo View Post
    This may give one an idea. Steve is fairly verbose which I like. He explains thing via many different angles.
    So did the suggestions I offered give you any ideas for solving this problem?
    The entire source for my solution is 20 lines (including declarations and whitespace).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 10-01-2017, 03:55 PM
  2. Need help with C programming exercise.
    By dnguyen8 in forum C Programming
    Replies: 7
    Last Post: 10-08-2013, 08:05 PM
  3. C Programming Language Exercise
    By serg_yegi in forum C Programming
    Replies: 17
    Last Post: 11-30-2010, 04:36 AM
  4. c programming exercise
    By Pulock2009 in forum C Programming
    Replies: 3
    Last Post: 10-30-2009, 02:48 AM
  5. programming exercise
    By mashour06 in forum C Programming
    Replies: 1
    Last Post: 06-01-2009, 06:22 AM

Tags for this Thread