8 Replies - 2392 Views - Last Post: 21 October 2011 - 04:51 AM Rate Topic: -----

#1 Eowyn27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 17-October 11

How do you print an array on a single line?

Posted 20 October 2011 - 02:48 AM

 
for(int i = num-1; i>=0; i--)
{
cout << "reverse: " << array[i] << ", " << endl;
}



I was wondering how do I print an array on a single line? That is, the above code gives me the numbers in a list fashion like this:

reverse: 3,
reverse: 2,
reverse: 4,

where as I just want it as:

reverse: 3,2,4

Thanks a lot in advance!!

Is This A Good Question/Topic? 0
  • +

Replies To: How do you print an array on a single line?

#2 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 536
  • Joined: 31-December 08

Re: How do you print an array on a single line?

Posted 20 October 2011 - 03:13 AM

have you tried
cout << "reverse: ";
for(int i = num-1; i>=0; i--)
{
cout << array[i] << ", ";
}
cout << endl;

This post has been edited by olibenu: 20 October 2011 - 03:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 Eowyn27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 17-October 11

Re: How do you print an array on a single line?

Posted 20 October 2011 - 03:23 AM

View Postolibenu, on 20 October 2011 - 03:13 AM, said:

have you tried
cout << "reverse: ";
for(int i = num-1; i>=0; i--)
{
cout << array[i] << ", " << endl;
}



Oh that works for the reverse but I wanted the numbers aligned horizontally not vertically.

Now it gives me:

reverse:
3,
2,
4,

instead of what I want:

reverse: 3,2,4

Thanks a lot again.

Also I noticed when I do:

 
cout << "reverse : , " ; 
for(int i = num-1; i>=0; i--)
{
cout << array[i];
}



It prints out like:

reverse: ,321

But I want it like:

reverse: 3,2,1 with commas in between each number.
Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 445
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: How do you print an array on a single line?

Posted 20 October 2011 - 04:09 AM

So

cout << "reverse : " ; 
for(int i = num-1; i>=1; i--)
{
cout << array[i] << ",";
}
cout << array[0] << endl; //without the ,



You should be able to figure things like this out.

This post has been edited by Karel-Lodewijk: 20 October 2011 - 04:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 4892
  • View blog
  • Posts: 11,287
  • Joined: 16-October 07

Re: How do you print an array on a single line?

Posted 20 October 2011 - 04:11 AM

If you print a comma behind the value every iteration of the loop except last one, it would work.
Was This Post Helpful? 0
  • +
  • -

#6 Eowyn27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 17-October 11

Re: How do you print an array on a single line?

Posted 20 October 2011 - 08:08 AM

View PostKarel-Lodewijk, on 20 October 2011 - 04:09 AM, said:

So

cout << "reverse : " ; 
for(int i = num-1; i>=1; i--)
{
cout << array[i] << ",";
}
cout << array[0] << endl; //without the ,



You should be able to figure things like this out.



Thanks for helping me out. When I run that code, it comes out like:

reverse: 3,2,4,4

repeating the last number without a comma. How do I not make it repeat the last number? ><'' And why does it repeat the last number in the first place? Thanks a bunch again!!
Was This Post Helpful? 0
  • +
  • -

#7 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 536
  • Joined: 31-December 08

Re: How do you print an array on a single line?

Posted 20 October 2011 - 08:23 AM

guy! can't you fix that?
Was This Post Helpful? 1
  • +
  • -

#8 WabiSabi  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 31-December 10

Re: How do you print an array on a single line?

Posted 20 October 2011 - 09:22 AM

View PostEowyn27, on 20 October 2011 - 09:08 AM, said:

How do I not make it repeat the last number? ><'' And why does it repeat the last number in the first place?


This makes it print... what?
cout << array[0] << endl;


So... what do you want it to NOT print?

How would you do that? //make it print ONE LESS thing that has a comma after it.

It's easy enough to figure out. If nothing else, use trial and error.
Was This Post Helpful? 0
  • +
  • -

#9 Eowyn27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 17-October 11

Re: How do you print an array on a single line?

Posted 21 October 2011 - 04:51 AM

View PostWabiSabi, on 20 October 2011 - 09:22 AM, said:

View PostEowyn27, on 20 October 2011 - 09:08 AM, said:

How do I not make it repeat the last number? ><'' And why does it repeat the last number in the first place?


This makes it print... what?
cout << array[0] << endl;


So... what do you want it to NOT print?

How would you do that? //make it print ONE LESS thing that has a comma after it.

It's easy enough to figure out. If nothing else, use trial and error.



Sorry! I'm not comfortable yet with the Syntax (first time learning and getting through this) but I did figure it out! Thanks!! :D I used an if statement for the , .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1