help me to test how can more simple for this C++ answer

Discussion in 'C++' started by allenevaalleneva@gmail.com, Oct 23, 2006.

  1. Guest

    Q:Write a program that prints the numbers 1 to 4 on the same line with
    with each pair of adjacent numbers separated by one space. Do this
    several ways:
    a) Using one statement with one stream insertion operator.
    b) Using one statement with four stream insertion operators.
    c) Using for statement

    my answers are as follow

    a)
    {
    std::cout << "1 2 3 4";
    system("PAUSE");
    return 0;
    }



    b)
    {
    std::cout << "1" << " 2" << " 3" << " 4";
    system("PAUSE");
    return 0;
    }



    c)
    {
    std::cout << "1";
    cout << " 2";
    cout << " 3";
    cout << " 4";
    system("PAUSE");
    return 0;
    }


    i don't know this answer is right or not
    if right
    how can be more simple for this question?
    , Oct 23, 2006
    #1
    1. Advertising

  2. John Carson Guest

    <> wrote in message
    news:
    > Q:Write a program that prints the numbers 1 to 4 on the same line with
    > with each pair of adjacent numbers separated by one space. Do this
    > several ways:
    > a) Using one statement with one stream insertion operator.
    > b) Using one statement with four stream insertion operators.
    > c) Using for statement
    >
    > my answers are as follow
    >
    > a)
    > {
    > std::cout << "1 2 3 4";
    > system("PAUSE");
    > return 0;
    > }
    >
    >
    >
    > b)
    > {
    > std::cout << "1" << " 2" << " 3" << " 4";
    > system("PAUSE");
    > return 0;
    > }
    >
    >
    >
    > c)
    > {
    > std::cout << "1";
    > cout << " 2";
    > cout << " 3";
    > cout << " 4";
    > system("PAUSE");
    > return 0;
    > }
    >
    >
    > i don't know this answer is right or not
    > if right
    > how can be more simple for this question?


    a) and b) are fine. c) does not use a for statement, so is incorrect.


    --
    John Carson
    John Carson, Oct 23, 2006
    #2
    1. Advertising

  3. wrote:
    > c)
    > {
    > std::cout << "1";
    > cout << " 2";
    > cout << " 3";
    > cout << " 4";
    > system("PAUSE");
    > return 0;
    > }
    >
    >
    > i don't know this answer is right or not
    > if right
    > how can be more simple for this question?

    Question 3 may be implemented use ostream_iterator<int> like this:
    #include<iostream>
    #include<iterator>
    using namespace std;
    int main()
    {
    ostream_iterator<int> numOut(cout);
    ostream_iterator<char> spaceOut(cout);
    int a[]={1,2,3,4};
    for(int i=0;i<4;i++){
    numOut++=a;
    spaceOut++=' ';
    }
    }
    Osamede Zhang, Oct 23, 2006
    #3
  4. red floyd Guest

    wrote:
    > Q:Write a program that prints the numbers 1 to 4 on the same line with
    > with each pair of adjacent numbers separated by one space. Do this
    > several ways:
    > a) Using one statement with one stream insertion operator.
    > b) Using one statement with four stream insertion operators.
    > c) Using for statement
    >
    > my answers are as follow
    >
    > a)
    > {
    > std::cout << "1 2 3 4";
    > system("PAUSE");
    > return 0;
    > }
    >
    >
    >
    > b)
    > {
    > std::cout << "1" << " 2" << " 3" << " 4";
    > system("PAUSE");
    > return 0;
    > }
    >
    >
    >
    > c)
    > {
    > std::cout << "1";
    > cout << " 2";
    > cout << " 3";
    > cout << " 4";
    > system("PAUSE");
    > return 0;
    > }
    >
    >
    > i don't know this answer is right or not
    > if right
    > how can be more simple for this question?
    >


    None of your answers are correct, since none of them are programs.

    You are missing main, you are missing the necessary #includes for
    std::cout and operator<<(std::eek:stream&,const char *).

    In addition, I believe (but I can't find chapter&verse) that if you
    don't flush/endl an ostream, your output is undefined -- you may not see
    anything.
    red floyd, Oct 23, 2006
    #4
  5. Jim Langston Guest

    <> wrote in message
    news:...
    > Q:Write a program that prints the numbers 1 to 4 on the same line with
    > with each pair of adjacent numbers separated by one space. Do this
    > several ways:
    > a) Using one statement with one stream insertion operator.
    > b) Using one statement with four stream insertion operators.
    > c) Using for statement
    >
    > my answers are as follow
    >
    > a)
    > {
    > std::cout << "1 2 3 4";
    > system("PAUSE");
    > return 0;
    > }


    Fine

    > b)
    > {
    > std::cout << "1" << " 2" << " 3" << " 4";
    > system("PAUSE");
    > return 0;
    > }


    Fine.

    > c)
    > {
    > std::cout << "1";
    > cout << " 2";
    > cout << " 3";
    > cout << " 4";
    > system("PAUSE");
    > return 0;
    > }


    Nope. Don't see a for statement here.

    for ( int i = 1; i <= 4; ++i )
    // do your std::cout << here

    > i don't know this answer is right or not
    > if right
    > how can be more simple for this question?
    Jim Langston, Oct 23, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    822
    Daniel Frey
    Jan 12, 2005
  2. Michael
    Replies:
    4
    Views:
    395
    Matt Hammond
    Jun 26, 2006
  3. Greg Corradini

    Simple mx.ODBC prob seeks simple answer

    Greg Corradini, Apr 6, 2007, in forum: Python
    Replies:
    0
    Views:
    304
    Greg Corradini
    Apr 6, 2007
  4. Steve Holden
    Replies:
    1
    Views:
    395
    Dennis Lee Bieber
    Apr 6, 2007
  5. Kirk

    Simple Question = simple answer?

    Kirk, Sep 23, 2003, in forum: Javascript
    Replies:
    1
    Views:
    153
    Martin Honnen
    Sep 23, 2003
Loading...

Share This Page