Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

Simple BASIC program to re-arrange the words in ascending order.

 
webmaster forum
Activity
0%
 
New Poster
Posts: 1
Topics: 1
February 25, 2011, 10:22:30 AM

Hi! My first post here at techtalk (finally!).  Actually I have this program for my exam syllabus and I've got the program here but it's not functioning as I want it to.  

What the program does is calculate the no of words  in a sentence and display that and then it re-arranges those words in alphabetical order.  

I got the code from my teacher (during notes), but it was as usual riddled with mistakes.  I was able to rectify it quite a bit and now am able to make it calculate the number of words correctly; however, the problem crops up while re-arranging words alphabetically.  

Please note that the logic used from 120 has just been copied by me from the notes. . .  and that is where the problem lies.  So, could you please go through the code and point out where I'm going wrong here?

10 READ A$
20 PRINT A$
25 DATA "every wednesday we have computer practicals. "
30 L=LEN(A$)
40 B$=" ":C=1: REM I've defined an empty string as B$.
50 FOR I=1 TO L
60 W$=MID$(A$,I,1)
70 IF W$<>" " THEN B$=B$+W$:GOTO 100: REM What this line does is group characters together to form words.  So, when a blank space is detected, it skips the THEN part.
80 C=C+1: REM Simultaneously calculating number of words
90 X$(C)=B$:B$" ": REM Re-initializing B$ to blank string to find another word
100 NEXT I
110 PRINT "Number of words in the sentence are: ",C
120 FOR I=1 TO C-1
130 FOR J=1 TO C
140 IF X$(I)<=X$(J) THEN 180
150 T$=X$(I)
160 X$(I)=X$(J)
170 X$(J)=T$:REM 150 to 170 are for creation of a temp string to switch strings in case there are bigger or smaller.
180 NEXT J
190 NEXT I
200 FOR I=1 TO C+2
210 PRINT X$(I);" ";
220 NEXT I
230 END

When I run it I get the o/p:

computer every have we wednesday

As you can see, it skipped displaying the last word 'practicals'.  I tried re-arranging the words in the program itself at line no 25 and every time it skipped arranging the last word. . .  What am I doing wrong here that the last word is not being taken into computation?

ThanQ very much for going through my problem!  :shiny
 
webmaster forum
Life Is Good!
Activity
0%
 
Professional Coder
Gender: Female
Posts: 242
Topics: 3
WWW
August 25, 2011, 09:08:43 AM

change line 50's I into 0 and not 1..

Affordable Custom Web Design Services
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page January 21, 2012, 11:26:54 PM