A global method that finds the average, and the highest speed of cars

1.3k views Asked by At

I created the Car class with attributes and properties. In the main part of the program, I created a series of cars and initialized the values.

My program should use the global method to find the fastest car and average speed of all cars, the method must also return two results.

I tried to make This method, I made for a loop that will pass through every speed individually and split it with the total number of cars in a row, then added to the variable average, does this make sense?

To find the fastest car in a row, I'm not sure how to do it, so I asked myself the question.

Can anyone explain to me about this speed finding algorithm in the global method and also generally about the whole task if I made a mistake in the definition of the same method?

class Car
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private int price;

    public int Price
    {
        get { return price; }
        set { price = value; }
    }

    private float speed;

    public float Speed
    {
        get { return speed; }
        set { speed = value; }
    }    
}

This is main program

class Program
{
    public static void MaxSpeedCarAverage(Car[] arraycar,float Maxspeed,float average)
    {                
        for(int i=0;i<10;i++)
        {
            average+= arraycar[i].Speed / 10;
        }
    }

    static void Main(string[] args)
    {
        Car[] car = new Car[10]
        {
            new Car(){Name="Bmw",Price=5888,Speed=290 },     //initialisation of array//
            new Car(){Name="Mercedes",Price=7544,Speed=300},
            new Car(){Name="Peugeot",Price=4500,Speed=190},
            new Car(){Name="Renault",Price=6784,Speed=210},
            new Car(){Name="Fiat",Price=3221,Speed=180},
            new Car(){Name="Audi",Price=4500,Speed=240},
            new Car(){Name="Golf",Price=4500,Speed=255},
            new Car(){Name="Sab",Price=4500,Speed=332},
            new Car(){Name="Range Rover",Price=4500,Speed=340},
            new Car(){Name="Honda",Price=4500,Speed=267},

        };

    }
}
3

There are 3 answers

1
kylew On BEST ANSWER

Though taking each speed and dividing by 10 works in this case, you may consider dividing by the length of the Car array instead of 10 (done in code sample below). To find the max speed, simply assign the speed of the first car in the array to the max speed, and update it if another Car turns out to be faster.

    public static void MaxSpeedCarAverage(Car[] arraycar, float maxspeed, float average)
    {
        maxspeed = arraycar[0].Speed;
        for(int i=0;i<10;i++)
        {
            if(arraycar[i].Speed > maxspeed) {
                maxspeed = arraycar[i].Speed;
            }
            average+= arraycar[i].Speed / arraycar.Length;
        }
    }
3
Kyle On

You should simplify your class like this:

public class Car {
    public string Name { get; set; }
    public double Price { get; set; }
    public double Speed { get; set; }
}

To return two results, you can either return a Tuple or pass your parameters by reference.

public static void GetMaxAndAverageFromCars(IEnumerable<Car> cars, ref double maxSpeed, ref double avgSpeed) {
    maxSpeed = cars.Max(c => c.Speed);
    avgSpeed = cars.Average(c => c.Speed);
}

Use like this:

using System.Linq;

var cars = new Car[]
{
    // ..
};

double maxSpeed = 0.0;
double avgSpeed = 0.0;

GetMaxAndAverageFromCars(cars, ref maxSpeed, ref avgSpeed);
0
Dylan Nicholson On

Unless performance is critical, doing it multiple steps is fine:

a) find the maximum speed (hint: you can use .Select(..) and .Max( ) from System.Linq)
b) then find the car (s) that have that speed (.Where(...))
c) calculating the average can likewise been done using .Select( ) and .Average( )

Yes, in principle you could do it all in a single hand-written loop but at the cost of readability/maintainability.