954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Which loops best for string arrays and int arrays looping?

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

codechrysalis
Light Poster
27 posts since Jan 2012
Reputation Points: 9
Solved Threads: 0
 

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.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

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";
        }
kplcjl
Junior Poster
149 posts since Sep 2009
Reputation Points: 16
Solved Threads: 12
 

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
codechrysalis
Light Poster
27 posts since Jan 2012
Reputation Points: 9
Solved Threads: 0
 

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);
        }
kplcjl
Junior Poster
149 posts since Sep 2009
Reputation Points: 16
Solved Threads: 12
 

i will look at it right now

codechrysalis
Light Poster
27 posts since Jan 2012
Reputation Points: 9
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: