GithubHelp home page GithubHelp logo

programming-univbasics-4-finding-the-maximum-value-in-an-array-lab-atl01-seng-ft-051120's Introduction

Using Loops and Arrays to Find Data

Learning Goals

  • Work with arrays and loops to extract specific values
  • Use loops to compare values in an array

Introduction

In the previous lab, we looped through arrays to find elements. In discussing how to solve the lab, multiple solutions were discussed - one solution looped through the entire array and the other stopped as soon as a matching element was found.

In this lab, we are going to look at a slightly more complex task. We still want to find a specific array element, but this time, we won't have a provided value to compare with. Instead, we will need to find an element based on its value relative to the other elements in the array.

Your task is to write a method that finds the element in an array with the highest value. This is often referred to as finding an array's maximum value.

Because you need to find an array element relative to other elements, we must loop over every element of the array. We can' be certain that one element is the maximum if we haven't checked every other element!

Instructions

For this lab's tests, assume all arrays are sets of positive integers. The find_max_value method and parameter name are provided for you in lib/finding_the_maximum_value.rb.

The find_max_value takes in an array of integers and should return whichever integer in the array has the highest value. So, for instance, if we ran the following:

2.6.1 :005 > find_max_value([1,2,1,3,4,3,5,4,3,2,1])
 => 5

We should expect to receive 5 in return. Remember when writing your implementation, you will need to check every number in a given array.

Remember again to check every element in the provided array! Try your best to solve this lab. A walkthrough is provided below to verify your understanding and help you if you get stuck.

...

...

...

Solving this Lab

Since we're comparing array elements, to start, we know we will need a loop to access every element.

def find_max_value(array)
  count = 0
  while count < array.length do

    count += 1
  end
end

The instructions state that we will always be working with arrays of positive integers, but how can we compare them to one another?

One way is to use a variable (let's call it max_value) - assign a variable a value that is lower than any possible value in the array. When looping over array elements, if an element's value is greater than the max_value variable, max_value gets reassigned to equal the element's value. max_value is then returned at the end:

def find_max_value(array)
  count = 0
  max_value = -1
  while count < array.length do
    if max_value < array[count]
      max_value = array[count]
    end
    count += 1
  end
  max_value
end

Let's think about how this works. Consider the following array:

[2, 3, 3, 4, 3, 6, 1, 3]

If this array was passed into find_max_value, the following steps would occur:

  • count gets assigned to 0
  • max_value gets assigned to -1
  • In the while loop initiates and count < array.length evaluates to true (0is less than 8), so the implementation inside the loop executes
    • max_value is less than array[count] (-1 is less than 2).
    • max_value is reassigned to 2.
    • count is assigned to 1
  • count < array.length again evaluates to true (1 is less than 8)
    • max_value is less than array[count] (2 is less than 3).
    • max_value is assigned to 3.
    • count is assigned to 2
  • count < array.length again evaluates to true (2 is less than 8)
    • max_value is NOT less than array[count] (3 is equal 3).
    • max_value is not reassigned, staying at 3
    • count is assigned to 3
  • count < array.length again evaluates to true (3 is less than 8)
    • max_value is less than array[count] (3 is less than 4).
    • max_value is reassigned to 4
    • count is assigned to 4

Eventually, max_value gets reassigned to 6, where it will stay until the loops finish. This is the maximum value in the array.

Conclusion

Finding the maximum value in a collection is extremely common in all sorts of applications.

  • Given an array of hourly temperatures, what is today's high?
  • Who is the tallest person in a line?
  • What was the high point of the stock market this week?

Even if we don't have a specific value we're looking for, we now have a way to work through a collection of data and pull out useful values.

Resources

programming-univbasics-4-finding-the-maximum-value-in-an-array-lab-atl01-seng-ft-051120's People

Contributors

maxwellbenton avatar sgharms avatar donnadieu avatar drakeltheryuujin avatar

Watchers

 avatar  avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar  avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.