I came up with these two arrays and now i need to search the nameArray by a name inputted by the user and then show the corresponding index value from the scoreArray? Any ideas?

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

If the two arrays are directly in sync, ie. position 0 in array one corresponds to position 0 in array two, you can for-loop through the first array and when you find a match, use the current value of the for-loop to access the related second array value.

Alternatively you could implement a 2D array to store both name and value in one array.

Edited 4 Years Ago by Mike Askew

Have you tried hashTables? I find them very useful when searching. Perhaps I am mistaken but Hashtabels are faster than array's aswell, because when seraching threw arrays, it starts from element 0 and moves its way up until if finds the source, while HashTables simply takes a guess at where the element is located and grabs it.

While Mikey's way would indeed work, I usually try to avoid this just incase somehow the two go out of sync (never the less Mikey's would work).

What you coudl do is make a custom object array

public class scoreData
{
    string name;
    int score;

    public scoreData(string name, int score) //this assigns values to the variables
    {
        this.name = name; //use this will assign the name in the () to the name in the class
        this.score = score;
    }

    public void inName(string input) //allows you to modify the variable by passing in a string
    {
        this.name = input;
    }

    public void inScore(int input)
    {
        this.score = input;
    }

    public string isName() //will pass back the name to a string
    {
        return (this.name);
    }

    public int isScore()
    {
        return (this.score);
    }
}

Now I know some of the members in Daniweb are yelling at me for not using get/set, sorry guys I have my reasons for doing it this way (helps when it come to reading code)

Then to implement this do it like this

scoreData [] myArray = new scoreData[5];

for(int i = 0; i < myArray.Length; i++)
{
    myArray[i] = new scoreData(arrayName[i], arrayScore[i]);
}

The for loop takes the data in the arrays and transfers it over to this one array however you don't need to do that, you could just implement one item at a time like this example

myArray[0] = new scoreData("name", 10);

Now if you want to find the name you could do something like this (and display the score)

int foundIndex = -1; //make sure you reset this value back to -1 everytime you search for a name if you use the if/else below

for(int i = 0; i < myArray.Length; i++)
{
    if(myArray[i].isName() == "Name to Find")
    {
        foundIndex = i;
        break; //jumps out of the for loop, no need to keep searching
    }
}

if(foundIndex == -1)
{
    //the name couldn't be found, so display maybe an error message
}
else
{
    //display the score, something like this...

    Console.WriteLine(myArray[foundIndex].isScore().ToString()); //prints the score
}

I use tricks like this as I usually build programs that contain large collections of data that must be grouped together. Oh yeah if you want to say change one of the names for instance you can do something like this

myArray[0].inName("New Name"); //remember 0 is just an example, you can use any index you'd like

So there you have it ... and look it's all in one array (if this is for a class maybe you outsmart your class with this clever move, since technically you are using an array)

(man I haven't used arrays in forever, Lists have spoiled me)

Edited 4 Years Ago by JOSheaIV

This article has been dead for over six months. Start a new discussion instead.