C++ Accelerated Questions Chapter 2.

This is a discussion on C++ Accelerated Questions Chapter 2. within the C++ Programming forums, part of the General Programming Boards category; C++ Accelerated Questions Chapter 2. 2-0 Compile and run the program presented in this chapter. Done. 2-1 Change the framing ...

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

    C++ Accelerated Questions Chapter 2.

    C++ Accelerated Questions Chapter 2.

    2-0 Compile and run the program presented in this chapter.
    Done.

    2-1 Change the framing program so that it writes its greeting with no separation from the frame.
    Code:
    #include<iostream>
    #include<string>
    
    using std::cin;         using std::endl;
    using std::cout;        using std::string;
    
    int main()
    {
        // Ask the name of the person.
        cout << "Please enter your name here: ";
    
        // Read the name.
        string name;
        cin >> name;
    
        // Build the greeting.
        const string greeting = "*Hello, " + name + "!*";
    
        // The amount of stars to be printed above and beneath the greeting.
        const string::size_type padding = greeting.size();
    
        // Print the first line of stars.
        for(int unsigned r = 0; r < padding; r++)
        {
            cout << "*";
        }
    
        // Print the greeting.
        cout << endl << greeting << endl;
    
        // Print the second line of stars.
        for(int unsigned r = 0; r < padding; r++)
        {
            cout << "*";
        }
    
        return 0;
    }
    2-2 Change the framing program so that it uses a different amount of space to separate the sides from the greeting than it uses to separate the top and bottom borders from the greeting.
    Code:
    #include<iostream>
    #include<string>
    
    using std::cout;        using std::cin;
    using std::endl;        using std::string;
    
    int main()
    {
        // Ask the name of the person.
        cout << "Please enter your name: ";
    
        // Read the name.
        string name;
        cin >> name;
    
        // Build the greeting.
        const string greeting = "Hello, " + name + "!";
    
        // Set the ammount of spacing on the middle line between the stars and the greeting.
        const int spacer = 10;
    
        // Set the amount of space for the padding.
        const int pad = 2;
    
        // The amount of stars to be printed above and beneath the greeting (cols) and tbe amount of rows.
        const string::size_type cols = greeting.size() + spacer * 2 + 2;
        const int rows = pad * 2 + 3;
    
        // Write a blank line to seperate input from output.
        cout << endl;
    
        // Write rows of output.
        for(int r = 0; r != rows; ++r)
        {
            string::size_type c = 0;
    
            while(c != cols)
            {
                // Is it time to write the greeting?
                if(r == pad + 1 && c == spacer +1)
                {
                    cout << greeting;
                    c += greeting.size();
                }
                else
                {
                    // Are we on the border?
                    if(r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
                        cout << "*";
                    else
                    cout << " ";
                        ++c;
                }
            }
            cout << endl;
        }
        return 0;
    }
    2-3 Rewrite the framing program to ask the user to supply the amount of spacing to leave between the frame and the greeting.
    Code:
    #include<iostream>
    #include<string>
    
    using std::cout;        using std::cin;
    using std::endl;        using std::string;
    
    int main()
    {
        // Ask for the persons name.
        cout << "Please enter your name: ";
    
        // Get and store the name.
        string name;
        cin >> name;
    
        // Ask for the amount of spacing between the greeting and the frame.
        cout << endl << "Now enter the amount of spacing you want: ";
    
        // Get and store the spacing.
        unsigned int spacing;
        cin >> spacing;
    
        // Build the greeting.
        const string greeting = "Hello, " + name + "!";
    
        // The number of rows and columns to write.
        const unsigned int rows = spacing * 2 + 3;
        const string::size_type cols = greeting.size() + spacing * 2 + 2;
    
        // Write a blank line to seperate input from output
        cout << endl;
    
        // Start writing the output.
        for(unsigned int r = 0; r != rows; r++)
        {
            string::size_type c = 0;
    
            while(c != cols)
            {
                // Is it time to write the greeting.
                if(r == spacing + 1 && c == spacing + 1)
                {
                    cout << greeting;
                    c += greeting.size();
                }
                else
                {
                    if(r == 0 || r == rows -1 || c == 0 || c == cols -1)
                    cout << "*";
                    else
                    cout << " ";
                    ++c;
                }
            }
            cout << endl;
        }
        return 0;
    }
    2-4 The framing program writes the mostly blank lines that separate the borders from the greeting one character at a time. Change the program so that it writes all the spaces needed in a single output expression.
    I need some help with this one. If someone could give me a hint on how to do this it would be great.

    2-5 Write a set of "*" characters so that they form a square, a rectangle and a triangle.
    Note: There must be a more easy way to draw the triangle at least. Could someone give me a hint on that one as well?

    The square:
    Code:
    #include<iostream>
    #include<string>
    
    using std::cout;        using std::endl;
    using std::string;
    
    int main()
    {
        // The square.
        const string::size_type x = 10;         // Amount of stars.
    
    
        for(unsigned int r = 0; r != x; r++)
        {
            cout << endl;
            string::size_type c = 0;
    
            while(c != x)
            {
                cout << "*";
                ++c;
            }
        }
        return 0;
    }
    The rectangle:
    Code:
    #include<iostream>
    #include<string>
    
    using std::cout;        using std::endl;
    using std::string;
    
    int main()
    {
        const string::size_type x = 30;    // Lenght.
        const string::size_type p = 5;     // Height.
    
        for(unsigned int y = 0; y != p; y++)
        {
            cout << endl;
    
            for(unsigned int c = 0; c != x; c++)
            {
                cout << "*";
            }
        }
        return 0;
    }
    The triangle:
    Code:
    #include<iostream>
    #include<string>
    
    using std::cout;    using std::endl;
    using std::string;
    
    int main()
    {
        string::size_type c = 5;
    
            if(c == 5)
            {
                cout << "     *";
                cout << endl;
                c--;
            }
            if(c == 4)
            {
                cout << "    ***";
                cout << endl;
                c--;
            }
            if(c == 3)
            {
                cout << "   *****";
                cout << endl;
                c--;
            }
            if(c == 2)
            {
                cout << "  *******";
                cout << endl;
                c--;
            }
            if(c == 1)
            {
                cout << " *********";
                cout << endl;
                c--;
            }
        return 0;
    }
    2-6 What does the following code do?
    Code:
    int i=0;
    while(i < 10){
    i += 1;
    std::cout << i << std::endl;
    }
    It creates the variable "i" the variable is set to be an integer and the value is "0". It then looks if "i" is smaller then "10" and if so it adds "1" to the variable "i". After that it prints the value of "i" and goes back to the beginning of the "while" loop untill the returned value is false.

    2-7 Write a program to count down from 10 to -5.
    Code:
    #include<iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
        signed int x = -6;
        signed int i = 10;
    
        while(i != x)
        {
            cout << i << endl;
            i--;
        }
        return 0;
    }
    2-8 Write a program to generate the product of the numbers in the range [1,10].
    Code:
    #include<iostream>
    
    using std::cout;        using std::string;
    using std::endl;
    
    int main()
    {
        for(unsigned int r = 1; r != 11; r++)
        {
            for(unsigned int x = 1; x != 11; x++)
            {
                unsigned int y = r * x;
                cout << r << "*" << x << "=" << y << endl;
            }
        }
    }
    2-9 Write a program that asks the user to enter two numbers and tells the user which number us larger than the other.
    Code:
    #include<iostream>
    #include<string>
    
    using std::cout;        using std::cin;
    using std::endl;        using std::string;
    
    int main()
    {
        // Ask for the first number.
        cout << "Enter the first number here: ";
        string::size_type number1;
        cin >> number1;
    
        // Ask for the second number.
        cout << "Please enter the second number here: ";
        string::size_type number2;
        cin >> number2;
    
        if(number1 > number2)
        cout << "Number 1 is bigger then number 2.";
        else if(number1 == number2)
        cout << "Number 1 and number 2 are the same.";
        else
        cout << "Number 2 is bigger then number 1.";
    
        return 0;
    }
    2-10 Explain each of the uses of std:: in the following program.
    Code:
    int main(){
    int k = 0;
    while (k != n){		//invariant: we have written k asterisks so far
    using std::cout;
    cout << "*";
    ++k;
    }
    std::cout << std::endl;		// std::is required here
    return 0;
    }
    At first std:: is used to tell the program that the call to cout will be part of the standard library. In the second use it tells that the call to endl is part of the standard library.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Quote Originally Posted by Kitt3n View Post
    At first std:: is used to tell the program that the call to cout will be part of the standard library. In the second use it tells that the call to endl is part of the standard library.
    No. The first imports the "symbol" (or object if you will) into the global namespace.
    The second explicitly tells the compiler to use the symbol cout which resides in the std namespace.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Quote Originally Posted by Kitt3n
    2-5 Write a set of "*" characters so that they form a square, a rectangle and a triangle.
    Really confused why you include the string header and then don't really use any string objects. Other than your use of string::size_type...

    ...anyway, if you're going to include the string header you might as well take advantage of an actual string container and its various constructors. If you wanted to, you could make use of the constructor that creates a string object of a given length/character. To shorten the triangle code for example you could do this:
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        for( int i = 0; i < 5; ++i)
            std::cout << std::string(5-i,' ') << std::string(i*2+1,'*') << std::endl;
    
        return 0;
    }
    The same thing can be done with the square/rectangle programs to turn those nested loops constructs into a single for-loop each.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    62
    @Elysia
    Thanks. I understood what they meant but I had no idea how to say it correctly.

    @hk_mp5kpdw
    The example code you are using it a bit confusing for me. The function you are using std::string() has not been mentioned yet in my book so I do not understand how it would execute.

    Next to that could someone also look at exercise 2.4 and then again at 2.5 keeping in mind that all I have learned now are loops, std::cout, std::cin, std::endl, std::string (to create a string to store for example a name in), std::string::size_type and .sizeof().

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    @hk_mp5kpdw
    The example code you are using it a bit confusing for me. The function you are using std::string() has not been mentioned yet in my book so I do not understand how it would execute.

    Next to that could someone also look at exercise 2.4 and then again at 2.5 keeping in mind that all I have learned now are loops, std::cout, std::cin, std::endl, std::string (to create a string to store for example a name in), std::string::size_type and .sizeof().
    What? You learned about std::string but that you don't know what std::string() is? Sounds like you need to bone up on all the different constructors provided by the string class. Given what you "learned" about std::string, nothing in what I posted above would be outside your realm of knowledge.

    The string header defines a container/object called string that represents an array of characters. You included the header but really didn't use any functionality of said objects. std::string() is a constructor for said string objects. There are many overloaded constructors to create a string object in a myriad of different ways. The particular constructor I used above is the one which creates a string of a certain length and consisting solely of a specific character. Thus, for example, std::string(10,'a') would create a string object representing "aaaaaaaaaa". I used that particular constructor because it eliminates the need for nested loops just to show what could be done. In the example above I used some math in the constructor to determine the number of characters the strings were to contain which duplicated the number of spaces and stars '*' to be displayed on each line as in your original post. My post above creates two temp string objects, the first consisting of a given number of spaces ' ' followed by the second which consists of a given number of stars '*'.

    As a further demonstration, 2.4 could be done as such:
    Code:
    // Ask for the persons name.
    cout << "Please enter your name: ";
    
    // Get and store the name.
    string name;
    cin >> name;
    
    // Ask for the amount of spacing between the greeting and the frame.
    cout << endl << "Now enter the amount of spacing you want: ";
    
    // Get and store the spacing.
    unsigned int spacing;
    cin >> spacing;
    
    // Build the greeting.
    const string greeting = '*' + std::string(spacing,' ') + "Hello, " +
                            name + '!' + std::string(spacing,' ') + '*';
    
    // Write a blank line to seperate input from output
    cout << endl;
    
    for( int i = 0; i < spacing * 2 + 3; ++i )
    {
        if( i == 0 || i == spacing * 2 + 2 )
        {
            cout << string(greeting.length(),'*') << endl;
        }
        else if(i == spacing+1)
        {
            cout << greeting << endl;
        }
        else
        {
            cout << '*' << string(greeting.length()-2,' ') << '*' << endl;
        }
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Accelerated Questions Chapter 0.
    By Kitt3n in forum C++ Programming
    Replies: 23
    Last Post: 07-27-2010, 02:24 PM
  2. C++ Accelerated Questions Chapter 1.
    By Kitt3n in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2010, 06:23 PM
  3. malloc problem
    By Tool in forum C Programming
    Replies: 23
    Last Post: 03-12-2010, 09:54 AM
  4. 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
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21