I am trying to find the "player" with the highest salary from an array of "BaseBallPlayer" objects. I know I need to loop through the array and compare the salary properties with an "if" statement. However, I cant seem to figure out how to access a specific property from the objects in the array.
here is a snippet of my main():
static void Main(string[] args)
{
BasketBallPlayer basketBP1 = new BasketBallPlayer("Jeff", 24, 30000.00, 12.2, 6, 7, 8);
BasketBallPlayer basketBP2 = new BasketBallPlayer("Jim", 27, 35000, 18, 5, 17, 9);
BasketBallPlayer basketBP3 = new BasketBallPlayer("James", 32, 65000, 34, 87, 15, 12);
object[] BasketBT = new object[] {basketBP1, basketBP2, basketBP3 };
BaseBallPlayer baseBP1 = new BaseBallPlayer("Craig", 26, 53000, 53, 12, 9);
BaseBallPlayer baseBP2 = new BaseBallPlayer("Chris", 35, 66000, 67, 19, 7);
BaseBallPlayer baseBP3 = new BaseBallPlayer("Charlie", 32, 75000, 87, 23, 4);
object[] BaseBT = new object[] { baseBP1, baseBP2, baseBP3 };
foreach (object player in BaseBT)
{
}
}
here is the parent class of BaseBallPlayer:
{
protected int age { get; set; }
protected string name { get; set; }
public double salary { get; set; }
public sportsPlayer(string Name, int Age, double Salary)
{
this.name = Name;
this.age = Age;
this.salary = Salary;
}
public override string ToString()
{
string details = string.Format("Name: {0} \n Age: {1} \n Salary: {2} \n ", this.name, this.age, this.salary);
return details;
}
}
And here is the class BaseBallPlayer:
class BaseBallPlayer: sportsPlayer
{
double battingAverage { get; set; }
int homeRuns { get; set; }
int errors { get; set; }
public BaseBallPlayer(string Name, int Age, double Salary, double BattingAverage, int HomeRuns, int Errors): base(Name, Age, Salary)
{
this.battingAverage = BattingAverage;
this.homeRuns = HomeRuns;
this.errors = Errors;
}
public override string ToString()
{
string details = string.Format("Batting Average: {0} \n Home Runs: {1} \n Errors: {2} \n", this.battingAverage, this.homeRuns, this.errors);
return base.ToString() + details;
}
}
In order to access properties in a type, you should declare variables of that type and not the object type, so your code should look like this in order to be able to access player.salary :
static void Main(string[] args)
{
BasketBallPlayer basketBP1 = new BasketBallPlayer("Jeff", 24, 30000.00, 12.2, 6, 7, 8);
BasketBallPlayer basketBP2 = new BasketBallPlayer("Jim", 27, 35000, 18, 5, 17, 9);
BasketBallPlayer basketBP3 = new BasketBallPlayer("James", 32, 65000, 34, 87, 15, 12);
sportsPlayer[] BasketBT = new object[] {basketBP1, basketBP2, basketBP3 };
BaseBallPlayer baseBP1 = new BaseBallPlayer("Craig", 26, 53000, 53, 12, 9);
BaseBallPlayer baseBP2 = new BaseBallPlayer("Chris", 35, 66000, 67, 19, 7);
BaseBallPlayer baseBP3 = new BaseBallPlayer("Charlie", 32, 75000, 87, 23, 4);
sportsPlayer[] BaseBT = new object[] { baseBP1, baseBP2, baseBP3 };
foreach (sportsPlayer player in BaseBT)
{
// here you can access player.salary
}
}
you have to fix a type of BaseBT as a BaseBallPlayer[]
BaseBallPlayer[] BaseBT = new BaseBallPlayer[] { baseBP1, baseBP2, baseBP3 };
player with max salary
var playerMaxSalary= BaseBT.OrderByDescending(b=>b.salary).FirstOrDefault();
result
Name: Charlie
Age: 32
Salary: 75000
Batting Average: 87
Home Runs: 23
Errors: 4
test
foreach (var player in BaseBT)
{
Console.WriteLine(player.ToString());
}
result:
Name: Craig
Age: 26
Salary: 53000
Batting Average: 53
Home Runs: 12
Errors: 9
Name: Chris
Age: 35
Salary: 66000
Batting Average: 67
Home Runs: 19
Errors: 7
Name: Charlie
Age: 32
Salary: 75000
Batting Average: 87
Home Runs: 23
Errors: 4
you will have the same reslt if you define BaseBT as a List of BaseBallPlayer
List<BaseBallPlayer> BaseBT = new List<BaseBallPlayer> { baseBP1, baseBP2, baseBP3};
Related
I need help in LINQ, I need to group a list of students based on the calculated average in a rage
There is a student class :
public class Student
{
public string Name { get; set; } // student name
public int[] Scores { get; set; } // test scores
}
and I have a list of students with the data
List<Student> students = new List<Student>
{
new Student { Name = "Michael", Scores = new int[] { 94, 92, 91, 91 } },
new Student { Name = "Isabelle", Scores = new int[] { 66, 87, 65, 93, 86} },
new Student { Name = "Chastity", Scores = new int[] { 76, 61, 73, 66, 54} },
new Student { Name = "Chaim", Scores = new int[] { 94, 55, 82, 62, 52} },
new Student { Name = "Patience", Scores = new int[] { 91, 79, 58, 63, 55} },
new Student { Name = "Echo", Scores = new int[] { 74, 85, 73, 75, 86} },
new Student { Name = "Pamela", Scores = new int[] { 73, 64, 53, 72, 68} },
new Student { Name = "Anne", Scores = new int[] { 78, 96, 52, 79, 60} },
new Student { Name = "Fuller", Scores = new int[] { 59, 68, 88, 85, 76} },
new Student { Name = "Cameron", Scores = new int[] { 70, 73, 75, 51, 98} },
new Student { Name = "Aurora", Scores = new int[] { 65, 70, 53, 80, 52} },
new Student { Name = "Anthony", Scores = new int[] { 68, 69, 94, 88, 98} },
}
the bracket I need to range is in Test score brackets are in multiples of 10
• 50, 60, 70, 80, 90, 100
The output should look like:
Something like that I suppose ?
Result:
--------------------------
90
Name: Michael
--------------------------
--------------------------
80
Name: Isabelle
Name: Echo
Name: Fuller
Name: Anthony
--------------------------
--------------------------
70
Name: Chastity
Name: Chaim
Name: Patience
Name: Pamela
Name: Anne
Name: Cameron
--------------------------
--------------------------
60
Name: Aurora
--------------------------
Code:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
internal static class Program
{
private static void Main()
{
var students = new List<Student>
{
new() {Name = "Michael", Scores = new[] {94, 92, 91, 91}},
new() {Name = "Isabelle", Scores = new[] {66, 87, 65, 93, 86}},
new() {Name = "Chastity", Scores = new[] {76, 61, 73, 66, 54}},
new() {Name = "Chaim", Scores = new[] {94, 55, 82, 62, 52}},
new() {Name = "Patience", Scores = new[] {91, 79, 58, 63, 55}},
new() {Name = "Echo", Scores = new[] {74, 85, 73, 75, 86}},
new() {Name = "Pamela", Scores = new[] {73, 64, 53, 72, 68}},
new() {Name = "Anne", Scores = new[] {78, 96, 52, 79, 60}},
new() {Name = "Fuller", Scores = new[] {59, 68, 88, 85, 76}},
new() {Name = "Cameron", Scores = new[] {70, 73, 75, 51, 98}},
new() {Name = "Aurora", Scores = new[] {65, 70, 53, 80, 52}},
new() {Name = "Anthony", Scores = new[] {68, 69, 94, 88, 98}}
};
var groups = students.GroupBy(s => Math.Round(s.Scores.Average() / 10) * 10).OrderByDescending(s => s.Key);
foreach (var grouping in groups)
{
Console.WriteLine("--------------------------");
Console.WriteLine(grouping.Key);
foreach (var student in grouping)
{
Console.WriteLine(student);
}
Console.WriteLine("--------------------------");
Console.WriteLine();
}
}
}
public class Student
{
public string Name { get; set; }
public int[] Scores { get; set; }
public override string ToString()
{
return $"{nameof(Name)}: {Name}";
}
}
}
I need to loop through a 2D array of ints that is 4x4, but I also have to create 4 2x2 arrays out of it. Then I have to loop through each of those 4 2x2 arrays to pick out the average of the numbers in each 2x2 array.
public int[,] Reduced(Sampler sampler)
{
int[,] a = new int[SampleSize,SampleSize];
for (int r = 0; r < Math.Sqrt(image.Length); r+=SampleSize)
{
for (int c = 0; c < Math.Sqrt(image.Length); c+=SampleSize)
{
InsideLoop(a, r, c);
}
}
return a;
}
private void InsideLoop(int[,] a, int r, int c)
{
for (r = 0; r < SampleSize; r++)
{
for (c = 0; c < SampleSize; c++)
{
a[r, c] = image[r, c];
Console.WriteLine("Value: {0}", a[r, c]);
}
}
}
This is essentially what I've got so far, but it's working how it's written instead of how I'd like it to work. For this example, SampleSize is a variable that is set to 2. What this does currently is print out the numbers that create the first 2x2 array four times. My laptop battery is about to die, so I can't elborate more, but if anyone has any tips while I'm driving home. I had to finish posting this on my phone.
Does this work?
int sampleSize = 2;
int[,] data = {
{1, 2, 3, 4 },
{5, 6, 7, 8 },
{9, 10, 11, 12 },
{13, 14, 15, 16 }
};
//assume input data is a perfect square as per your example
int max = (int)Math.Sqrt(data.Length);
List<int[,]> samples = new List<int[,]>();
int startX = 0;
while (startX + sampleSize <= max)
{
int startY = 0;
while (startY + sampleSize <= max)
{
int[,] sample = new int[sampleSize, sampleSize];
for (int x = 0; x < sampleSize;x++)
{
for (int y = 0; y < sampleSize; y++)
{
sample[x, y] = data[x + startX, y + startY];
}
}
samples.Add(sample);
startY += sampleSize;
}
startX += sampleSize;
}
//for output testing
foreach (int[,] sample in samples)
{
Console.WriteLine(sample[0, 0].ToString().PadLeft(2) + " | " + sample[0, 1]);
Console.WriteLine(" ----- ");
Console.WriteLine(sample[1, 0].ToString().PadLeft(2) + " | " + sample[1, 1]);
Console.WriteLine();
Console.WriteLine();
}
Console.ReadLine();
and here's the output
1 | 2
-----
5 | 6
3 | 4
-----
7 | 8
9 | 10
-----
13 | 14
11 | 12
-----
15 | 16
Generalized version:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
int[,] original = new int[,] { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int[,] harder = new int[,] { { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 10, 11, 12, 13, 14, 15, 16, 17, 18 },
{ 19, 20, 21, 22, 23, 24, 25, 26, 27 },
{ 28, 29, 30, 31, 32, 33, 34, 35, 36 },
{ 37, 38, 39, 40, 41, 42, 43, 44, 45 },
{ 46, 47, 48, 49, 50, 51, 52, 53, 54 },
{ 55, 56, 57, 58, 59, 60, 61, 62, 63 },
{ 64, 65, 66, 67, 68, 69, 70, 71, 72 },
{ 73, 74, 75, 76, 77, 78, 79, 80, 81 } };
IterateArray(original);
Console.ReadLine();
}
static void IterateArray(int[,] array)
{
double tDim = Math.Sqrt(Math.Sqrt(array.Length));
int dim = (int)tDim;
if (dim != tDim) throw new ArgumentException("Not a valid array!");
for (int i = 0; i < dim; i++)
{
IterateRows(array, dim, i);
}
}
static void IterateRows(int[,] array, int dim, int pass)
{
int maxRow = dim * dim;
IList<int> list = new List<int>(maxRow);
for (int curRow = 0; curRow < maxRow; curRow++)
{
IterateColumns(array, dim, curRow, pass, list);
if (list.Count == maxRow)
{
PrintNewArray(list, dim);
list.Clear();
}
}
}
static void IterateColumns(int[,] array, int dim, int row, int pass, IList<int> list)
{
int maxCol = dim + (dim * pass);
for (int curCol = pass * dim; curCol < maxCol; curCol++)
{
list.Add(array[row, curCol]);
}
}
static void PrintNewArray(IList<int> list, int dim)
{
for(int i = 0; i < list.Count; i++)
{
if (i % dim == 0)
{
Console.WriteLine();
}
Console.Write($"{list[i]} ");
}
Console.WriteLine($"\nAverage {list.Average()}");
}
}
how can I find the printer type which is installed in our PC, whether is it dot-Matrix, laser or inkjet in c#?
The following class gives you whether the printer is dot-Matrix, laser or inkjet:
using System;
using System.Management;
namespace ConsoleDemo
{
class Printer
{
public enum TechnologyType
{
Other = 1,
Unknown = 2,
Electrophotographic_LED = 3,
Electrophotographic_Laser = 4,
Electrophotographic_Other = 5,
Impact_Moving_Head_Dot_Matrix_9pin = 6,
Impact_Moving_Head_Dot_Matrix_24pin = 7,
Impact_Moving_Head_Dot_Matrix_Other = 8,
Impact_Moving_Head_Fully_Formed = 9,
Impact_Band = 10,
Impact_Other = 11,
Inkjet_Aqueous = 12,
Inkjet_Solid = 13,
Inkjet_Other = 14,
Pen_ = 15,
Thermal_Transfer = 16,
Thermal_Sensitive = 17,
Thermal_Diffusion = 18,
Thermal_Other = 19,
Electroerosion = 20,
Electrostatic = 21,
Photographic_Microfiche = 22,
Photographic_Imagesetter = 23,
Photographic_Other = 24,
Ion_Deposition = 25,
eBeam = 26,
Typesetter = 27
}
public static void GetPrinterInfo()
{
var printerQuery = new ManagementObjectSearcher("SELECT * from Win32_Printer");
foreach (var printer in printerQuery.Get())
{
var name = printer.GetPropertyValue("Name");
var status = printer.GetPropertyValue("Status");
var isDefault = printer.GetPropertyValue("Description");
var MarkingTechnology = printer.GetPropertyValue("MarkingTechnology");
var CurrentCapabilities = (string )printer.GetPropertyValue("CurrentCapabilities");
Console.WriteLine("Name:{0} |(Status: {1} | Description: {2}| Technology: {3} | {4} ",
name, status, isDefault, MarkingTechnology , CurrentCapabilities);
}
}
}
}
The enum TechnologyType gives you the type of printer technology.
For more information review: Win32_Printer class
I would like to know how to take one element from each item in a list and put it into an array for graphing purposes. I was wondering if, since I want to graph each element in each row of the list, could I do it all at once or would I have to separately pull each element out into its own array?
A few lines of a code example is my preferred learning method and would be much appreciated.
Thanks!
You can conceptually put the logic to do this in one place.
class DataIterator<T> : IEnumerable<T[]> {
private readonly IList<IList<T>> _lists;
public DataIterator(IList<IList<T>> lists) {
Contract.Assert(lists.All(l => l.Count == lists[0].Count));
_lists = lists;
}
public IEnumerator<T[]> GetEnumerator() {
var value = new List<T>(_lists.Count);
for (var i = 0; i < _lists[0].Count; i++) {
value.AddRange(_lists.Select(t => t[i]));
yield return value.ToArray();
value = new List<T>(_lists.Count);
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}
This implements IEnumerable<T> so you can use it which foreach loops.
An example of use:
var lists = new List<IList<int>> {
new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
new List<int> { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 },
new List<int> { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 }
};
var iter = new DataIterator<int>(lists);
foreach (var items in iter) {
Array.ForEach(items, i => {
Console.Write("{0:D2} ", i);
});
Console.WriteLine();
}
outputs:
01 11 21
02 12 12
...
If I have 100 lists (eg x1 to x100), is there a better way of expressing the final line of code?
var x1 = new List<int>() { 75 };
var x2 = new List<int>() { 95, 64 };
var x3 = new List<int>() { 17, 47, 82 };
var x4 = new List<int>() { 18, 35, 87, 10 };
var x5 = new List<int>() { 20, 04, 82, 47, 65 };
var x6 = new List<int>() { 19, 01, 23, 75, 03, 34 };
var x7 = new List<int>() { 88, 02, 77, 73, 07, 63, 67 };
//etc..
var listOfListOfInts = new List<List<int>>() { x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15 };
possibly a Dictionary and a for loop to reference all the x1..100.
As long as x1 etc. aren't referenced elsewhere, then write:
var listOfListOfInts = new List<List<int>>()
{
new List<int>() { 75 },
new List<int>() { 95, 64 },
//etc.
};
In fact you shouldn't need to reference the individual variables elsewhere since listOfListOfInts[0] is just as good as x1 for example.
Do you really need these to be of type List<T>? It looks like you're setting up preinitialized data. If you're never going to change the length of any of these "lists", you could use arrays instead; the syntax is more compact:
var listOfListOfInts = new[] {
new[] { 75 },
new[] { 95, 64 },
new[] { 17, 47, 82 },
new[] { 18, 35, 87, 10 },
new[] { 20, 04, 82, 47, 65 },
new[] { 19, 01, 23, 75, 03, 34 },
new[] { 88, 02, 77, 73, 07, 63, 67 },
// ...
};
Perhaps i'm over complicating things but you could do something like
public interface IClass1
{
IList<IList<int>> ListList { get; set; }
void AddList(List<int> nList);
}
public class Class1 : IClass1
{
public IList<IList<int>> ListList { get; set; }
public void AddList(List<int> nList)
{
ListList.Add(nList);
}
}
and then use it like:
public class Create1
{
public Create1()
{
IClass1 iClass1 = new Class1();
iClass1.AddList(new List<int>() { 75 });
}
}