Jonathan Oberhaus

Jonathan Oberhaus

  • NA
  • 6
  • 10.9k

Searching through an array

Nov 1 2012 1:25 PM

In the following code I have created two arrays, one string and one int, to strore a student name and corresponding test score. The score value in the scoreArray is in the same index as the nameArray. What I have to do now is search through the nameArray and return the corresponding test result for that name.

Im not asking you all to do this for me, but I am having a very difficult time understanding how to search the nameArray and also what procdeure is used to return the corresponding value in scoreArray.

Any help is greatly appreciated.


[code]

public class InitArraya

{



    public static string[] arrayName = new string[5]; // declare array named array

    public static int[] arrayScore = new int[5];


    public static void PopulateNameArray()

    {

        // Prompt user for names and assign values to the elements of the array

        for (int intCounter = 1; intCounter < arrayName.Length; intCounter++)

        {

            Console.Write("Enter name {0}: ", intCounter);

            arrayName[intCounter] = Console.ReadLine();

        }

    }


    public static void PopulateScoreArray(string[] array)

    {


        // Prompt user for names and assign values to the elements of the array

        for (int intCounter = 0; intCounter < 5; intCounter++)

        {

            Console.Write("Enter score for {0}: ", arrayName[intCounter]);

            arrayScore[intCounter] = Convert.ToInt32(Console.ReadLine());


        }

    }


   public static int FindStudentPosition(string name, string[] array)

    {

       string target=0;

       int result;

       target = Console.ReadLine();



       Array.FindAll(arrayName, s => s.Equals(target));



       return result;

        

       

       

    }


   public static void Main( string[] args )

   {

  

      Console.WriteLine("Enter 5 names:"); // headings


    PopulateNameArray();

    PopulateScoreArray(arrayName);


       

    

      Console.ReadLine();


   } // end Main

} // end class InitArray

[/code]


Answers (3)