We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,708 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Maths puzzle; Reversing Numbering Order!

Hi,

I'm trying to reverse the ordering of number.

I'm given an array of size.. say, 5. WITHIN a do-while loop, I must iterate from element 0 to 4.

For example, if the element contains ["Apple", "Banana", "Cow", "Dog", "Egg"], the output of the do-while loop has to be:

Apple Banana Cow Dog Egg

I have tried (in C#):

int numOfElements = array.Count;    //Number of elements. 

do
{
    TextBox.Text += array[numOfElements-1] + " ";
    numOfElements--;
}
while (numOfElements != 0);

...but obviously this will print out "Egg Dog Cow Banana Apple", which is the reverse order of what I'm tring to achieve.

So how can I make it print it in the correct order, within the Do-While loop? Some mathematical work involved in my opinion...?

Thank you!

3
Contributors
2
Replies
12 Hours
Discussion Span
5 Months Ago
Last Updated
4
Views
thompsonSensibl
Light Poster
36 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Um...what? So you're trying to traverse an array with a do..while loop in forward order?

int i = 0;

do
    Console.Write(array[i] + " ");
while (++i < array.Count);

Or if you can't have a counter starting from 0 and must have a descending counter starting from array.Count, the difference of the counter and array.Count will give you what you want:

int i = array.Count;

do
    Console.Write(array[array.Count - i] + " ");
while (--i != 0);
deceptikon
Challenge Accepted
Administrator
3,427 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

It is conventional and more readable to use a for loop or a foreach loop, if the language has one.

for(int i = 0; i < array.Count; ++i){ 
    Console.Write( array[i] );
}
firstPerson
Industrious Poster
4,044 posts since Dec 2008
Reputation Points: 851
Solved Threads: 625
Skill Endorsements: 14

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0565 seconds using 2.66MB