Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

index out of range??!! 1

Status
Not open for further replies.

Triacona

Technical User
Jun 11, 2009
462
0
0
GB
Hi all,
Thanks for a wonderful forum [smile]
The code below is set to ask the user for an input to calculate an octal number.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

            dec = ReadAns("Please enter your number ");

            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.ReadLine();
                Console.WriteLine("This is the quotient {0} ", quot);
                Console.WriteLine("This is the remaider {0} ", rem);
                Console.ReadLine();
                dec = quot; 
              
            } while (dec != 0);

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

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

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

        private static int ReadAns(string p)
        {
            Console.Write(p);
            string ans = Console.ReadLine();
            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?!!

Please help, this is a humdinger!
Thank you [smile]

Thank you,

Kind regards

Triacona
 
Code:
           for (int i = revOctal.Length; i != 0 ; i--)
should be
Code:
           for (int i = revOctal.Length -1; i >= 0 ; i--)
 
Thank you so much !! [2thumbsup]
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?

Thanks for all your help [bigsmile]


Thank you,

Kind regards

Triacona
 
Thank you so much !! [2thumbsup]
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?

Thanks for all your help [bigsmile]

Thank you,

Kind regards

Triacona
 
Oh I know I know!! [smile]

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?

Thanks again for your help [smile]




Thank you,

Kind regards

Triacona
 
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.
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top