Basically I have to do this: Write a program that displays the number of students that can still enroll in a given class.
Design your solution using parallel arrays.

my problem is what loops should I use for this I dont think for loops would be useful at all...I think foreach

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            string[] classes = new string[] { "CS150", "CS250", "CS270", "CS300", "CS350" };
            byte[] currentEnrolled = new byte[] { 18, 11, 9, 4, 20 };
            byte[] maxEnrollment = new byte[] { 20, 20, 20, 20, 20 };
            currentEnrolled = checkClassSize(); 
            string className; 
            string userVal; 
            bool data = true; 
            DisplayInstructions(); 
            userVal = GetUserVal(); 
                             

        } // end of main 

        public static void DisplayInstructions()
        {

            Console.WriteLine("This program will check the number of opening seats for a class you type in"); 
            Console.WriteLine(""); 
            Console.Clear(); 
            Console.WriteLine("Please press any key to continue...."); 
            Console.ReadKey(); 
       } // end of display instructions

       public static string GetUserVal()
       
      {
           string userVal; 
           Console.WriteLine("Please enter a class you wish to check an open seat for"); 
           userVal = Console.ReadLine(); 
          
           return userVal; 
      }

      public static string checkClassSize()
      {
          byte currentEnrolled;
          for (i = 0; i < Array.currentEnrolled; i++ )
          {
              Console.WriteLine(i);
          }
      }
   
      }// end of class 
   } // end of namespace

Thanks

Recommended Answers

All 6 Replies

You could use either a regular for loop or a foreach.
If you use a foreach, you would need to increment your own counter; negating some of the benefit.

Since you are using parallel arrays, it is important to have an index. This means you should use a for loop.

In the body of your code, you have defined a byte array called currentEnrolled. This data is only in the scope of your main function, it's invisible elsewhere.

Further on compressed code:
public static string checkClassSize() { byte currentEnrolled;
for (i = 0; i < Array.currentEnrolled; i++ ) { Console.WriteLine(i); }}

This currentEnrolled has nothing to do with the other. "i" isn't declared, don't see how this could compile. Array is a collections type that does not have a property or function called currentEnrolled, again, won't compile, you are writing out what appears to be the iterator insead of the non-existant array's value.

public static string checkClassSize(byte[] currentEnrolled)
        {
            for (int i = 0; i < currentEnrolled.Length; i++) { Console.WriteLine(currentEnrolled[i]); }
            return "Nonsense because you didn't define the string";
        }

im thinking as well to implement foreach because I have no idea how to use a for loop to display the class names with the number of open seats...

this is what i have so far.. stuck until i figure out the correct looping structure:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            string[] classes = { "CS150", "CS250", "CS270", "CS300", "CS350" };
            int[] currentEnrolled =  { 18, 11, 9, 4, 20 };
            int[] maxEnrollment =  { 20, 20, 20, 20, 20 };
            string className; 
            string userVal; 
            bool data = true; 
            DisplayInstructions(); 
            userVal = GetUserVal(); 
                             

        } // end of main 

        public static void DisplayInstructions()
        {

            Console.WriteLine("This program will check the number of opening seats for a class you type in"); 
            Console.WriteLine(""); 
            Console.Clear(); 
            Console.WriteLine("Please press any key to continue...."); 
            Console.ReadKey(); 
       } // end of display instructions

       public static string GetUserVal()
       
      {
           string userVal; 
           Console.WriteLine("Please enter a class you wish to check an open seat for"); 
           userVal = Console.ReadLine(); 
          
           return userVal; 
      }

      public static string classLoop() 
      {
          string classes = "";  
          for(int i = 0; i < classes.Length; i++)
          {
              
              Console.WriteLine(i);
          }

          return classes;
          
           
      }

      public static int currentEnrolled()
      {
          int enrolled = 0;
          for (int i = 0; i < currentEnrolled.Length; i++)
          {
              Console.WriteLine(i);
          }
          return enrolled; 
      }
   
      }// end of class 
   } // end of namespace

OK, people (including me) are talking to you like you would understand what we are saying. That's not working. Think of scope, I moved the arrays outside of the main routine to make the arrays globally available. Others said you should use the for loop to reach all the arrays in parallel. That's correct, I've supplied a routine to show how that works. In that routine, "i" is an iterator. I used "i" over and over to show its scope is local and independent in each of the routines. Check out and understand what "i" is doing in each of the arrays. Someone else said you could use the foreach but then you would have to independently maintain the iterator. That's true too. Didn't give an example on how that would work.

/// <summary>
        /// parallel array of class names
        /// </summary>
        private static string[] classes = { "CS150", "CS250", "CS270", "CS300", "CS350" };
        /// <summary>
        ///  parallel array of people currently enrolled in each class.
        /// </summary>
        private static int[] currentEnrolled = { 18, 11, 9, 4, 20 };
        /// <summary>
        ///  parallel array of maximum enrollment in each class.
        /// </summary>
        private static int[] maxEnrollment = { 20, 20, 20, 20, 20 };
        /// <summary>
        /// Goes through the currently enrolled count and sums it.
        /// </summary>
        /// <returns>total enrolled in all classes</returns>
        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);
            }
        }
        /// <summary>
        /// Collects statistics from the parallel arrays
        /// </summary>
        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]);
            } 
        }
        /// <summary>
        /// Entry routine for Console apps.
        /// </summary>
        /// <param name="args">Allows parameters to be passed on to the app, any paassed are ignored in this app</param>
        static void Main(string[] args)
        {
            Console.WriteLine("Currently Enrolled: {0}", currentEnrollment());
            ClassStatus();
            listClasses();
            Console.ReadKey(false);
        }

i will look at it right now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.