-2

I have

string[] ColorsArray = new string[12] { "Blue", "Red", "Green", 
"Yellow", "Blue", "Green", "Blue", "Yellow", "Red", "Green", 
 "Red", "Yellow" };

float[] LengthArray = new float[12] { 1.3f, 1.4f, 5.6f, 1.5f, 
3.5f, 5.4f, 1.2f, 6.5f, 4.4f, 4.1f, 3.3f, 4.9f };

I want to match them up so that each index equals the other. So ColorsArray[0] which is Blue, equals LengthArray[0], which is 1.3f

The color is a fish. So each color is a fish and each corresponding number is the length of that fish. I need to be able to take what the user gives me (Blue, Red, Green Yellow) and then find out what the Biggest Fish is.

The goal is to match them all up, and then be able to compare the values for each color. So there are 4 colors and 3 duplicates, for a total of 12 "fish".

Apparently a For Loop can be used, but I am not sure how.

Xoax
  • 77
  • 4
  • 13
  • 2
    Please show us your desired result. – GregorMohorko Jun 02 '17 at 21:08
  • Do you know how to write a `for` loop? – Kenneth K. Jun 02 '17 at 21:09
  • You want to use the same index for both, so you need a single `for` loop and then use the loop counter as your index in both arrays. But as already mentioned, since you don't describe how you want to combine the two items, we can't advise you any further than that. – Jack A. Jun 02 '17 at 21:11
  • how can "Blue" equal 1.3 ?!! – niceman Jun 02 '17 at 21:20
  • The color is a fish. So each color is a fish and each corresponding number is the length of that fish. I need to be able to take what the user gives me (Blue, Red, Green Yellow) and then find out what the Biggest Fish is. – Xoax Jun 02 '17 at 21:25

5 Answers5

0

The following code finds maximum lengths for each fish type:

var query = from 
    fish in ColorsArray.Select((t, i) => new { fishName = t, fishLength = LengthArray[i]})
    group fish by fish.fishName
    into team
    select new
    {
        fishName = team.Key,
        maxLength = team.Max(item => item.fishLength)
    };

   // get max length of fish by name
   var maxLength = query.FirstOrDefault(fish => fish.fishName == "Blue")?.maxLength;
sasha_gud
  • 1,635
  • 13
  • 18
0

You could combine the lengths by color in a Dictionary and then simply access them.

var colors = new Dictionary<string,List<float>>();

for(int i = ColorsArray.Length-1; i >= 0; --i){
    string color = ColorsArray[i];
    float length = LengthArray[i];
    if(!colors.ContainsKey(color)){
        colors.Add(color, new List<float>());
    }
    colors[color].Add(length);
}

// largest length of a specific color:
float largestBlueLength = colors["Blue"].Max(); // 3.5
float largestRedLength = colors["Red"].Max(); // 4.4
// etc.

// getting all lengths of a specific color:
List<float> lengthsOfBlue = colors["Blue"]; // { 1.2, 3.5, 1.3 }
List<float> lengthsOfRed = colors["Red"]; // { 3.3, 4.4, 1.4 }
// etc.
GregorMohorko
  • 2,739
  • 2
  • 22
  • 33
0

Here is a simple extension method...

public static T2 ItemInOtherCollection<T1, T2>(this IEnumerable<T1> items, IEnumerable<T2> otherItems, T1 item)
{
    var list = items.ToList();
    return otherItems.ElementAt(list.IndexOf(item));
}

You can use it like this...

var item1 = LengthArray.ItemInOtherCollection(ColorsArray, 5.6f);
var item2 = ColorsArray.ItemInOtherCollection(LengthArray, "Blue");
Blake Thingstad
  • 1,639
  • 2
  • 12
  • 19
0

Here's a LINQ solution:

Dictionary<string, float> biggestFishDictionary = ColorsArray
    .Zip(LengthArray, (color, length) => new { color, length })
    .GroupBy(pair => pair.color)
    .ToDictionary(g => g.Key, g => g.Max(pair => pair.length));

Returns { { "Blue", 3.5f }, { "Red", 4.4f }, { "Green", 5.6f }, { "Yellow", 6.5f } }

budi
  • 6,351
  • 10
  • 55
  • 80
0

Since this seems like homework, we probably shouldn't be helping...

Without extra collections, etc. and possibly .Net 1.0 compliant:

string[] userFishes = Console.ReadLine().Split(',');
string MaxFishName = "";
float MaxFishLength = 0;

foreach (var userFish in userFishes) {
    for (int j1 = 0; j1 < ColorsArray.Length; ++j1) {
        if (ColorsArray[j1] == userFish) {
            if (LengthArray[j1] > MaxFishLength) {
                MaxFishLength = LengthArray[j1];
                MaxFishName = userFish;
            }
        }
    }
}

Console.WriteLine(String.Format("User's largest fish is {0} at {1} feet", MaxFishName, MaxFishLength));

Of course, I would rename your variables to be friendlier (ColorsArray would be betters as FishNames and LengthArray (singular?) as FishLengths) and use LINQ in real life:

var FishMaxLengthMap = FishNames.Zip(FishLengths, (fishName, fishLen) => new { fishName, fishLen }).GroupBy(fnl => fnl.fishName, (fishName, fnlg) => new { fishName, MaxLen = fnlg.Max(fnl => fnl.fishLen)}).ToDictionary(fnml => fnml.fishName.ToLower(), fnml => fnml.MaxLen);

var userFishNames = Console.ReadLine().Split(',').Select(fn => fn.Trim().ToLower());

foreach (var fnl in userFishNames.Select(fn => new { fishName = fn, fishLen = FishMaxLengthMap[fn] }).Where(fml => fml.fishLen == userFishNames.Select(fn => FishMaxLengthMap[fn]).Max()))
    Console.WriteLine($"Largest fish is {fnl.fishName} with length {fnl.fishLen} feet.");
NetMage
  • 26,163
  • 3
  • 34
  • 55