Connecting Tech Pros Worldwide Forums | Help | Site Map

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

allenevaalleneva@gmail.com
Guest
 
Posts: n/a
#1: Oct 23 '06
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?


John Carson
Guest
 
Posts: n/a
#2: Oct 23 '06

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


<allenevaalleneva@gmail.comwrote in message
news:1161602596.893804.72620@f16g2000cwb.googlegro ups.com
Quote:
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


Osamede Zhang
Guest
 
Posts: n/a
#3: Oct 23 '06

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



allenevaalleneva@gmail.com wrote:
Quote:
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<intlike this:
#include<iostream>
#include<iterator>
using namespace std;
int main()
{
ostream_iterator<intnumOut(cout);
ostream_iterator<charspaceOut(cout);
int a[]={1,2,3,4};
for(int i=0;i<4;i++){
numOut++=a[i];
spaceOut++=' ';
}
}

red floyd
Guest
 
Posts: n/a
#4: Oct 23 '06

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


allenevaalleneva@gmail.com wrote:
Quote:
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::ostream&,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.
Jim Langston
Guest
 
Posts: n/a
#5: Oct 23 '06

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


<allenevaalleneva@gmail.comwrote in message
news:1161602596.893804.72620@f16g2000cwb.googlegro ups.com...
Quote:
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
Quote:
b)
{
std::cout << "1" << " 2" << " 3" << " 4";
system("PAUSE");
return 0;
}
Fine.
Quote:
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
Quote:
i don't know this answer is right or not
if right
how can be more simple for this question?

Closed Thread