×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# index out of range??!!

## index out of range??!!

(OP)
Hi all,
Thanks for a wonderful forum
The code below is set to ask the user for an input to calculate an octal number.

#### CODE --> c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace doExerciseAttempt
{
class Program
{
static void Main(string[] args)
{
int dec = 0;
int rem = 0;
int quot = 0;
string revOctal = "";
string fullOctal = "";

do
{
quot = CalculateQuot(dec);
rem = calculateRem(dec);
string octal = rem.ToString();
Console.WriteLine("This is the octal number {0} ", octal);
octal = rem.ToString();
revOctal += octal;
Console.WriteLine("This is the quotient {0} ", quot);
Console.WriteLine("This is the remaider {0} ", rem);
dec = quot;

} while (dec != 0);

Console.WriteLine("This is the reverse octal number {0} ", revOctal);
for (int i = revOctal.Length; i != 0 ; i--)
{
fullOctal += revOctal[i];
}
Console.WriteLine("This is the octal number {0} ", fullOctal);

}

private static int CalculateQuot(int dec)
{
return dec / 8;
}

private static int calculateRem(int dec)
{
return dec % 8;
}

{
Console.Write(p);
return int.Parse(ans);
}
}
} 

I am stuck because I have tried to use the for loop to reverse write the string stored in revOctal and reorder it correctly to assign to fullOctal.
I get an error Index Out of Range Exception was unhandled..??!
I checked the code, if I input 999, the code returns 7471
Which has 4 characters...
so I made the loop go through it backwards, by setting I to the length of the revOctal, so 4, and then while i != 0; i-- , add the full octal (blank) to the revOctal so 1 (which is position 4) in this case, and then to the 3rd character and then the second etc...
but it gives the above error.
What is weird is that if I state i = revOctal.Length - 1, it works, but only outputs 3 characters?!!

Thank you

Thank you,

Kind regards

Triacona

### RE: index out of range??!!

#### CODE

for (int i = revOctal.Length; i != 0 ; i--)
should be

#### CODE

for (int i = revOctal.Length -1; i >= 0 ; i--)

### RE: index out of range??!!

(OP)
Thank you so much !!
Why is it -1
that would give 3, but if > or = 0 then it would include 0 so 4 characters, but if no -1 then it would include 5 characters?

Why could I not use i != 0?
would it not go 4,3,2,1 which is also 4 characters?
or is it because i is decremented only after the loop is executed and therefore will include one more (0)?

What if i moved the -- to the front of i so --i?

Thank you,

Kind regards

Triacona

### RE: index out of range??!!

(OP)
Thank you so much !!
Why is it -1
that would give 3, but if > or = 0 then it would include 0 so 4 characters, but if no -1 then it would include 5 characters?

Why could I not use i != 0?
would it not go 4,3,2,1 which is also 4 characters?
or is it because i is decremented only after the loop is executed and therefore will include one more (0)?

What if i moved the -- to the front of i so --i?

Thank you,

Kind regards

Triacona

### RE: index out of range??!!

(OP)
Oh I know I know!!

It is because i starts out as 0 and therefore has to iterate through the loop at least once from 0 position??

if it is starting from 0 instead of 4, then why is the number outputting correctly, if it is 0 is that a null value in the characters and therefore does not display?

If you have 4 characters, does the numbering of each start at 0, 1, 2, 3...

If so then why does the first i not display the 0 position of the character set?

E.g. if revOctal = 7471, that is 4 characters in total...
therefore 7 is the 0 position in the character array?
therefore 4 is the 1 position in the character array?
therefore 7 is the 2 position in the character array?
therefore 1 is the 3 position in the character array?

so

#### CODE

for (i = revOctal.Length - 1 (so 3); i >=0 (true) ; i--)
{
fullOctal += revOctal[i(3)]//so "1"
} 

#### CODE

for (i = revOctal.Length - 1 (so 2); i >=0 (true) ; i--)
{
fullOctal += revOctal[i(2)] //so "7"
} 

#### CODE

for (i = revOctal.Length - 1 (so 1); i >=0 (true) ; i--)
{
fullOctal += revOctal[i(1)] //so "4"
} 

#### CODE

for (i = revOctal.Length - 1 (so 0); i >=0 (true) ; i--)
{
fullOctal += revOctal[i(0)] //so "7"
} 

Is this how you would count characters?
so it must be that i is not 0 but is designated the length - 1 so therefore iterates through from 3 to 0?

Thank you,

Kind regards

Triacona

### RE: index out of range??!!

The standard idiom for traversing an array of length items is

#### CODE

for (int i = 0; i < length; ++i)

or in reverse

for (int i = length - 1; i >= 0; --i) 
If you are asking lots of questions, next time, could you number them - thanks.

The first i does not display the 0th position because it is assigned to length-1.

#### Quote:

E.g. if revOctal = 7471, that is 4 characters in total...
therefore 7 is the 0 position in the character array?
therefore 4 is the 1 position in the character array?
therefore 7 is the 2 position in the character array?
therefore 1 is the 3 position in the character array?
Correct

In your case, you are going through the loop backwards so you use the second idiom. It doesn't matter whether you have ++i or i++. In theory, it is marginally faster to do ++i instead of i++ because i++ saves the value, increments and returns the saved value, which isn't used. The compiler will normally optimize this out.

Don't quite understand what you mean by counting characters. The number of characters in the string is in the length attribute - you don't need to count them.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!