6 Replies - 707 Views - Last Post: 06 November 2012 - 10:36 PM Rate Topic: -----

#1 eraebus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-November 12

Searching through a string array, returning index of another array

Posted 01 November 2012 - 10:09 AM

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.

 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] 


Is This A Good Question/Topic? 0
  • +

Replies To: Searching through a string array, returning index of another array

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: Searching through a string array, returning index of another array

Posted 01 November 2012 - 10:28 AM

Quote

In the following code I have created two arrays, one string and one int, to strore a student name and corresponding test score.


Stop. If you're going to do this, then do it right. This isn't the 1980's and we're not programming in BASIC. This is an OBJECT ORIENTED language. Make a Student class and give it two properties. That way you deal with Student instances and are not trying to keep separate arrays in sync.

public class Student(string studentName)
{
   public string StudentName { get; set; }
   public decimal TestScore { get; set;}
}





As for your actual question:

Loop through one array until you find a match

for (int index = 0; index < arrayName.Length; index++)
{
   if (arrayName[index] == userInputtedName)
   {
       Console.WriteLine(string.format("{0} received a score of {1}", arrayName[index],arrayScore[index]);
       // If the 3rd index is the name, then the 3rd index is the corresponding score, right?
   }
}

Was This Post Helpful? 3
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Searching through a string array, returning index of another array

Posted 01 November 2012 - 10:30 AM

To me using parallel array suggests that the code should be using object classes, to encapsulate the data.


public class StudentTestScore
{
  public Student Student() { get; private set;}
  public Score Score() { get; private set;}
  public StudentTestScore(Student Student, Score Score)
  {
    this.Student = Student;
    this.Score   = Score;
  }
} 
public class Score
{
  public Decimal Score() { get; private set; }
  public Score(Decimal Score)
  { this.Score = Score;}
}
public class Student
{
  public String Name() { get; private set; }
  public Score(String Name)
  { this.Name = Name;}
}



This then allows you to have a List<Student> and a List<StudentTestScore>

Now you don't need to index the array, as the association is preserved.

For example you could sort them by test score
var sorted = StudentTestScores.OrderBy( sts => sts.Score.Value );
foreach(var s in sorted)
{
  Console.WriteLine("{0} {1}", s.Score.Value, s.Student.Name); 
}


which doesn't involve tracking which index is which student.
Tutorial: Parallel Arrays, Array with Object (Class & Structure)
(vb.net but principals still apply to c#)

This post has been edited by AdamSpeight2008: 01 November 2012 - 10:31 AM

Was This Post Helpful? 3
  • +
  • -

#4 dhazard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 67
  • Joined: 06-September 06

Re: Searching through a string array, returning index of another array

Posted 01 November 2012 - 10:37 AM

        public static int FindStudentPosition(string name, string[] array)
        {
            [b]string target = 0;[/b]
            int result;
            target = Console.ReadLine();


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

            return result;

        }



Cant Convert String to Ints. Either do a cast or change the type from string to int. Your method doesnt even return anything at all either.

foreach( KeyValuePair<string, string> kvp in openWith )
{
    Console.WriteLine("Key = {0}, Value = {1}", 
        kvp.Key, kvp.Value);
}



I agree with everyone above about using Objects. I do think you need to get the basics down when it comes to arrays as well. I would opt for a KeyValuePair the key being the student name and the pair being the test score. You can read more about it on MSDN.

MSDN - KeyValuePair<TKey, TValue> Structure

This post has been edited by dhazard: 01 November 2012 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Searching through a string array, returning index of another array

Posted 01 November 2012 - 05:57 PM

If you need to search for something in an array (and don't want to use LINQ), Array.IndexOf will find it for you.
Was This Post Helpful? 1
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: Searching through a string array, returning index of another array

Posted 02 November 2012 - 06:00 AM

Looks like you're trying to create a Dictionary.
Was This Post Helpful? 0
  • +
  • -

#7 eraebus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-November 12

Re: Searching through a string array, returning index of another array

Posted 06 November 2012 - 10:36 PM

Thanks everyone! You've all been a huge help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1