0

This is for my school project and therefor I need to code without LINQ methods etc. I'm trying to show which city has the highest temperature by finding the highest temp and then print it together with the city name.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class City
{
    public string name { get; set; }
    public int temp { get; set; }


    public override string ToString()
    {
        return $"CITY:\t{name}\nTEMP:\t{temp}°C\n";
    }
}

I want to use Tostring() to print.

//=====Highest Temperature=====//
static void HighestTemp(City[] cities)
{
    int highest = cities[0].temp;
    
    for (int i = 1; i < cities.Length; i++)
    {
        if (highest < cities[i].temp)
        {
            highest = cities[i].temp;          
        } 
    }
    
}

I've found the highest temperature. But the question is: How do I print it with ToString()?? I want both cities[i].name and cities[i].temp

  • @AlexeiLevenkov I assume you closed a "I **don't** want to use LINQ to.." as a duplicate of a "how do I use LINQ to..." because the dupe target contains implementations of MaxBy for net versions that don't have it, and ergo the OP could take one of those and use it in a non LINQ way in their code, but a comment making that clear would be welcome! – Caius Jard May 08 '22 at 05:06
  • don't track your max using an `int`, track it using a `City` i.e.`City highest = cities[0]`, loop in the same way `for (int i = 1; i < cities.Length; i++)` but get the temp property from highest var when comparing eg `if (highest.temp < cities[i].temp)...` then at the end of the op the highest is the whole city, not just the temp – Caius Jard May 08 '22 at 05:10
  • Please note that we name public properties and fields in PascalCase in C#; your `temp` should be called Temp etc. it might seem picky and trivial but eventually you will come to appreciate why we do it.. – Caius Jard May 08 '22 at 05:14

2 Answers2

1

Your issue is you found the highest temp, not the city with the highest temp.

Without using Linq or any of the shortcut methods like .OrderBy(..) or .OrderByDescending(..), you can simply just store a second variable that tracks your best i

int highestTemp = cities[0].temp;
int highestTempIndex = 0;

for (int i = 1; i < cities.Length; i++)
{
    if (highest < cities[i].temp)
    {
        highest = cities[i].temp;
        highestTempIndex = i;          
    } 
}

var highestTempCity = cities[highestTempIndex];

And to stringify the city its as simple as:

$"{highestTempCity}"

Because overriding specifically .ToString() automates this. You also can call highestTempCity.ToString() if you like.

If you utilize the power of Linq though you can simplify this a lot by just doing:

var highestTempCity = cities.OrderByDescending(city => city.temp).First();
-1

SOLVED!
I succeeded by doing like this:

//=====Highest temperature=====//
static int HighestTemp(City[] cities, int n)
{
    int highestTemp = cities[0].temp;
    var highestTempIndex = 0;



    for (int i = 1; i < n; i++)
    {
        if (highestTemp < cities[i].temp)
        {
            highestTemp = cities[i].temp;
            highestTempIndex = i;
        }
    }
   return highestTempIndex;
}

From what this method return I made int Index and print the result in Main

//----------City with HIGHEST temperature----------//
index = HighestTemp(cities, n);
Console.WriteLine(cities[index].ToString());
  • This is *exactly the same* code that shown in the other answer - I'm not exactly sure why you think adding another copy was useful. Also `Console.WriteLine(cities[index].ToString());` is pointless suggestion - ``Console.WriteLine(cities[index]);` is shorter and means exactly the same thing. – Alexei Levenkov May 09 '22 at 17:24
  • 1
    Is it really *exactly the same code*? If you look again you will see some diffrence. The code in the other answer did not work and therefor I had to change it a bit. Also `.ToString` as you can see in `Class City` is returning something that I want. – Jerry Ohlson May 09 '22 at 18:55
  • you were right about that `Console.WriteLine(cities[index]` works fine without `.ToString()`. Thanks! – Jerry Ohlson May 09 '22 at 21:35