0

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},

        };

    }
}
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Jakobson
  • 101
  • 1
  • 2
  • 10

3 Answers3

0

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.

Dylan Nicholson
  • 1,301
  • 9
  • 23
0

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;
        }
    }
kylew
  • 150
  • 1
  • 11
  • This is what I was looking for, I was something similar and I figured it out, but I did not realize it,thanks ! – Jakobson Nov 15 '17 at 20:06
0

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);
Kyle
  • 5,407
  • 6
  • 32
  • 47
  • Thanks for the advice, your code looks much more readable than my code, and it's shortened, just for my experience is a bit too much, I'll have to go through a few facts that I'm not very clear like the library system.linq,and IEnumerable what is it,but this advice is so good,and that will help me in further progress – Jakobson Nov 15 '17 at 20:08
  • @Mark0n1 LINQ is wonderful... It's a library with some very useful functions like `.Where`, `.First`, etc. and allows you to `select` and `join` to manipulate data. `IEnumerable` is an interface that anything can implement... for beginner's purposes though, I would just stick to the fact that if it's an `IEnumerable` ==> then you can do `foreach` on it. An array is an IEnumerable, so is a list... The advantage to using an interface is that your method doesn't care if it's an array OR a list: So you can do this: `GetAverage(myArray)` and `GetAverage(myList)`. – Kyle Nov 16 '17 at 15:15
  • Today I just thought about that library, and a little I've been researching it has very good functions, I would not even know about it. I assume it's even easier to do your work when someone knows to work with it. I also noticed that it was IEnumerable is interface, but I just need to learn about it, delegates, exceptions ...Thanks a lot again ! – Jakobson Nov 17 '17 at 01:07