Thread: C++ Accelerated Questions Chapter 0.

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    62

    C++ Accelerated Questions Chapter 0.

    As stated in an earlier topic I am trying to follow the exercises in this book. However the answers to the questions seem to be missing and can not be found online either (at least not to the majority of them). I am hoping some one can look my answers over for me.

    Chapter 0.
    0.0 Compile and run the "hello world" program.
    Done.
    0.1 What does the following statement do?
    Code:
    3+4;
    It adds the number 4 to the number 3.
    0.2 Write a program that, when run, writes: "This is a quote (") and this is a backslash (\).
    Code:
    #include<iostream>
    
    int main()
    {
        std::cout << "This (\") is a quote, and this (\\) is a backslash." << std::endl;
    
        return 0;
    }
    0.3 The string literal "\t" represents a tab character; different C++implementations display tabs in different ways. Experiment with your implementation to learn how it treats tabs.
    Done.
    0.4 Write a program that, when run, writes the "hello, world!" program as its output.
    Code:
    #include<iostream>
    
    int main()
    {
        std::cout << "Hello world!" << std::endl;
    
        return 0;
    }
    0.5 Is this a valid program? Why or why not?
    Code:
    #include<iostream>
    int main() 
    std::cout << "Hello, world!" << std:: endl;
    No, the curly braces ({}) are missing in the main function.
    0.6 Is this a valid program? Why or why not?
    Code:
    #include<iostream>
    int main()
    {{{{{
    std::cout << "Hello, world!" << std:: endl;
    }}}}}
    Yes, the program can be build. However the great amount of curly braces ({}) makes it hard to read.
    0.7 What about this one?
    Code:
    #include<iostream>
    int main()
    {
    /* This is a comment that extends over several lines
    because it uses /* and */ as its starting and ending delimiters */
    std::cout << "Does this work?" << std::endl;
    return 0;
    }
    No, the comment stops between the words "and" and "as".
    0.8 And this one?
    Code:
    #include<iostream>
    int main()
    {
    // This is a comment that extends over several lines
    // by using // at the beginning of each line instead of using /* 
    // or */ to delimit comments.
    std::cout << "Does this work?" << std::endl;
    return 0;
    }
    Yes this will work. The "//" will comment out everything places behind it on that line.
    0.9 What is the shortest valid program?
    Code:
    #include<iostream>
    int main()
    {
    3+4;
    }
    0.10 Rewrite the "Hello, world!" program so that a new line occurs everywhere that white space is allowed in the program.
    Code:
    #include<iostream>
    
    int main()
    {
        std::cout << "Hello" << std::endl;
        std::cout << "World!" << std::endl;
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Well, except for 0.2, which is correct but the order of words, you only have one mistake here: your shortest program can be shorter. Try it. Why do you even have the include, you don't need it? And you don't even need that "3+4;" either, it does nothing at all... Try again and post it here.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by EVOEx View Post
    Well, except for 0.2, which is correct but the order of words, you only have one mistake here: your shortest program can be shorter. Try it. Why do you even have the include, you don't need it? And you don't even need that "3+4;" either, it does nothing at all... Try again and post it here.
    I thought the program had to do something in its function but hope this is better. Nothing is included and no calculations are made. It is made out of nothing more then the main functions skeleton.
    Code:
    int main()
    {
    
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Kitt3n View Post
    I thought the program had to do something in its function but hope this is better. Nothing is included and no calculations are made. It is made out of nothing more then the main functions skeleton.
    Code:
    int main()
    {
    
    }
    Exactly. If you really want it the smallest you could also remove all whitespace. Try it; not that it matters but just so you can understand which whitespaces are mandatory and which can be left out.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by EVOEx View Post
    Exactly. If you really want it the smallest you could also remove all whitespace. Try it; not that it matters but just so you can understand which whitespaces are mandatory and which can be left out.
    I got it. For what I understood white space is meant for formatting the code to majke it more easy to read. So.
    Code:
    int main(){}
    Could you also take a look at the next chapter that I posted. It would be nice for other people as well that try to use google to look for the answers to find a place where someone already overlooked all the answers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Kitt3n View Post
    I got it. For what I understood white space is meant for formatting the code to majke it more easy to read. So.
    Code:
    int main(){}
    Could you also take a look at the next chapter that I posted. It would be nice for other people as well that try to use google to look for the answers to find a place where someone already overlooked all the answers.
    Perfect. That is, indeed, the very shortest legal program that can be written. Some would say "main(){}" would be valid; while according to some standards it is, it's not in C++.

    I've already answered your chapter 1 questions.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Is the stuff in the code block for 0.4 the output of your program, or is it the program itself?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by pianorain View Post
    Is the stuff in the code block for 0.4 the output of your program, or is it the program itself?
    It is the program itself.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But the description asks you to write a program that writes the code for the Hello World program, not a program that writes Hello World.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by CornedBee View Post
    But the description asks you to write a program that writes the code for the Hello World program, not a program that writes Hello World.
    I got the question wrong then. I will rewrite it and post the corrected version in this topic later. I am currently trying to solve how to code a triangle using loops in the 3th chapter.

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    0.1 What does the following statement do?
    Code:
    3+4;
    I would argue that this code does nothing, as EVO said. So your answer is wrong. After all, if I tell you "3+4", what am I telling you to do? Nothing. It's as if I say "platypus."
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by CodeMonkey View Post
    I would argue that this code does nothing, as EVO said. So your answer is wrong. After all, if I tell you "3+4", what am I telling you to do? Nothing. It's as if I say "platypus."
    I thought it added 3 to 4 but did not store the result in a variable but that it would still do the calculation.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Kitt3n View Post
    I thought it added 3 to 4 but did not store the result in a variable but that it would still do the calculation.
    In the strict interpretation of the standard, it will. In reality, compilers will simply remove the calculation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by CornedBee View Post
    But the description asks you to write a program that writes the code for the Hello World program, not a program that writes Hello World.
    Is this better?
    Code:
    #include<iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
    cout << "#include<iostream>" << endl << endl;
    cout << "int main()" << endl;
    cout << "{" << endl;
    cout << "std::cout << \"Hello, world!\"; << std::endl" << endl << endl;
    cout << "return 0;" << endl;
    cout << "}" << endl;
    
    return 0,
    }

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Except for indentation, looks correct. But you shouldn't use endl every time you want a newline. Use \n within the string literals instead. endl flushes the stream, and you probably don't want that. In fact, I use endl very rarely, because you very rarely need to flush a stream explicitly. (Note that reading from cin flushes the stream implicitly.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc problem
    By Tool in forum C Programming
    Replies: 23
    Last Post: 03-12-2010, 10:54 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM