Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

QBASIC


  • Please log in to reply

#16
banetistic

banetistic

    Member

  • Topic Starter
  • Member
  • PipPip
  • 23 posts
bdlt: I have the textbook for the class, so I think that's enough reading for me, heh. And about the code DIM A(10), I don't know what that is. We've only learned up to Chapter 4.3 in the book which is about functions. I don't think he would tell us to put things in the code that we didn't learn yet.

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

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

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
  • 0

Advertisements


#17
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
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']

View Post

[/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.

  • 0

#18
banetistic

banetistic

    Member

  • Topic Starter
  • Member
  • PipPip
  • 23 posts
For the main part of the program, I don't understand why line 4 (totalPop = getTotalPop) is written. What's it purpose?

And "format$" is a string right? That means I could call it anything I want?

Also the things in parenthesis after SUB doStats and SUB getData are the same. I don't really understand why and what purpose it serves.

I kind of understand the percentage calculation a little, but not entirely. Also, the PRINT TAB line. The teacher said it determines the spacing for the header. I just don't know how they come up with the coding.

Why are there so many PRINT statements?

When do you know to use a RESTORE statement?
  • 0

#19
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
In line 4, you have to assign the value returned by the "getTotalPop" function. Functions must return a value and will return the value in the variable of the same name as the function. So in the getTotalPop function, you see the last line is:
"getTotalPop = totalPop". This assigns the calculated totalPop variable to the variable getTotalPop that will be returned. Then the line 4 takes that returned "getTotalPop" variable and assigns it to the main program variable of "totalPop".

The lines after the parenthesis in SUB getData and doStats are the same, but those are not "lines", those are the variables you are passing into the function. Both functions need totalPop and format$ to do their job. I will go through the two functions again here. Here is SUB doStats:

SUB doStats(totalPop, format$)
CALL getData(totalPop, format$)
CALL getData(totalPop, format$)
CALL getData(totalPop, format$)
END SUB
This function uses the calls the getData subroutine to parse the data from the DATA statements. There are three DATA statements, so you need to call getData three times. You pass the variables totalPop and format$ to the subroutine since it needs the format to print to the screen and needs totalPop to calculate the last value - overall % or something. Here is SUB getData:

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
This function will take in the values stored in one DATA statement, calculate the percentage Males (perMales) and percentage Females (perFemales) and then print out those values based on the format defined in format$.

The percentage calculation is the same as any you have done before. If I have 4 X's and 6 Y's, the percentage of X's is 4 / (4+6). Percentage of Y's is 6 / (4+6).

To be honest, I not sure exactly the syntax for the PRINT TAB function, I am more trying to help with the basic coding and structure of the program.

You need to use a PRINT statement everytime you want to output to the screen. This program is basically designed to output a set of data and calculations to the screen. So, you are going to have a lot of PRINT statements.

You need to use a RESTORE statement after you have "used up" all of your DATA statements by calling READ statements (One READ statement "uses up" one DATA statement). You see in the initial getTotalPop subroutine, you call READ three times and have "used up" all of your DATA statements. But, we know you are going to need to use those DATA statements again to do our calculations and output them to the screen, so we use the RESTORE statement to "reset" our program to looking at the first DATA statement. This would be much easier to show if I just had a whiteboard!

Format$ is just a string and I assume you could use any variable name, but I am not a master of QBASIC and it may require the formating string variable to be called "format$". Also, it is good programming practice to use variable names that reflect their usage.

For the main part of the program, I don't understand why line 4 (totalPop = getTotalPop) is written. What's it purpose?

And "format$" is a string right? That means I could call it anything I want?

Also the things in parenthesis after SUB doStats and SUB getData are the same. I don't really understand why and what purpose it serves.

I kind of understand the percentage calculation a little, but not entirely. Also, the PRINT TAB line. The teacher said it determines the spacing for the header. I just don't know how they come up with the coding.

Why are there so many PRINT statements?

When do you know to use a RESTORE statement?

View Post


  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP