CheckEmoji Community · the emoji forum
🏠 Home 🆕 What's new ❓ Unanswered 🔥 Popular 📡 RSS Members 👥 0 online log in · register
Home › IT › IT Support › Algorithm help needed

Algorithm help needed

Started by Eric Carter · · 👁 4 views · 11 replies

📡 Subscribe to replies

Participants Eric CarterMichael Jackson10William Richardson12darkowl72ironcyclist58Jamie Green5
Eric Carter Eric Carter NewcomerOP
5 messages
joined Jun 2015
#1 ·
Hey everyone, I’ve got an algorithms exam coming up at school soon, and we were handed this specific assignment for homework: "Print n natural numbers and determine which one is the largest and which one is the smallest." My professor made some comment about starting off by making an assumption regarding which number is the largest... I’m hoping someone in this community has some actual experience with programming and algorithms who could walk me through how to tackle this problem.
Michael Jackson10 Michael Jackson10 Active Member
140 messages
joined Feb 2023
#2 ·
So, who actually gets the credit and the degree at the end of this whole saga—you or the guy who actually fixes it?

. generate unnamed
- use them to build an array of n elements
- (print the array)?
- set the first element as both the max and the min
- loop through the rest of the array
if the next number is bigger than the current max, update the max
(else) if it's smaller than the current min, update the min
- print out the final max and min
Eric Carter Eric Carter NewcomerOP
5 messages
joined Jun 2015
#3 ·
Michael Jackson10 said:At the end of the day, who actually gets the grade and the degree—you, or the guy who actually figures out the solution?

. generate unnamed
- from them, construct an array of n elements
- (print the array)?
- declare the first element of the array as both the maximum and the minimum
- loop through the subsequent elements of the array
if the next element is greater than the current max, then that element becomes the new max
(else) if that element is smaller than the current min, it becomes the new min
- print the max and min

I honestly wish I didn't have to come looking for help on a forum like this, but every single department where this new junior professor is teaching—mine included—has reached the same conclusion: they don't understand a lick of what he's saying. It's pretty obvious his explanations just aren't clicking.

Thanks for this, man. Any chance you could lay this out for me with a diagram (start $\rightarrow$ input...)? I'm still struggling to wrap my head around how to actually turn this logic into something functional.
William Richardson12 William Richardson12 Regular
301 messages
joined Aug 2014
#4 ·
Just grab that textbook or manual and start reading. There’s probably some code snippets in there from the lab exercises and stuff. Go through the materials... even if it takes you a couple of days.

This assignment is basic stuff. Anyone who's finished an intro to programming course should have this down... it's like learning addition in grade school.
Eric Carter Eric Carter NewcomerOP
5 messages
joined Jun 2015
#5 ·
William Richardson12 said:Just grab that textbook or script and start reading; there’s definitely something useful in there, probably some code snippets from those audit exercises or lab sessions. Go through the materials, even if it takes you a solid two days to digest it all.

Look, this assignment is basic stuff—something anyone who has ever touched the fundamentals of programming should be able to handle effortlessly, much like how a kid learns to add numbers up to ten in elementary school.

For context, I'm just a high school student taking an intro computer science class...
darkowl72 darkowl72 Regular
311 messages
joined Oct 2015
#6 ·
Look, you’ve got two basic ways to handle this without any fancy optimizations—just the rawest versions possible. Either you work with a single one-dimensional array (basically just a list of numbers) or you use two arrays: one filled with your data and a second empty one where you move everything.

Using two arrays is definitely easier. Let's say you have an unnamed(n) array and a Sorted(n) array:
First, you just assume the first number in the array is the minimum (Min).
Then, you run a loop through that unnamed(n) array $n$ times. You look for the smallest value (or one equal to your current Min) and shove it into the Sorted array at the current loop index.
Once that loop finishes, your list is sorted in ascending order, from smallest to largest.

If you need to sort in descending order instead, just assume the first number is the maximum and hunt for values that are smaller or equal.

The second method uses just one array, but you'll need nested loops.

The first loop runs from $i = 1$ to $n$.
The second loop runs from $j = i$ to $n$.
Again, you start by assuming the first value is the minimum (or maximum). Every time you find a smaller (or larger) value, you swap them. This way, the sorted numbers always end up at the "top" of the array, and in the next iteration, you're only checking the remaining unsorted part.
ironcyclist58 ironcyclist58 Regular
863 messages
joined Feb 2020
#7 ·
Look, based on how the prompt is worded, your solution is way too over-engineered. He doesn't even need an array—it doesn't say he needs to store all those inputs. All he really needs are two variables to keep track of the current max and min values.
William Richardson12 William Richardson12 Regular
301 messages
joined Aug 2014
#8 ·
ironcyclist58, what we actually need here is for the author to show us their own attempt at a fix. No point in us jumping in to solve it if they haven't even tried...

We're here to explain things, not do the work for them 😬
darkowl72 darkowl72 Regular
311 messages
joined Oct 2015
#9 ·
Yeah, I messed up... it only asks for the min and max, but I’d honestly just sort the whole thing right away. My bad. 😁

Just run one loop from 1 to n and use two variables to track the largest and smallest numbers.

Assume the first number in the array is both the max and the min, then just swap them out as you go...
Michael Jackson10 Michael Jackson10 Active Member
140 messages
joined Feb 2023
#10 ·
ironcyclist58 said:Look, based on how the prompt is phrased, your solution is way over-engineered. He doesn't need an entire array (it doesn't say we need to store all the input numbers), just two variables to track the current max and min.

The assignment starts with "unnamed...", so I figured at least one array was involved.
But wait, what kind of n numbers are we talking about here? Given ones? User input?
Since there's zero clarification, I just assumed they should be generated and filled into that array before testing...

Honestly, I'm not even sure if the original post actually contains the literal wording of the task (though maybe it does, given how our American school system works), or if this is just a "loose interpretation" of the thread starter.
Jamie Green5 Jamie Green5 Member
17 messages
joined Sep 2017
#11 ·
Eric Carter said:I'm currently in high school, and this is actually from my computer science class..

That’s precisely the point. This entire curriculum is a complete waste of time for high schoolers. If we're being honest, I'd argue they shouldn't even be forced through calculus or physics, and let's face it, Latin has been a dead language for over a millennium now. Not to mention that art and music should probably just be handled by specialized vocational schools instead.

I truly can't fathom why they continue to burden young minds with such trivialities.
Eric Carter Eric Carter NewcomerOP
5 messages
joined Jun 2015
#12 ·
The goal here is to map out a flowchart for this specific algorithm, starting from the initial trigger and moving through the input phase...

You must log in or register to reply here.

Log in Register

🔗 Similar threads