12 Replies - 3900 Views - Last Post: 03 February 2012 - 06:08 PM Rate Topic: -----

#1 babbu25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 26-November 11

Index was outside the bounds of the array Error...

Posted 28 January 2012 - 09:43 AM

hi friends..I am doing a c# program which uses Jagged array at one point.I am not able to repeat the for loop which is going to store my value in a jagged array.I have attached the Sample output i have to get in a word document and also the problem description .Here is my code for PART-1......



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

namespace Lab2
{
    class Program
    {
        static void Main(string[] args)
        {
            int noofagents;
            int[][] intJaggedArray;
           string[] names;
            Console.WriteLine("NUmber of agents ? ==>");
            noofagents =  Convert.ToInt32(Console.ReadLine());
            names = new string[noofagents];
            for (int i = 0; i < noofagents; i++)
            {
                Console.WriteLine("Agent Name # {0}==>", i + 1);
                names[i] = Console.ReadLine();
                Console.WriteLine("NUmber of Homes sold by {0}===>", names[i]);
                int noofhomes=Convert.ToInt32(Console.ReadLine());
                intJaggedArray = new int[noofhomes][];
                for (int j = 1; j < noofhomes; j++)
                {
                    intJaggedArray[j] = new int[j + 1];
                    for (int k = 0; k < j; k++)
                    {
                        Console.WriteLine("Saleprice for Home # {0} ==>", k++);
                        intJaggedArray[j][k] =Convert.ToInt32( Console.ReadLine());                        

                    }


                }
                
            }

            
        }
    }
}





-------------------I am not getting any error but i am not also getting the output as i need.Please help me what was my mistake here...Thanks n advance

Is This A Good Question/Topic? 0
  • +

Replies To: Index was outside the bounds of the array Error...

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: Index was outside the bounds of the array Error...

Posted 28 January 2012 - 09:55 AM

Reminder to new folks: This is homework and we don't provide completed code for homework. Rather we try to help the student comprehend the concept they are struggling with so they can write their own code. They are being graded on their skill not ours.

Learning to debug one's own code is an essential skill. Sadly, one that apparently no college course teaches. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler
[/spoiler]
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: Index was outside the bounds of the array Error...

Posted 28 January 2012 - 10:04 AM

Suggestions:

if you use Console.Write instead of WriteLine your input will be at the question, not on the next line.

All collections in C# are zero indexed, but you are starting your loops at 1, so you are always off by 1 from the start.

Line 30 is incrementing the value of K. You don't want to do that here. Do the same that you do elsewhere K+1 not K++

Most of your errors are typical little things that you would see if you breakpoint and step through execution with the F10 key and look at the Locals pallet. Those debugging tutorials will really help you.


Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

// Try #1 - May 1, 0900hrs
//    code
//    code  
//    code
// Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
//    code
//    code  
//    code
// Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
    code
    code  
    code


If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.



You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: Index was outside the bounds of the array Error...

Posted 28 January 2012 - 01:20 PM

Ok... I just have to ask... What did you find unhelpful in those suggestions, or the errors I found in your code?

This post has been edited by tlhIn`toq: 28 January 2012 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: Index was outside the bounds of the array Error...

Posted 28 January 2012 - 02:19 PM

Never mind. I guess it was pretty bad advice.

I mean its not like those things worked when I took 20 minutes to build this, right?
And its not like you'd like to know how to keep your current code from crashing when someone enters "five" instead of "5", or anything silly like that?

And having the console output line up with the answers on the same line as the questions is just fraking silly, right?

Attached Image

Anyway, best of luck with your course.

If you need a little help with the code, here's how I did it

Spoiler

Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: Index was outside the bounds of the array Error...

Posted 30 January 2012 - 05:05 PM

This is the 'Help me with my homework' article you wanted.
Was This Post Helpful? 0
  • +
  • -

#7 babbu25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 26-November 11

Re: Index was outside the bounds of the array Error...

Posted 30 January 2012 - 05:46 PM

hi tlhIn`toq

It was not that the post was not worth but it was not at all answered my question.Yeah its my mistake that i have kept the whole thing.I didnt wanted all the solution though.I was trying hard to solve it from few days.I was just confused as how to explain my QUESTION.I couldnt see a way to let u people know about my question.

I just thought i had that question and thats what i have done.I thought i would get an way to solve it .My whole stuff which i put has some wrong code..Thats all i asked to solve ..

Sorry if i mentioned any wrong

Still i didnt get any solution that i could solve it .

Thankyou sir
tlhIn`toq
Was This Post Helpful? 0
  • +
  • -

#8 babbu25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 26-November 11

Re: Index was outside the bounds of the array Error...

Posted 30 January 2012 - 06:47 PM

hi friends.I have been working on project which using a jagged arrays should Store values in my array..It includes User to ask fro number of agents and based on a particular name number of houses he sold and the Sale price of that particular house has to be stored.
AGents names (stored into an array )----what i am doing
Number of sales for each agent.are stored into a jagged array--------(required for sure)


static void Main(string[] args)
        {

            int noofagents;
            int[][] intJaggedArray;
            string[] names;
            Console.Write("NUmber of agents ? ==>");
            noofagents = Convert.ToInt32(Console.ReadLine());
            names = new string[noofagents];
            for (int i = 0; i < noofagents; i++)
            {
                Console.Write("Agent Name # {0}==>", i+1);
                names[i] = Console.ReadLine();
                Console.Write("NUmber of Homes sold by {0}===>", names[i]);
               int noofhomes = Convert.ToInt32(Console.ReadLine());
             intJaggedArray = new int[noofhomes][];
            // intJaggedArray[0] = new int[1];
             //for (int j = 1; j < noofhomes; j++)
            // {
                 intJaggedArray[0] = new int[noofagents + 1];
                // int[] innerarray=new int[noofhomes];
                 for (int k = 0; k < noofhomes; k++)
                 {
                     Console.Write("Saleprice for Home # {0} ==>", k+1);
                     intJaggedArray[i][k] = Convert.ToInt32(Console.ReadLine());--------------------------------
                     

                 }//


            }




At the dasshed line i am getting an error -----"Index was outside the bounds of the array-----------
I think the problem is at the initialization of the jagged array.Any ideas about this problem.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: Index was outside the bounds of the array Error...

Posted 30 January 2012 - 07:14 PM

Please don't start new threads for the same problem. I've merged the new thread with the existing one.

You really might want to look at this article:

This is the 'Help me with my homework' article you wanted.

Considering this very problem is what spurred me to write it, and I use this problem as and example of how to setup your homework.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: Index was outside the bounds of the array Error...

Posted 30 January 2012 - 07:36 PM

Update: As near as I can see you've only made one change to your code since the original post.

Attached Image

Despite being shown how you need to re-organize to make it functional, for some reason you choose to not take advice. You did however choose to repost it instead of work on. I'm not sure I see the reasoning behind that. I have an assumption, but I won't voice it yet.

As long as you are willing to work and make an effort to learn this, I'm willing to give you my attention to help you. But I will NOT give you code. I will help you think.

One:
A jagged array is an array, of arrays. Each element of a jagged array is itself an array. It doesn't matter what the elements of an array are, they have to be initialized, right? So if the jagged array consists of arrays... then each element-array must also be initialized right?

Two:
I've given you links to debugging tutorials... tutorials for how to use breakpoints and look at the values of variables as code executes... They were given to you to help you learn and understand. But none of us here can do them for you. You have to put in the time and effort to develop your craft and skills.

I want you to look at what you do here:
10 for (int i = 0; i < noofagents; i++)
{
16 intJaggedArray = new int[noofhomes][];

}

Stop and think about that please. Then answer me one question: What is happening to the jagged array holding all of your information, every time your loop executes?

No help from the audience please. This is not about whether or not you know the answer, but does the person writing the code and getting the grade comprehend this loop?

babbu25: Do you know what is happening to your intJaggedArray every time this loop executes?
Was This Post Helpful? 0
  • +
  • -

#11 babbu25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 26-November 11

Re: Index was outside the bounds of the array Error...

Posted 30 January 2012 - 07:59 PM

hi tlhIn`toq


I have submitted my project and i scored less but still i am trying to solve it just because i want to be clear on it sir.

--ABout your question-------: Do you know what is happening to your intJaggedArray every time this loop executes?

intJaggedArray = new int[noofhomes][];

When ever it executes the array changes its value as the noofhomes (in my case rows for a agent) changes.I have this array intJaggedArray where each row (noofhomes) can have different number of elements or columns (In my example it is price of each home ).
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: Index was outside the bounds of the array Error...

Posted 30 January 2012 - 09:48 PM

Ok. So this is the first problem, because that's not what it does.

inJaggedArray is being reset to a new instance of a completely blank jagged array of dimentions [noofhomes][].

It is being totally reset: All new object. All old data gone.

So you have say... 5 sales people... This loop executes 5 times, once for each person. But every time it executes you re-initialize the jaggedarray that is supposed to be holding all of their data.

Instead of initializing the entire jaggedarray, you need to just initialize one element of it, to become the home prices for that sales agent.

JaggedArray[AgentCounter] = new int[NumberOfHomes];

Do me a favor: Start over. I've pointed you to a couple tutorials, one of which actually sets up this very question. Go through that much. Get yourself restarted properly organized. Then post the code for the new project, however far you get, and we'll pick up from there.

See post #9 for that link again.
Was This Post Helpful? 0
  • +
  • -

#13 babbu25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 26-November 11

Re: Index was outside the bounds of the array Error...

Posted 03 February 2012 - 06:08 PM

hi tlhIn`toq

I tried to follow what you said to.I have checked using breakpoints and changed my variables name so that i can understand.

Here is my new code as far as i understood from the link you have given.


              int noofagents;
            int[][] intJaggedArray;
            string[] names;
            Console.Write("NUmber of agents ? ==>");
            noofagents = Convert.ToInt32(Console.ReadLine());
            names = new string[noofagents];
            intJaggedArray = new int[noofagents][];
            for (int Agentcounter = 0; Agentcounter < noofagents; Agentcounter++)
            {
                Console.Write("Agent Name # {0}==>", Agentcounter+1);
                names[Agentcounter] = Console.ReadLine();
                Console.Write("NUmber of Homes sold by {0}===>", names[Agentcounter]);
               int noofhomes = Convert.ToInt32(Console.ReadLine());
             
            // intJaggedArray[0] = new int[1];
             //for (int j = 1; j < noofhomes; j++)
            // {
                 intJaggedArray[Agentcounter] = new int[noofhomes];
                 //intJaggedArray[i] = new int[i + 1];
                // int[] innerarray=new int[noofhomes];
                 for (int salecounter = 0; salecounter < noofhomes; salecounter++)
                 {
                     //intJaggedArray[salecounter] = new int[Agentcounter+1];
                     Console.Write("Saleprice for Home # {0} ==>", salecounter+1);
                     intJaggedArray[Agentcounter][salecounter] =Convert.ToInt32 (Console.ReadLine());
                     

                 }//







I have understood now.From all the time i have been re intitalizing my array.Thats why i am always starting from initial.
I have got much clarity about jagged arrays now .this makes much sense to me .

Correct me if m wrong.......
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1