Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hi, i have a problem with this question
Write a program in C# using classes then suppose you have an array in (4,6) size of
Players. (ID, Name, Tallness, Weight).
ID, Name, Tallness, Weight

101 102 103 104 105 106
U V W X Y Z
155cm 180cm 174cm 168cm 165cm 177cm
75kg 70kg 65kg 68kg 80kg 63kg

Find the following:
1. Print ID and Name of player who is Tallest between them.
2. Print ID and Name of player who get highest weight.
3. Print ID and Name of each player who have a weight of less than 70g.
4. Find the average of all players tallness and weights.

anyone can solve this?

What I have tried:

C#
class player
        {
            string[,] ply = new string[4, 6]
                { {"101","102","103","104","105","106" },
                {"u","v","w","q","a","s"},
                {"155cm","145cm","150cm","156cm","140cm","130cm"},
                {"50kg","20kg","70kg","54kg","66kg","90kg"} };
}
Posted
Updated 14-Feb-18 4:45am
v2
Comments
F-ES Sitecore 14-Feb-18 9:35am    
We're not here to do your homework for you.
Member 13678067 14-Feb-18 9:38am    
first of all if you don't know what is this don't comment
this is not a homework, if I understand this i will know how class is worked
#realJSOP 14-Feb-18 9:54am    
If it's not homework, what is it? No requirement for a real-world application would be stated this way. No programmer in his right mind would use an array instead of some sort of IEnumerable collection. The only programmer that could not solve this is a STUDENT that hasn't been paying attention in class.

Lastly, your code makes it obvious that you don't understand the question. If we do your homework for you, your instructor will be able to (or at least SHOULD be able to) determine that you did not write the code.
johannesnestler 14-Feb-18 10:02am    
sure anyone with some experience could solve this - for shure all that still bother answering such questions (despite the fact that you ignored the Forum rules) like me or John. But my education is over, I don't do homework anymore - I do work. Homework is for learning - you can only do this for yourself. No solution to your "requirement" can be of any value in the real world. If I give help or advice here, it should be a "real Problem" for someone at least - not just a lazy Student who doesn't want to do his homework. Sry
ZurdoDev 14-Feb-18 11:08am    
We should be willing to help, homework or not. I hate when people say "This is homework so we won't help you."

Well despite your comment above this is an obvious homework question. But since the question does not make much sense I will give you a suggestion.

Firstly, learn how to create classes in C#: Classes (C# Programming Guide) | Microsoft Docs[^].

Secondly ask your teacher what is meant by the statement "then suppose you have an array in (4,6) size". Does it mean 4 objects of this class, or 4 groups of 6 objects?
 
Share this answer
 
Comments
#realJSOP 14-Feb-18 10:18am    
I gave him the codez. Check it out. :)
Richard MacCutchan 14-Feb-18 10:47am    
I had to give you a 5 for that, since it is correct.
#realJSOP 14-Feb-18 10:51am    
It does indeed produce the results he's after. I wonder how his instructor will react to the use of Random, since it obviously hasn't come up in class yet... :)
David Crow 14-Feb-18 11:21am    
"It does indeed produce the results he's after."

But it does not print or find anything. Isn't that what he's ultimately after?
#realJSOP 14-Feb-18 11:27am    
Well, he can't print or find anything until he gets the array built correctly, but I edited the answer to fulfill all of the requirements so he didn't have to do it himself.
Here, hand this in. It creates a 4x6 array of players...

(I'm gonna venture a guess that this is the only solution you get that has code in it.)

C#
public class Player
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Height { get; set; }
    public int Weight { get; set; }
}

public class Players
{
    public double AvgWeight
    {
        get
        {
            List<Player> flat = this.Flatten(this.ThePlayers).ToList();
            return Math.Round(flat.Average(x=>x.Weight), 1);
        }
    }
    public double AvgHeight
    {
        get
        {
            List<Player> flat = this.Flatten(this.ThePlayers).ToList();
            return Math.Round(flat.Average(x=>x.Height),1);
        }
    }
    public string Tallest
    {
        get
        {
            List<Player> flat = this.Flatten(this.ThePlayers).ToList();
            Player player = flat.FirstOrDefault(x=>x.Height == flat.Max(y=>y.Height));
            return string.Format("Tallest player: {0} {1}", player.ID, player.Name);
        }
    }

    public string Heaviest
    {
        get
        {
            List<Player> flat = this.Flatten(this.ThePlayers).ToList();
            Player player = flat.FirstOrDefault(x=>x.Weight == flat.Max(y=>y.Weight));
            return string.Format("Tallest player: {0} {1}", player.ID, player.Name);
        }
    }

    public int RandomWeight()
    {
        return random.Next(63,80);
    }

    public int RandomHeight()
    {
        return random.Next(155,180);
    }

    public IEnumerable<T> Flatten<T>(T[,] map) 
    {
        for (int row = 0; row < map.GetLength(0); row++) 
        {
            for (int col = 0; col < map.GetLength(1); col++) 
            {
                yield return map[row,col];
            }
        }
    }

    private Player[,] ThePlayers = null;

    Random random = new Random();

    public Players()
    {
        this.ThePlayers = new Player[4,6]
        {
            { 
                new Player(){ID=101, Height=RandomHeight(), Name="Adam", Weight=random.Next(63,80)},
                new Player(){ID=102, Height=RandomHeight(), Name="Zach", Weight=random.Next(63,80)},
                new Player(){ID=103, Height=RandomHeight(), Name="Andy", Weight=random.Next(63,80)},
                new Player(){ID=104, Height=RandomHeight(), Name="Bob", Weight=random.Next(63,80)},
                new Player(){ID=105, Height=RandomHeight(), Name="Sam", Weight=random.Next(63,80)},
                new Player(){ID=106, Height=RandomHeight(), Name="John", Weight=random.Next(63,80)}
            },
            { 
                new Player(){ID=107, Height=random.Next(155,180), Name="Pradeep", Weight=random.Next(63,80)},
                new Player(){ID=108, Height=random.Next(155,180), Name="Panda", Weight=random.Next(63,80)},
                new Player(){ID=109, Height=random.Next(155,180), Name="Deepak", Weight=random.Next(63,80)},
                new Player(){ID=110, Height=random.Next(155,180), Name="Reetard", Weight=random.Next(63,80)},
                new Player(){ID=111, Height=random.Next(155,180), Name="Dumass", Weight=random.Next(63,80)},
                new Player(){ID=112, Height=random.Next(155,180), Name="Skinflute", Weight=random.Next(63,80)}
            },
            { 
                new Player(){ID=113, Height=random.Next(155,180), Name="Analpore", Weight=random.Next(63,80)},
                new Player(){ID=114, Height=random.Next(155,180), Name="Sumyunguy", Weight=random.Next(63,80)},
                new Player(){ID=115, Height=random.Next(155,180), Name="Tittiefreak", Weight=random.Next(63,80)},
                new Player(){ID=116, Height=random.Next(155,180), Name="Carpetmuncher", Weight=random.Next(63,80)},
                new Player(){ID=117, Height=random.Next(155,180), Name="Dickwad", Weight=random.Next(63,80)},
                new Player(){ID=118, Height=random.Next(155,180), Name="Numnuts", Weight=random.Next(63,80)}
            },
            { 
                new Player(){ID=119, Height=random.Next(155,180), Name="Asspilot", Weight=random.Next(63,80)},
                new Player(){ID=120, Height=random.Next(155,180), Name="Budonkidonks", Weight=random.Next(63,80)},
                new Player(){ID=121, Height=random.Next(155,180), Name="Lactator", Weight=random.Next(63,80)},
                new Player(){ID=122, Height=random.Next(155,180), Name="Loverope", Weight=random.Next(63,80)},
                new Player(){ID=123, Height=random.Next(155,180), Name="Pearlchin", Weight=random.Next(63,80)},
                new Player(){ID=124, Height=random.Next(155,180), Name="Bukake", Weight=random.Next(63,80)}
            }

        };
    }

    public List<Player> GetUnderWeight(int weight)
    {
        List<Player> players = this.Flatten(this.ThePlayers).Where(x=>x.Weight < 70).ToList();
        return players;
    }

}


Usage:

C#
class Program
{
    static void Main(string[] args)
    {
        Players players = new Players();
        double avgHeight = players.AvgHeight;
        double avgWeight = players.AvgWeight;
        string giant = players.Tallest;
        string tubbo = players.Heaviest;
        List<Player> wimps = players.GetUnderWeight(70);
    }
}
 
Share this answer
 
v8
Comments
Richard MacCutchan 14-Feb-18 10:46am    
ROFL!
johannesnestler 14-Feb-18 12:46pm    
nice examples John ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900