Taher Hussini
Taher Hussini

Reputation: 3

find the highest number in an array made by the method

There is a class for passengers which contain string name, int age, string job etc. I made an array of this class, lets say it has 10 places. I wanna find the oldest passenger.

My code doesn't work, because it is not possible to compare passenger[i] with an integer. I mean I need only the age in passenger[]

How to find the oldest one in passenger[]?

EDIT: The return value should be a passenger by his name and major, not only its age.

public Passenger Oldest()
{
    int oldest = 0;
 
    for (int i = 0; i < passengers.Length; i++)
    {
        if (passengers[i] > oldest)
        {
            oldest = passengers[i];
        }
    }
  
    return oldest;
}

    
class Passenger
{
    int age;
    string name;
    string major;
    
    public Passenger(int _age, string _name, string _major)
    {
        age = _age;
        name = _name;
        major = _major;
    }
}

Upvotes: 0

Views: 86

Answers (3)

Roman
Roman

Reputation: 12171

Firstly, as mentioned by @Cid in comment to question all fields of Passenger are private (by default when modifier is not specified). You should either mark then as public (to access them outside declaring class) or better create public properties:

class Passenger
{
    public int Age { get; set; } // public auto property
    public string Name { get; set; } // public auto property
    public string Major { get; set; } // public auto property

    public Passenger(int age, string name, string major)
    {
        Age = age;
        Name = name;
        Major = major;
    }
}

Secondly, you need to compare Age (proeprty, not whole object) of the passenger:

if (passengers[i].Age > oldest)
{
    oldest = passengers[i].Age;
}

Also, you could use LINQ to find the oldest passenger:

var oldest = passengers.Max(item => item.Age);

Finally, to return the oldest passenger:

public Passenger Oldest()
{
    // if no passengers -> return null
    if (!passengers?.Any() ?? true)
    {
        return null;
    }

    var maxAge = passengers.Max(item => item.Age);
    
    return passengers.First(item => item.Age == maxAge);
}

Also, as mentioned by @DmitryBychenko the method can be shortened to:

public Passenger Oldest()
{
    // if no passengers -> return null
    if (!passengers?.Any() ?? true)
    {
        return null;
    }

    return passengers.Aggregate((s, a) => s.Age > a.Age ? s : a);
}

or without LINQ:

public Passenger Oldest()
{
    // if no passengers -> return null
    if (passengers == null || passengers.Length == 0)
    {
        return null;
    }

    var maxAge = passengers[0].Age;
    var oldestPassengerIndex = 0; 
 
    for (var i = 1; i < passengers.Length; i++)
    {
        if (passengers[i].Age > maxAge)
        {
            oldest = passengers[i].Age;
            oldestPassengerIndex = i;
        }
    }
  
    return passengers[oldestPassengerIndex];
}

Upvotes: 2

Amedeo Bruni
Amedeo Bruni

Reputation: 21

Using Linq is easy

var oldest=passengers.Max(x=>x.Age):

Otherwise

Passenger oldest=new Passenger(0,"","");
foreach (Passenger p in Passengers){
   if (p.age>oldest.age) oldest=p;
}

Upvotes: 0

Johnathan Barclay
Johnathan Barclay

Reputation: 20353

Slightly more efficient version of Roman's answer, especially if the oldest passenger appears last in the list:

public Passenger Oldest()
{
    if (passengers.Length == 1) return passengers[0];

    var oldest = passengers[0];
    for (int i = 1; i < passengers.Length; i++)
    {
        if (passengers[i].Age > oldest.Age) oldest = passengers[i];
    }
    return oldest;
}

This only ever requires a single iteration.

Upvotes: 0

Related Questions