Qbasicnews.com

Full Version: help understanding some things. ..
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i need help to understand several things w/in the code we get from our teacher. . .for example:

CLS
DIM char(127)
PRINT "Enter the string and press the ENTER key"
LINE INPUT text$
FOR count=1 to LEN(text$)
char(ASC(MID$(text$,count,1)))=char(ASC(MID$(text$,count,1))) +1
NEXT count
PRINT
PRINT
PRINT "There are "; LEN(text$); " characters in the string"
PRINT
PRINT "The breakdown is. . ."
FOR count=32 to 127
IF char(count) > 0 then
IF count=32 then
PRINT "Sp"; " - ";
ELSE
PRINT CHR$(count); " - ";
END IF
PRINT char(count); " - ";
PRINT USING "###.#"; (char(count)/LEN(text$))*100
END IF
NEXT count

my question is all the bold parts. i dont understand what the first bold is doing exactly. he told us but i forgot. i dont get char(count), how/why is count w/in parenthesis along side the char variable. it might be a stupid question but i dont see it. . .as you can see i am having trouble w/arrays.
Well, for example: The capital letter A is numbered as 65. So if you enter "AABCDA", char(65) will be 3 because there are 3 As.

Here's some psuedo-code for the loop the first bold part is in:
1. take a letter from what the user typed in (say A, for example)
2. get the letter's number, AKA ascii code. (65 for A)
3. add one to that element number in the array (add 1 to char(65))
4. back to step 1 for the next letter, until all the letters have been done

MID$(...) does step 1, ASC(...) does step 2, and char(...) = char(...) + 1 does step 3

This does the same thing as the first bold line, and might be a little clearer:
Code:
asciiCode = ASC(MID$(text$, count, 1))
char(asciiCode) = char(asciiCode) + 1
Quote:i dont get char(count), how/why is count w/in parenthesis along side the char variable. it might be a stupid question but i dont see it.
the value inside the parenthesis tells qbasic which number of the array to access. e.g.

if you want to hold three numbers in variables you can do this.
char0 = 4
char1 = 56
char2 = 10

but if you use an array you can do this
char(0) = 4
char(1) = 56
char(2) = 10

the difference is that with arrays you can use other variables inside the parenthesis.

e.g. If you want to print out the values inside the char variable
** Without using arrays **
Code:
PRINT char0
PRINT char1
PRINT char2
** Using an array **
Code:
FOR i = 0 TO 2
   PRINT char(i)          'notice the variable i is used
NEXT i
Altho both ways use three lines of code... if you wanted 200 variables the first way would take 200 lines of code, the second way would still only take three.
If you wanted to reverse the order of printing (decending) the first way you would have to reverse the whole code (a big task if there are 200 lines) but with arrays you would only need to change the FOR statement.

Edit: N.B. that you cannot use a variable to access different non-array variables, so you couldn't use the code:
Code:
FOR i = 0 TO 2
   PRINT chari
NEXT i
as qbasic will assume that chari is a new variable.

HTH Smile

~Phydaux
ok fellas..i am getting the grasp of things, thanx. .. .
Also, I wanna let you know that what your stupid teacher did in the second line was bad and stupid.

This creates an array from 0 to 127 as a SINGLE PRECISION number:
Code:
DIM char(127)

This would work much better, because it creates an array with the only characters you can even type (you can't type any characters less than 32 from the keyboard!!!) and it makes the array a bunch of integers, which take up a LOT less memory than "Singles"
Code:
DIM char(32 to 127) as integer
i see that too because i didnt understand why he used 127 then used a for next loop for 32 to 127. in order for me to understand about single precision & anything else you might throw @ me, i'll have to read the book because he sure isnt teaching us out of the book. hence, you will get no arguments on how dumb he is. his teaching techniques suck. . .thats why i mostly hit up this site for the answers to my problems.
Quote:i see that too because i didnt understand why he used 127 then used a for next loop for 32 to 127. in order for me to understand about single precision & anything else you might throw @ me, i'll have to read the book because he sure isnt teaching us out of the book. hence, you will get no arguments on how dumb he is. his teaching techniques suck. . .thats why i mostly hit up this site for the answers to my problems.

Here is my advice: Astound him with your knowledge of Qbasic by telling him what I just told you. Do the crap projects he tells you to do. Get an EZA+. Come here to actually learn QBasic.
advice taken
Quote:.... i dont get char(count), how/why is count w/in parenthesis along side the char variable....
You seem to have a little trouble with the following:
char(count)

Well, char is an array variable, having been defined by the DIM statement at the top, and count is a variable containing a value from 32 to 127 as controlled by the FOR.

So, char(count) points (refers) to the "count" position of the "char" array. If count is a 33 then char(count) refers to the 33rd position of the char array. When you refer to a position or slot within an array, you use the parenthesis to specify which position of the array you are referring to. So, char(n) would be the nth position of the array char.

Think of an array as a bunch of mailboxes, each having its own unique number, like for an apartment building. In this particular case, we have 127 mailboxes, but we never use those numbers less than 32 (maybe these became a parking garage).

Do you understand in general what the program does? I ran it, and it does work. Here's an outline of what it does:

* You enter a string of characters.

* Since the characters that you are able to type on the keyboard fall into the ASCII range of 32 to 127, he sets up an array called char with 127 elements. What he's going to do is count the number of occurrences of each character entered, into the corresponding array position of that character. This count of occurrences is developed in the position of the char array that corresponds to the ASCII value of the character. If the text contains 3 E's, you will end up with a count of 3 in position 69 of the char array. 69 is the ASCII representation in decimal for the character "E".

* So, he gets each character of the input string, converts the character to ASCII with a ASC function, and adds one to the position in the char array which corresponds to the ASCII value of the character.

* Later he prints out the counts contained in the char array, printing the actual character (which he can derive from the ASCII value of the array position), the count for this character, and a percent of the total characters entered.

I hope this helps.
*****
Big Grin much much clearer. now i understand the coding w/in the FOR...NEXT loops. thanx man!!!