Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Solution Review: Array of Squares

Sumit Rawal answered on May 23, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • Solution Review 1: Square Numbers and Return Their Sum
  • Solution Review: Array of Integers
  • Challenge: Array of Squares

  • Solution Review: Array of Squares

    0

    Task

    In this challenge, you were provided an array of numbers which needed to be squared.

    Solution

    Let’s go over the solution step-by-step.

    The first thing you had to figure out is that this problem requires a for expression.

    Next, you had to figure out the number of iterations required for the program to return the correct output. As each element of the array needed to be squared, the number of times the for expression should run is equivalent to the length of the array.

    for (i <- 0 until testArray.length)

    To create our range for the iterator i we use the Range keyword until. This is because the last index of an array is one less than its length as indexes start from 0 and length starts from 1. As we already know from a previous lesson, until excludes the upper limit.

    Finally, the last step required you to assign the square of the value at an index to the same index.

    testArray(i) = testArray(i)*testArray(i)

    You can find the complete solution below:

    You were required to write the code from line 3 to line 5. 

    Popularity 1/10 Helpfulness 1/10 Language python
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on May 23 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.