3 Replies - 589 Views - Last Post: 30 August 2010 - 11:58 PM Rate Topic: -----

#1 Guest_Coolio*


Reputation:

Printing Array

Posted 29 August 2010 - 06:29 PM

Hiya. How do you print an array like this:
123
234
345
456
....all the way to 10.

I tried using 2 for loops but keep printing a triangle.
for (int i=1; i <=10; i++)
{ 
    for ( int k=1; k<=3; k++)
      cout << a[k];

}



Is This A Good Question/Topic? 0

Replies To: Printing Array

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Printing Array

Posted 29 August 2010 - 06:53 PM

Not really clear, are the contents of the array numerical values where each subsequent entry has an increment of one for reach digit?
Was This Post Helpful? 0
  • +
  • -

#3 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Re: Printing Array

Posted 29 August 2010 - 06:55 PM

I believe you want to replace the second for loop with for ( int k=i; k<=3; k++). Also, in the first for loop, if you want it to print everything in the array, you want to initialize i to 0, because array indexes start at 0, not 1.
Was This Post Helpful? 0
  • +
  • -

#4 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Printing Array

Posted 30 August 2010 - 11:58 PM

Your array has four rows and three columns
Your first loop controls the rows and your second loop controls the columns
So as mentioned above the end condition for the first loop should be <3
The end condition for second loop should be <4.
Now cout<<a[i][k]in the body of the second loop
This assumes that int a [][]={1,2,3,2,3,4,3,4,5,4,5,6;}is already initialized like this.Clearly this level of array initialization needs to be extended to get to ten. And as was mentioned above your loops should start with i and k being assigned 0
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1