Hello,
Are there certain parts of the code that don't make sense or the basic flow of the program? The professor did a good job, but it does seem a bit tough for a beginning/first ever programming class, and he did use functions (I was wondering how one was supposed to do this without them, I used them in my hacked-together example). I read through the whole tutorial that I linked to in my last post and I found it extremely helpful, so if you can find time or even use it as a function look up tool since it does have example code. Subroutines simply do something, while Functions do something and return a value. DIM is Basic's way of declaring room in memory for a variable (DIM = Declare in Memory, if I remember correctly). So the statement "DIM A(10)" creates a spot in memory with 10 boxes to hold a value - be it an integer, string, etc. You reference those boxes by the variable name and the "box number", e.g. A(3), A(7), etc. You can use DIM for any variable you use in the program, but BASIC lets us be slackers and defines the variable for us the first time we try to use it and bases the type off the value you are trying to shove into it.
A flowsheet is just the steps one needs to do (in plain English) in order to solve the problem or do the task assigned. The flowsheet listed before in a post was a great example.
[/quote]
[quote name='banetistic' date='Sep 27 2005, 09:52 AM']Anyway, yesterday I had class in the comp lab where a lab teacher took over for my professor and helped us with the assignment. I'm glad he was there because he basically did the program with us, and explained every step as he went along. In the end I got the assignment done. And kind of understood how it was coded. I know the professor wanted the assignment to have certain things (like subroutines). Although I got the assignment done, I still haven't gotten the concept enough where I can do it alone (like when I take the final). And I still don't know how to do a flowchart.
Here's the program according to the teacher:
CLS
format$ = "\ \ ##,###.## ##,###.## ##.#% ##.#% ##.#%"
CALL header
totalPop = getTotalPop
CALL doStats(totalPop, format$)
DATA Under 20, 39168, 37202
DATA 20-64, 76761, 78291
DATA Over 64, 13881, 19980
END
[/quote]
Above is the "main" part of the program. This calls the all of the subroutines defined below and is basically a code version of your flowsheet that you created prior to starting the program. So this flowsheet looks like "1) Print heading. 2) Calculate total population. 3) Parse input. 4) Write output."
You can see the use of the DATA tag as a holder for information and is extracted with the READ command.
[quote name='banetistic' date='Sep 27 2005, 09:52 AM']SUB doStats(totalPop, format$)
CALL getData(totalPop, format$)
CALL getData(totalPop, format$)
CALL getData(totalPop, format$)
END SUB
SUB getData(totalPop, format$)
READ label$, males, females
perMales = males / (males + females) * 100
perFemales = females / (males + females) * 100
total = (males + females) / totalPop * 100
PRINT USING format$; label$; males; females; perMales; perFemales; total
END SUB
[/quote]
This is the meat of the program. It parses one DATA statment and outputs it to the screen in the format$ defined fashion. It is called three times by the doStats subroutine to parse the three data statements.
[quote name='banetistic' date='Sep 27 2005, 09:52 AM']SUB header
LET head1$ = "U.S. Population (in thousands)"
LET head2$ = "Age Group Males Females %Males %Females %Total"
PRINT TAB((LEN(head2$)/2) - (LEN(head1$)/2)); head1$
PRINT
PRINT head2$
PRINT
PRINT
END SUB
FUNCTION getTotalPop
totalPop = 0
READ label$, males, females
totalPop = totalPop + males + females
READ label$, males, females
totalPop = totalPop + males + females
READ label$, males, females
totalPop = totalPop + males + females
RESTORE
getTotalPop = totalPop
END FUNCTION
[/quote]
All this function does is go through the input using the READ command (they go through the DATA statements in order they are written in the program). It reads out the values (using label$ to get the first value as a string) and adds them together. It then returns the total number of people. You can see the use of the RESTORE function to set the program back to looking at the first DATA statement for future READ function calls.
[quote name='banetistic' date='Sep 27 2005, 09:52 AM']
[/quote]
Let us know if you have any other questions or specific questions dealing with this program. We are glad to help you understand the concepts.
Cheers,
Tom
Edited by gust0208, 27 September 2005 - 03:47 PM.