Alpha DevCon 2018
Results 1 to 2 of 2

Thread: Another nested loop puzzle

  1. #1
    Member
    Real Name
    Rob Polley
    Join Date
    Nov 2005
    Posts
    432

    Default Another nested loop puzzle

    Tom,

    These puzzles have been a great help to me in learning to program in Xbasic. I had not touched programming since my college FORTRAN class over 30 years ago.

    I was having trouble digesting nested loops, so I made up the simplest problem I could think of and more or less copied your example to solve it. I hope to contribute to your efforts by submitting it here. The problem is:

    For a list of names, write an xbasic program that counts the number of times the first letter of each name is used. Post the answer to the trace window. For a bonus, also have a message box pop up after each letter is counted with the number of times that letter has appeared. I'll submit a file of names and my code in separate attachments.

    Thank you!

    --Rob
    Attached Files Attached Files

  2. #2
    Member StephenP's Avatar
    Real Name
    Stephen Pilon
    Join Date
    Apr 2000
    Location
    Front Royal, Virginia
    Posts
    490

    Default Re: Another nested loop puzzle

    Rob,

    Here is my entry. Of course, I am assuming that every name starts with A thru Z. I'd have to adapt it for other circumstances.

    Code:
    'Date Created: 21-Dec-2012 12:37:04 PM
    'Last Updated: 21-Dec-2012 12:37:04 PM
    'Created By  : spilon
    'Updated By  : spilon
    dim name_list as c
    dim count_list as c
    dim letter_count as n
    
    name_list = "Jacqueline,Karen,Pat,Kerry,Ludivia,Martha,Doriana,Kimberly,AnnaMarie,Amanda,Caitlin,Gianna,Tara,Britt,Whitney,Kelly,Lisa,Gina,Laura,Debra,Robin,Dana,Valerie,Pamela,Steven,Thomas,Margaret,Denise,Ellen,Jack,Kimberly,Mary,Kathy,Nicole,East,Aziza,Patrick,Ryan,Caryl,Anne,Christina,Jeannette,Glenda,Mary,Laura,Marguerite,Bobble,Suzanne,Catherine,Jenifer,Anne,Cynthia,Rebecca,Heidi,Nancy,Jennifer,Ilya,Aurea,Leah,Denise,Mary Ellen,Amy ,Samantha,Laura,Stephanie,Janet,Claudia,Anne,Marta,Katherine,Martha,Susanne,Robert,Cathy,Clare,Robert,Bunny,Jennifer,Rebecca,Lauren,Danielle,Meghann,Samuel,Lauren,Laura,Julia,Christina,Mary Ann,Sylvia,Katherine,Antonia,Kathryn,Michaela,Jennifer,Michelle,Carolyn,Stacey,Joan,Tara,Kristina,Gaston,Kristine,Anne,Michelle,Kimberly,Nelen"
    name_list = comma_to_crlf(upper(name_list))
    
    count_list = *for_each(foo,left(foo,1),name_list)
    
    for i = 65 to 90
    	letter_count = word_occurs(count_list,chr(i),crlf())
    	trace.writeln(chr(i) +" : " + letter_count)
    	ui_msg_box("Letter " + chr(i),"Count of letter " + chr(i) + ": " + letter_count)
    next i
    I just put the names in a variable, but getting them from a table would be no biggie. I would use something like this in place of the two lines that start with "name_list =":

    Code:
    name_list = sql_records_get("::name::user_db","select upper(left(first_name,1)) from dbo.users","","")
    -- Stephen
    Stephen Pilon
    Associate Librarian
    Christendom College

Similar Threads

  1. Programming Puzzle 2 - Another Simple Loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 16
    Last Post: 12-20-2014, 10:38 AM
  2. Programming Puzzle 23 - For / Next Loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 9
    Last Post: 07-18-2012, 06:54 PM
  3. Programming Puzzle 7 - Yet another nested loop!
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 4
    Last Post: 08-08-2011, 05:41 PM
  4. Programming Puzzle 6 - Another nested loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 6
    Last Post: 08-08-2011, 04:30 PM
  5. Programming Puzzle 5 - Nested loops
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 10
    Last Post: 08-08-2011, 02:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •