Palindromes

Announce and discuss the progress of your various programming-related projects...programs, games, websites, tutorials, libraries...anything!

Moderators: Pete, Mods

Post Reply
User avatar
Raspberrypicker
Veteran
Posts: 55
Joined: Thu Aug 02, 2007 4:54 pm
Location: Florida

Palindromes

Post by Raspberrypicker » Sun Sep 09, 2007 4:50 pm

Hey, you know what I find really annoying? When people use GOTO and GOSUB. I always try to stray from this, because of the speghetti code. But what's really annoying is when a beginner turtorial uses GOTO. If the turtorial uses it, then the person learning it will use it too.

So I was bored, tired, and needed an easy project to do. So I "fixed" a palindrome program. You can consider this my first contribution to the qb community. hee-hee. :lol:

Code: Select all

CLS
PRINT "@@@@@@@@@@@@@@@@@@"
PRINT " @@@@@@@@@@@@@@@@"
PRINT "  Palindromotron"
PRINT " @@@@@@@@@@@@@@@@"
PRINT "@@@@@@@@@@@@@@@@@@"

DO
DO
 PRINT CHR$(13); "Enter a word below to see if it is"
 INPUT "a palindrome: ", Original$
 Original$ = UCASE$(LTRIM$(RTRIM$(Original$)))
LOOP UNTIL LEN(Original) <> 0

Reversed$ = ""
CurrentPosition% = LEN(Original$)
DO
 Reversed$ = Reversed$ + MID$(Original$, CurrentPosition%, 1)
 CurrentPosition% = CurrentPosition% - 1
LOOP UNTIL CurrentPosition% <= 0

PRINT "The word "; Original$;
IF Original$ = Reversed$ THEN
	PRINT " is a palindrome"
ELSE
	PRINT " is not a palindrome"
END IF

DO
 PRINT CHR$(13); "Do you want to enter another word?"
 INPUT "Y/N: ", YESNO$
 YESNO$ = UCASE$(LTRIM$(RTRIM$(YESNO$)))
 SELECT CASE YESNO$
	 CASE "Y"
		repeat = 1
	 CASE "N"
		END
	 CASE ELSE
		repeat = 0
 END SELECT
LOOP UNTIL repeat = 1
CLS
LOOP
Fruit Pickin'

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Sun Sep 09, 2007 6:16 pm

i've never understood the animosity towards GOTO and GOSUB they
work fine and the code looks fine if you document it well.

i of course am only looking for the text/data programs end i have no
idea how bad it would be for a graphic program.


maybe you can explain the evils in detail of GOTO/GOSUB?

User avatar
Raspberrypicker
Veteran
Posts: 55
Joined: Thu Aug 02, 2007 4:54 pm
Location: Florida

Post by Raspberrypicker » Sun Sep 09, 2007 7:06 pm

Well if you can do an excellent job of documenting the thing, then maybe GOTO and GOSUB would be ok. But in general it just makes it really hard to read or debug since you have to jump all over the place.
Bigger program=Harder to read/debug

Plus I heard that over using the command will result in wacky bugs which make it near impossible to fix...or something like that. This is what I've heard, but I don't really know about it because I never use GOTO or GOSUB.
Fruit Pickin'

User avatar
k7
Coder
Posts: 41
Joined: Wed Aug 01, 2007 7:38 am
Location: Tasmania, Australia
Contact:

Post by k7 » Sun Sep 09, 2007 7:21 pm

GOTO is fine with me. Currently I'm using JustBASIC and [labels] and GOTO are always used by myself and other JB'ers. Honestly I don't see what the fuss is about, I use it where it's most convenient, but I also use SUB's and FUNCTION's as well.

User avatar
k7
Coder
Posts: 41
Joined: Wed Aug 01, 2007 7:38 am
Location: Tasmania, Australia
Contact:

Post by k7 » Sun Sep 09, 2007 7:30 pm

Code: Select all

[initialize]
   usrInput$ = ""

[main]
   cls

   print action$
   
   input usrInput$
   if lower$(usrInput$) = "restart" then goto [initialize]
   if lower$(usrInput$) = "quit" then end
   
   action$ = usrInput$
   goto [main]
Code like that is common practice in JB.

User avatar
Raspberrypicker
Veteran
Posts: 55
Joined: Thu Aug 02, 2007 4:54 pm
Location: Florida

Post by Raspberrypicker » Mon Sep 10, 2007 4:17 pm

Ok well this is what the original program was. I did not write this, I only made the version without the GOTOs. But the original programmer does admit to GOTO being a bad way to write algorithms. You can decide which is easier to read. Personally, I like loops better...it's a bit more thinking involved for the programmer but a whole bunch easier to read.

Code: Select all

DIM Original AS STRING, Reversed AS STRING

Again:
CLS
PRINT "@@@@@@@@@@@@@@@@@@"
PRINT " @@@@@@@@@@@@@@@@"
PRINT "  Palindromotron"
PRINT " @@@@@@@@@@@@@@@@"
PRINT "@@@@@@@@@@@@@@@@@@"

AgainTypeWord:
PRINT CHR$(13); "Enter a word below to see if it is"
INPUT "a palindrome: ", Original
IF LEN(Original) = 0 THEN GOTO AgainTypeWord
Original = UCASE$(LTRIM$(RTRIM$(Original)))

GOSUB ReverseWord

PRINT "The word "; Original;
IF Original = Reversed THEN
	PRINT " is a palindrome"
ELSE
	PRINT " is not a palindrome"
END IF

AgainTypeYesOrNo:
PRINT CHR$(13); "Do you want to enter another word?"
INPUT "Y/N: ", YESNO$
YESNO$ = UCASE$(LTRIM$(RTRIM$(YESNO$)))
SELECT CASE YESNO$
	CASE "Y"
		GOTO Again
	CASE "N"
		CLS
	CASE ELSE
		GOTO AgainTypeYesOrNo
END SELECT

END


ReverseWord:
?this is a crappy inefficient algorithm, but since I have
?to use goto.. lol ;)
Reversed = ""
CurrentPosition% = LEN(Original)
NextPosition:
Reversed = Reversed + MID$(Original, CurrentPosition%, 1)
CurrentPosition% = CurrentPosition% - 1
IF CurrentPosition% > 0 THEN GOTO NextPosition
RETURN
Fruit Pickin'

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Mon Sep 10, 2007 4:28 pm

The only goto I use is Main, Start, and Finish. But Subroutines are good, as long as they are explained. I don't like reading a bunch of goto lines especially if they're GOTO 4. But TI - BASIC doesn't differentiate between programs and subs, as opposed to just gotoing.
For any grievances posted above, I blame whoever is in charge . . .

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Tue Sep 11, 2007 6:37 pm

ABOUT THE GOTO.
Computer programming experts have spent a tremendous amount of time in the last 35 to 40 years on the subject of using/not using GOTO.

Structured programming and top-down programming methodologies were heavily focused on eliminating the use ot the GOTO. The original PASCAL language of circa 1970 did not contain a GOTO instruction, although I heard that later versions did have it.

Even experts like the renowned Donald E. Knuth state that you still need to use GOTO for certain situations.

All modern programming languages, except Java, still have the equivalent of a GOTO instruction.

I must admit that the techniques of top-down or structured programming enable you to make a better, understandable program. However, there are many instances when a properly inserted GOTO will simplify the layers of structured programming code.

Programmers will ALWAYS criticize another programmer's code. Why did he use so many GOTOS, or why didn't he just insert a GOTO here?

The biggest argument against using GOTO is that it tends to produce spaghetti code. This is true especially for an unexperienced programmer who has yet to master the statements for structured programming availailable in the language that he's using. Or perhaps a programmer who is translating assembly language code to a higher level language, and translates all the branch or jump instructions into GOTOS.

So, be kind to the above novice programmers, and also take a second look at the code of an experienced programmer who used an ocassional GOTO --- you might learn something.

Regards..... Moneo
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.

User avatar
Raspberrypicker
Veteran
Posts: 55
Joined: Thu Aug 02, 2007 4:54 pm
Location: Florida

Post by Raspberrypicker » Tue Sep 11, 2007 6:51 pm

ah ic.

thanx for the insight :idea:
Fruit Pickin'

Post Reply