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

A

allenevaalleneva

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?
 
J

John Carson

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.
 
O

Osamede Zhang

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++=' ';
}
}
 
R

red floyd

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.
 
J

Jim Langston

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top