0

Here's the problem: Index was outside the bounds of the array. Assignment: Write a program that determines the number of students who can still enroll in a given class. Design your solution using parallel arrays. Test your solution by retrieving the following data from a text file. Define a exception class for this problem if the current enrollment exceeds the maximum enrollment by more than three. Halt the program and display a message indicating which course is over-enrolled.

Here's the original code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {

        private static string[] classes = { "CS150", "CS250", "CS270", "CS300", "CS350" };

        private static int[] currentEnrolled = { 18, 11, 9, 4, 20 };

        private static int[] maxEnrollment = { 20, 20, 20, 20, 20 };

        private static int currentEnrollment()
        {
            int enrolled = 0;
            foreach (int i in currentEnrolled)
            {
                enrolled += i;
            }
            return enrolled;
        }
        private static void listClasses()
        {
            foreach (string i in classes)
            {
                Console.WriteLine("Class: {0}", i);
            }
        }

        private static void ClassStatus()
        {
            for (int i = 0; i < currentEnrolled.Length; i++)
            {
                Console.WriteLine("Class: {0}, Max: {1}, Current: {2}, remaining: {3}", classes[i], maxEnrollment[i], currentEnrolled[i], maxEnrollment[i] - currentEnrolled[i]);
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Currently Enrolled: {0}", currentEnrollment());
            ClassStatus();
            Console.ReadKey(false);
        }
    }
}

Now, I've been editing the above code to take a text file instead, however I get an error. Here's what I'm working with:

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

namespace ConsoleApplication1
{
    class Program
    {

        private static string[] classes = new string[900];
        private static int[] currentEnrolled = new int[900];
        private static int[] maxEnrollment = new int[900];

        private static int currentEnrollment()
        {
            int enrolled = 0;
            foreach (int i in currentEnrolled)
            {
                enrolled += i;
            }
            return enrolled;
        }
        private static void listClasses()
        {
            foreach (string i in classes)
            {
                Console.WriteLine("Class: {0}", i);
            }
        }

        private static void ClassStatus()
        {
            for (int i = 0; i < currentEnrolled.Length; i++)
            {
                Console.WriteLine("Class: {0}, Max: {1}, Current: {2}, remaining: {3}", classes[i], maxEnrollment[i], currentEnrolled[i], maxEnrollment[i] - currentEnrolled[i]);
            }
        }

        static void Main(string[] args)
        {
            string[] lines = File.ReadAllLines("classes.txt");
            int i = 0;

            foreach (string line in File.ReadAllLines("classes.txt"))
            {

                string[] parts = line.Split(',');
                while (i < 900 && i < parts.Length)
                {
                        classes[i] = parts[1];
                    currentEnrolled[i] = int.Parse(parts[2]);
                    maxEnrollment[i] = int.Parse(parts[3]);
                }
                    i++;
            }

            Console.WriteLine("Currently Enrolled: {0}", currentEnrollment());
            ClassStatus();
            Console.ReadKey(false);
        }
    }
}

Some of the components used in the above code were taken from this article: Splitting data from a text file into parallel arrays

Text file looks like this:

CS150,18,20
CS250,11,20
CS270,32,25
CS300,4,20
CS350,20,20

Any assistance will be appreciated. And yes, this is an assignment. Programming is most definitely not my strong suit.

4
  • Did you try using a debugger to step through the code? Commented Dec 4, 2013 at 16:18
  • Yes. The debugger would stop and output the above error at the following line: maxEnrollment[i] = int.Parse(parts[3]); @DanielMann Commented Dec 4, 2013 at 16:23
  • Arrays in .NET are zero based: parts[0] parts[1] parts[2], also your while loop is wrong. Check the index i! Commented Dec 4, 2013 at 16:24
  • I removed the "parallel processing" tag. Having multiple arrays does not make your code parallel. Commented Dec 4, 2013 at 16:37

1 Answer 1

1

There seem to be multiple problems with your while loop.

First, parts.Length will always be 3, since you have 2 commas and split on that. So the condition i < 900 && i < parts.Length does not really make sense, it's like i < 900 and i < 3, so it will always stop at 3. The intent is not really clear here, I think you meant to loop on each 900 values, but fi soforeach already does that for you.

Next, since there's 3 parts and C# arrays are 0-based, it should be parts[0], parts[1] and parts[2]. That's what causing your 'out of range' exception.

Finally, i++; should be in your while loop. If you leave it outside, you will loop forever as the index will never increase.

Basically, it should be something like this :

            while (i < 900)
            {
                classes[i] = parts[0];
                currentEnrolled[i] = int.Parse(parts[1]);
                maxEnrollment[i] = int.Parse(parts[2]);
                i++;
            }

Again, the 900 is not really clear since you don't have 900 values per line (remember you're in a foreach). In my opinion you might as well scratch all that and redo it carefully.

What you need to do, is the following :

Read the file and store all the lines
Foreach line do:
    Split the line in 3 parts
    Store each separate part
Write results

For the "custom exception" part, you can add:

For the length of currentEnrollment do:
    If currentEnrollment at current index is superior than maxEnrollment at current index do:
        Throw a new exception with the className at current index
Sign up to request clarification or add additional context in comments.

2 Comments

I made the following changes. I also changed 900 to 9. However, when I run the program, it basically outputs the first line of the text file 9 times.
Yeah, because you're looping on the same line. The foreach gives you each line separately, then you loop again with each parts. I'll add some pseudocode to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.