logotransparent.png

Legion of Learners

www.lol-101.com

  • Home

  • Classrooms

  • Courses

  • About Us

    • Executive Team
    • Board Members
  • Resources

  • More

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    sfolax6776
    Jun 30, 2018
      ·  Edited: Dec 23, 2020

    1.7 Workshop: what is your algorithm?

    in Introduction to Java

    A. Introduction

    We said in 1.5 that when writing a program, you start from an algorithm, which is the series of steps to solve your problem, and can be written in pseudo code. For instance, to solve the following problem:

     You put $10,000 into a bank account that earns 5 percent interest per year. How many years does it take for the account balance to be double the original? 

    you can use this algorithm (written in pseudo code, of course there are other ways):

     Start: year = 0, rate=0.05, balance = 10,000 
     Repeat steps below until balance reaches 20,000
       Add 1 to the year value;
       Compute the interest as balance x 0.05 (i.e., 5 percent interest);
       Add the interest to the balance 
     Print the final value of year

    B. Try:

    1. Suppose your cell phone carrier charges you $29.95 for up to 300 minutes of calls, and $0.45 for each additional minute, plus 12.5 percent taxes and fees. Give an algorithm to compute the monthly charge from a given number of minutes. What should replace "?" in the following pseudo code?

    Start: set number of minutes to x; 
        If x ? 300 then charge = 29.95
        else charge = 29.95+(x-300)*0.45
    set tax = ? * charge (i.e., 12.5% )
    set total charge =  ?
    Print out total charge

    2. Consider the following pseudo code for finding the most attractive photo.

     Line up all photos in a row;
     Pick the first photo and call it "the best so far";
     For each photo in the row, repeat the following till reach the end of row:
       If it is more attractive than the "best so far":
          discard "the best so far"
          call this photo "the best so far"
       else do nothing (well, it's okay to omit this line)
     Move to the next photo
     The photo called "the best so far" is the most attractive photo

    Is this an algorithm that will find the most attractive photo? Can you modify this algorithm to find the most ugly photo? Donald Trump's photo? the biggest number? the smallest number?

    When will the center have 2 tiles? when will it have 3? Which row is at center?

    3. Write an algorithm to create a tile pattern composed of black and white tiles, with a fringe of black tiles all around and two or three black tiles in the center, equally spaced from the boundary.


    The inputs to your algorithm are the total number of rows x and columns y. (x=7, y=9 is shown here. You can try x=6, y=8 or x=4, y=5, see how the pattern will change)





    4. Given an integer, print out each digit of that integer.

    5. Given a 16-digit credit card number, print out the sum of all digits.

    12 comments
    0
    mr.ricklu
    Jun 30, 2018

    Number of rows = #rows

    Number of Columns = #clms

    Place #clms black tiles

    Go down 1 line

    Place 1 black tile

    Place #clms - 2 white tiles

    Place 1 black tile

    Go down 1 line

    Repeat the previous 4 steps x - 1 times

    If #rows is even:

    x = (#rows / 2) - 2

    Else:

    x = (#rows / 2) - 1.5

    Place 1 black tile

    Place x white tiles

    Place #clms - (2 + 2x) black tiles

    Place x white tiles

    Place 1 black tile

    Go down 1 line

    If #rows is even:

    repeat the previous 6 steps

    Place 1 black tile

    Place #clms - 2 white tiles

    Place 1 black tile

    Go down 1 line

    Repeat the previous 4 steps x - 1 times

    Place #clms black tiles

    0
    tian
    Jul 01, 2018


    0
    wangernestrdragon
    Jul 01, 2018

    If first row or last row, fill all spaces with color

    Fill in the first and last columns with color

    Take the total number of rows and subtract 2 and divide that number by 2, if that number is a fraction round down.

    Add that number to the 1st row and subtract that number form the last row. The remaining range will be the middle repeat this process for the columns and fill in the middle according to the ranges.

    0
    leedylan2016
    Dec 15, 2019

    1st Algorithm

    Number of rows = x is 7

    Number of columns = y is 9

    1. Place y black tiles rightward on the first row;

    2. Place (x - 1) black tiles going down;

    3. At the last tile drawn, place (y - 1) black tiles to the left;

    4. At the last tile drawn, place (y - 2) black tiles upwards then go one black tile up;

    5. Go half of (x-1) down and (y-2-3)/2 right

    6. Then draw 3 black tiles to the right;

    0
    sfolax6776
    Dec 15, 2019

    You definitely know how to write an algorithm. But we have a small problem: a computer can only go from left to right and top to bottom. It can not go upwards. So once it reaches the bottom of the rectangle, it won't be able to go back to draw the right boarder or the center. Any other way?

    0
    leedylan2016
    Dec 15, 2019

    2nd algorithm

    Number of rows = x is 6

    Number of columns = y is 8

    1. Place y black tiles rightward on the first row;

    2. Place (x - 1) black tiles going down;

    3. At the last tile drawn, place (y - 1) black tiles to the left;

    4. At the last tile drawn, place (y - 2) black tiles upwards then go one black tile up;

    5. Go (x - 4) down then (y - 3) to the right;

    6. Then draw 2 black tiles to the right;

    7. Go to the left 4 black tiles;

    8. Then go down 1 tile down;

    9. Then go (y - 3) to the right;

    10. Then draw 2 black tiles to the right;

    0
    dylanchauhan2007
    Dec 17, 2019

    Rows: x is 6

    columns: y is 8

    First, make a row that has y black tiles at the top. Once you reach the end of the row, place (x-1) black tiles as you go down. Then, put down (y-1) black tiles as you go to the left. Next, place (x-2) black tiles as you go up. For the last few step, start by going (y-4) to the right and (x-2) down. Place a black tile on your current location and the tile to the right.


    0
    arnav.gpt1234
    Dec 19, 2019

    columns: x=7

    row's: y=9

    Make a row that has 9 black squares all next to each other

    then on the 9th black square make a column with 6 more black squares next to each other

    then on the last square you made on your column make 8 more black squares next to each other in a row

    then on the last square you made on your second row make a column with 5 more black squares next to each other

    then on the point x=4 and y=4 make a black square

    then make two more black squares to the right of that


    0
    hastin.chen
    Jan 02, 2020
    <