C

Join Server

# ❔ Hello everyone, can someone help me out understand this part!

FFaraj12/21/2022
`````` Search and sort algorithm
Searching and sorting are important tools in programming. We will study some
search and sorting algorithms.
Search
Searching is the process of looking for a particular value in a data set. The searched values are called
keys.
Linear search
Linear search is a simple sorting method used when performing the search in a
unsorted array. The algorithm goes through the entire array for index i = 0, 1, 2, ...,
arr.length-1. If key = arr[i] then index = i, the loop is terminated and index is returned.
If the key is not found in the entire array, -1 is returned. Average complexity for
the linear search algorithm is O(n/2) and in the worst case the complexity is O(n)``````
AAnu6is12/21/2022
That's a lot of text, understand what exactly?
FFaraj12/21/2022
The bottom line is that I'm trying to learn `Search and sort algorithm` do u have any tips or resources to learn this?
FFaraj12/21/2022
Is this feel unclear?
FFaraj12/21/2022
I'm just trying to learn how to use this concept 🙂

"searching algorithms"/"lookup algorithms" and "sorting algorithms"
https://algorithm-visualizer.org/brute-force/bubble-sort and there are sites like this that let you play a visualization of the operations
FFaraj12/21/2022
Wow, what a great tips & resources 😃 thanks a lot bro. I will definitely look them up 👍 🙂
FFaraj12/21/2022
Is this related to databases in any sense? Is it complicated to learn? Just getting an idea from your experience 🙂
FFaraj12/21/2022
I did visit the link that you attached, but I don't really know what to do with it 🤷🏻‍♂️ it's not even c# code
TTheBoxyBear12/21/2022
Can be related to databases depending on the use case. Sorting algorithms work with data from an arbitrary source
FFaraj12/21/2022
Ok! Well! Since the assignment from my course requires me to build a weather station console application project and I'm gonna need to use this concept in the project.
FFaraj12/21/2022
NOTE: You should not use the compiler predefined methods/functions. This means that you must write the code yourself for all methods/functions you call in the program.

Create a class city that contains two attributes.

The city class contains two member variables/attributes:

string name;
int temp;

These store the city name and temperature, as well as the ToString function/method

public string ToString()

They return a string containing values in the name and temp attributes.
Note that the ToString() member function/method does not generate any output to the console.
Write a function/method for linear search in a field (array, vector, list) cities with city objects.
int lensesok(field cities, int n, int searchtemp)
The function/method should search a field cities for a city with a specific temperature (temp) and return the index for that city. If no city with the temperature is found -1 is returned. The variable is the number of elements in the field. Write pseudo code and draw flowchart for search method/function.
Also write a function/method that sorts a city field by temperature (coldest first).
void bubble sort(field cities, int n)
Software that can be used to draw diagrams
Test the class
Make a main program to test your class and functions/methods. Declare a field cities with four cities that should contain temperature measurements from four different cities. The values are entered by the user when the program is run.
The program should then sort the cities field and print the cities' names and temperatures in order of temperature with the coldest city first. Finally, the function/method lens yoke must be called and search for whether any city has a certain temperature. You can choose whether this temperature is fixed in the code or entered by the user.
Mmtreit12/22/2022
I got lost at "Linear search is a simple sorting method" since...it's not
FFaraj12/22/2022
Oh! Really, is it because of the complexity?
it's not a sorting method
it's a search as the name implies
AAccord12/23/2022
Was this issue resolved? If so, run `/close` - otherwise I will mark this as stale and this post will be archived until there is new activity.
FFaraj12/23/2022
Based on the assignment above, which one should I use, searching or sorting? Any tips would be appreciated 🤗
FFaraj12/23/2022
I have watched and googled this concept so far and I got an idea theoretically, but when it comes to putting it in code I just don't know how to implement it 🙆🏻‍♂️
idk why you'd implement your own searching or sorting
c# has native implementations that i assume are the fastest for their flexibility
FFaraj12/23/2022
Well! This is what is required for me to implement in the weather station project based on the assignment.
FFaraj12/23/2022
Ok! That's interesting to know. Could u give me an example of what you just mentioned? Do u mean like this: https://www.youtube.com/watch?v=NJ5ghiutzfY&t=4s
LINQ has Find and OrderBy, etc
if you haven't learned LINQ now is a good time
basically, if your goal is to do the assignment in a way that a typical dotnet pro would, use LINQ
if your goal is to learn specific algorithms, study and implement those algorithms
FFaraj12/23/2022
Oh! You're right. Everyone says LINQ is a great tool for implementing various stuff in projects 😃 I will definitely start to learn it.
FFaraj12/23/2022
These are such great tips 🤗 I will try to learn both.
FFaraj12/23/2022
Although searching & sorting algorithms are a bit complicated I guess 💁🏻‍♂️
AAccord12/24/2022
Was this issue resolved? If so, run `/close` - otherwise I will mark this as stale and this post will be archived until there is new activity.
FFaraj12/24/2022
This website looks like a good one for practicing purposes, but sadly this site only provides two different languages Java & c++ but not c# 💁🏻‍♂️
consider it practice to translate the code to c#
FFaraj12/24/2022
Do u mean I should practice in one of the offered languages and then use the same methodology in c#?
EEro12/24/2022
the languages aren't that different
EEro12/24/2022
just translate it directly
FFaraj12/24/2022
Ah! Ok! Now I got the idea 😉
FFaraj12/24/2022
How was your experience in learning this concept? Any tips would be appreciated 🤗
EEro12/24/2022
i didn't
EEro12/24/2022
i use code other people already wrote
EEro12/24/2022
i don't need to know sorting or Big O or anything like that
EEro12/24/2022
it's already been done by many people many times
EEro12/24/2022
so i just don't care and use what they made
FFaraj12/24/2022
Oh! Really. So u didn't go through too many headaches by searching and learning etc...
FFaraj12/24/2022
Maybe I should think about using the same method.
FFaraj12/24/2022
So u mean I should use Google to find a similar code that will fit my project!
EEro12/24/2022
sure
EEro12/24/2022
i don't know if this is an assignment or you're just learning for yourself
EEro12/24/2022
but i generally don't really care at all about learning trivial stuff like this when it's already been solved by millions of people
FFaraj12/24/2022
Ok! I get your point and I totally agree. U get to save a lot of time. For ex: I got this assignment from my course where they want me to build a `weather station` project and they want me to use searching & sorting algorithms. I've been what 7 days in a row trying to figure out what to do 🤷🏻‍♂️
FFaraj12/24/2022
Have I asked someone like u and gotten this tip, I wouldn't waste all these days just circling around and looking for a way to write this program 🙆🏻‍♂️
FFaraj12/24/2022
But at least, I learned something from my mistake.
EEro12/24/2022
well, if it's a course you're making yourself go through, and you don't really know how to implement something like this, it's at least worth trying to understand how the code you take from other people work
EEro12/24/2022
like, i can at least whip a sorting algorithm up if i was asked to
EEro12/24/2022
so it's a crucial skill to understand what you have at your disposal and how to properly put it to use. logical thinking is one of the biggest skills you need in programming
EEro12/24/2022
the rest is being good at googling
EEro12/24/2022
``````// returns the index the value was found at
int LinearSearch(int[] array, int valueToFind)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == valueToFind)
{
return i;
}
}

return -1;
}``````
FFaraj12/24/2022
Ok! You're certainly right about everything u mentioned. Well! I tried to do some searching and find out how the logic of this concept works theoretically but just couldn't put it in a code logically. That's why it took me some days not to end up with an actual solution for the problem.
FFaraj12/24/2022
Wow! This code looks good.
FFaraj12/24/2022
Currently! I'm watching these shorts offered by CS50 https://www.youtube.com/watch?v=ktWL3nN38ZA
FFaraj12/24/2022
Is this code considered a leaner search?
Mmtreit12/24/2022
Linear?
Mmtreit12/24/2022
If you meant linear (not leaner), yes that is pretty much the definition of a linear search
FFaraj12/24/2022
Yes, sorry! I meant linear search. Ok! Great. I will try to implement the project using this code snippet.
FFaraj12/25/2022
Guys I need help with this step
FFaraj12/25/2022
```Create a class city that contains two attributes. The city class contains two member variables/attributes: string name; int temp; These store the city name and temperature, as well as the ToString function/method public string ToString() They return a string containing values in the name and temp attributes. Note that the ToString() member function/method does not generate any output to the console. Task 4-2- Compulsory. Grading scale: F-C Write a function/method for linear search in a field (array, vector, list) cities with city objects. int lensesok(field cities, int n, int searchtemp) The function/method should search a field cities for a city with a specific temperature (temp) and return the index for that city. If no city with that temperature is found -1 is returned. The variable n is the number of elements in the array.```
FFaraj12/25/2022
FFaraj12/25/2022
It says create a function where it search for every city's temperature if it doesn't exist return -1
FFaraj12/25/2022
My question is how do I add/ assign temp values for those cities in the array so I can loop through them?
FFaraj12/25/2022
as far as I know usually when using array we define two separated arrays that contains different elements and then we get the index of each list of array.
FFaraj12/25/2022
Hello there
GGEmanuel12/25/2022
Hello
GGEmanuel12/25/2022
I'm not a pro at programming, but i could give you some directions.
FFaraj12/25/2022
Ok! That would be great. None of us are perfect, you're not alone.
GGEmanuel12/25/2022
I assume you want to return the city in this method if it matches `valueToFind`. However in this ss, you are returning the index, not the actual value. So instead of `return i;` you need to have `return array[i];` since `array[i];` represents the city returned which passes your `array[i] == valueToFind` condition.
GGEmanuel12/25/2022
how do I add/ assign temp values for those
If im assuming correctly, you want to add something to the array. You cannot add an item/city, to an array once declared since arrays have fixed length. Thats why you always, initialize an array either with a size, or initialize it directly with values. If you want to add items, you need to use `List<T>`. You can google C# lists, and you can find all the info that you need there. Tons of good sources to learn from.
FFaraj12/25/2022
Wow, it makes very sense bro 😃 I haven't notice that.
GGEmanuel12/25/2022
Indeed. It's easy for small details like this to slip your grasp, especially when you're new to programming. I too went through this so i can relate 🙂
GGEmanuel12/25/2022
``````        public override string ToString()
{
return \$"Name {Name} - Temp {Temp}";
}``````

A more cleaner way of implement `ToString()`
If you're not familiar with this technique then you should definitely check this link https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
It's called string interpolation. It's a better way concatenating strings and variables into sentences. So to speak.
FFaraj12/25/2022
Ok! Great. You're right. I have watched this tutorial and they do exactly as u described.
GGEmanuel12/25/2022
I mean, you could do it the default way until later when stuff like this becomes more relevant, but if you can learn to do it right from the start then why not? 🙂 It's not complicated and people will notice this which will certain highlight your effort to learn and write quality code which won't go unappreciated!
FFaraj12/25/2022
Ok! You're right. We can't use a two separated list of array and try to emerge them altogether.
GGEmanuel12/25/2022
Im not sure that's possible but i think it may be. I never done anything remotely related, but even if it is, it would overkill for the purpose of this exercise.
FFaraj12/25/2022
I'm just trying to review and understand all the content that u mentioned, that's why I might seem a bit slow 🤗
GGEmanuel12/25/2022
Don't worry, no one is rushing you 🙂 Take your time, study them as much as you need to. It's not a race! In the end, it doesn't necessarily matter how much we know, but rather, how much we understand from everything that we know.
FFaraj12/25/2022
Ok! Let's follow the pattern that u mentioned, which's creating a list under the cities array. Well! Here is the question remains! How do I go loop through both the array and the list in order to search using the linear search concept?
FFaraj12/25/2022
That's very wise thinking and I totally agree.
FFaraj12/25/2022
You're right. I have implemented this pattern of overriding in other projects and just forgot it, Thanks a lot 🙏
GGEmanuel12/25/2022
You have an array of cities as of now. And you are only returning the city that matches the value in `valueToFind` variable. So you are returning only one value. Every single time.
GGEmanuel12/25/2022
You loop through the entire array of city and if you find the city that matches the city you're searching for in the array then you return the city found at that index.
GGEmanuel12/25/2022
I made a mistake here. I noticed that in fact your method returns and `int` which is the index of the city that matched the condition. So yes, you should `return i;`. You could also return the city itself like i mentioned initially, but that may be outside the scope for this exercise.
GGEmanuel12/25/2022
I meant that you could use a list if you prefer here instead of an array. It wouldn't make much difference. The thing with lists is that they are often used in conjuction with the `foreach` loop. In truth it's always more preferable to use lists instead of arrays. But that depends on the context. You could have a list and use `for` loop on it. The idea is that lists almost always go with `foreach` as long as you dont need the index of the item. But in your case, you need to return the index of the found city (city that matched your condition) so both array and list can work here just fine. The main difference between arrays and lists is that lists are dynamic in size. Meaning they can adapt to new lengths if needed. While an array is size bound. Meaning that an array has a fixed size that cannot be changed. Internally a list is based on the array. If you happen to add more items than the size you've declared for a list then the list will automatically expand itself to adjust to fit in the newly added elements.

TLDR: You can use both a list or an array with `for` loop. Only use `for` if you need the index of an item. If you don't then always prefer lists with the `foreach` loop.
FFaraj12/25/2022
Ok! What a great knowledge you're distinguished with bro. Your tips are very rewarding and are even more explanatory than the docs.
FFaraj12/25/2022
It's mentioned in the assignment that I have a multiple options to use either (array, vector, list) although I've been using lists for all the projects I built, but not the arrays and I would prefer practice using them, because I might need use them for career purposes in the future.
GGEmanuel12/25/2022
Write a function/method for linear search in a field (array, vector, list) cities with city objects.
Im assuming this means that instead of `valueToFind` you need to pass in a list of values to find. If they are found. Then you return their index. In truth, you won't be returning a single index, but a list of indexes. Since you would be returning more than one index.
FFaraj12/25/2022
The cities of array is not the problem, but what confuses me most is the values or the temperatures that needs to be assigned to the cities. If you review the assignment requirements, you'll notice a bit confusion when it comes to temperatures 🤔
GGEmanuel12/25/2022
Maybe ask for some clarifications from whomever gave you the task? Or is it something you found and decided to practice on?
FFaraj12/25/2022
Oh! Really is that how it goes!
GGEmanuel12/25/2022
Write a function/method for linear search in a field (array, vector, list) cities with city objects
Also, im not sure i understand anymore. So write a method that can take either an array, vector or list? Or to contain all 3? I cant exactly offer much help if im not sure what's being asked.