Qbasic, Quickbasic/QBASIC HELP
I am taking an online class for basic and am absolutely terrible with writing code and programs. I do not really know why i signed up for this class but i could use some help.
1. Salespeople at XYZ Company are paid a base salary of $10,000. This salary may be augmented by commissions, which are equal to 10 percent of gross sales, and by a bonus of $500. The bonus is awarded only to salespeople with more than $80,000 in gross sales. Compute and output the amounts earned by each of these salespeople:
Salesperson Gross Sales
Carlos Ortiz $90,000
Jill Johnson $70,000
Don Williams $20,000
Dee Jones $95,000
Al Ennis $40,000
Your output should include the name of each salesperson and his or her earnings. Use trailer values at the end of your data list so that your program can sense when there are no more data.
I have attempted this and come up with the following..not sure if this is correct.
DIM f AS INTEGER
DIM person AS STRING, grosssale AS DOUBLE
DIM salary AS DOUBLE
DIM ff AS STRING
f = FREEFILE
ff = "c:\tmp2\qbasic\FINALsales.dat"
OPEN ff FOR INPUT AS #1
WHILE NOT EOF(f)
INPUT #f, person, grosssale
salary = 10000 + .1 * grosssale
IF grosssale > 80000 THEN
salary = salary + 500
PRINT person; " : "; salary
2. A company running a copying service charges the following rates:
The first 500 copies are billed at 5 cents per copy.
The next 500 copies are billed at 4 cents per copy.
Any additional copies are billed at 3 cents per copy.
Compute and output the amount each of the following customers is to be billed:
XYZ Amalgamated 1,200
ABC Industries 200
TR Systems Limited 800
Your output should include the name of each customer as well as the billing amount. Use trailer values at the end of your data list so that your program can sense when there are no more data.
What i had come up with:
Console.WriteLine(" XYZ " & vbTab & vbTab & " 1200 " & vbTab & getAmount(1200))
Console.WriteLine(" ABC " & vbTab & vbTab & " 200 " & vbTab & getAmount(200))
Console.WriteLine(" TR " & vbTab & vbTab & " 800 " & vbTab & getAmount(800))
Function getAmount(ByVal numCopies As Integer)
'The first 500 copies are billed at 5 cents per copy
'The next 500 copies are billed at 4 cents per cop
'Any additional copies are billed at 3 cents per copy
Dim amount As Integer = 0
If numCopies >= 1000 Then
amount = (500 * 0.05) + (500 * 0.04) + ((numCopies - 1000) * 0.03)
ElseIf numCopies > 500 Then
amount = ((numCopies - 500) * 0.04) + (500 * 0.05)
amount = (numCopies * 0.05)
6. Write a program that reads the following two dimensional array,
8 7 3
2 4 1
6 5 8
Adds the number 5 to each element (number) of the array, and prints the result.
Your out put will look like:
13 12 8
7 9 6
11 10 13
I do not know how to do this at all.
7. File Processing:(very similar to your Home work in chapter 13)
1) Create a file called Employee with the following data: Name, Pay Rate, and Hours Worked. Use the following data: “John Doe”, 6.3, 40; “Mary Smith”, 7, 35; “Ann Jones”, 5, 20.
2) After you create the file in #1, read and print the file with the data created in #1. Use suitable headings.
3) Use the same data file and print name, pay rate, hours worked, and gross pay (hours * rate).
What i Have come up with
REM *** PROGRAM TO CREATE A SEQUENTIAL FILE ***
REM *** Variables Used ***
REM Nam$ Name
REM HorW$ Hours Worked
REM PayR$ Pay Rate
REM *** Program Mainline ***
REM *** Create The File ***
OPEN "Employee.DAT" FOR OUTPUT AS #1
REM *** Load The File ***
WRITE #1, Nam$, PAyR$, HorW$
DATA "John Doe", 6.3, 40; "Mary Smith", 7, 35;
"Ann Jones", 5, 20.
REM *** Close The File ***
REM *** END OF PROGRAM ***
The first thing I want to clarify is if this is for QBasic or VisualBasic. Your code to #2 looks like VisualBasic, while the other code is QBasic, so you're going to possibly run into problems with that (unless it is on purpose that some of your assignments are VB).
For your first question, everything looks alright, but it does look like the assignment may be expecting you to use DATA statements rather than a file, so you may want to clarify with your teacher. Otherwise, it looks good - the main way to test, though, is for you to run it, and see what it outputs - do the calculations on a scrap piece of paper or calculator, to see that you're program is doing what you want.
For the second question, you may need to do some minor tweaks for your if statements. For instance, the pricing should look like this:
1-500 copies = $0.05/copy
501-1000 copies = $0.04/copy
1001-... = $0.03/copy
With that in mind, your first if statement should be ... > 1000 and not ... >= 1000.
Otherwise, your answer looks good.
Two dimensional arrays aren't as tricky as it sounds. A one dimensional array could look like this: DIM oneDimension(1 TO 100) AS INTEGER
This means that there is one row of data.
A two dimensional array could look like this: DIM twoDimensions(1 TO 10, 1 TO 3) AS INTEGER
This means that we have multiple rows of data, and a possible 30 (10x3) array elements to use.
If you're a math person, think of it as having a sheet of grid paper and using my example 2 dimensional array, you have 10 lines across, and 3 lines down, which would look like this:
| | | | | | | | | |
| | | | | | | | | |
Now imagine that every "+" is a place where you can store data. The top left would be (1,1), just like a graph coordinate. To assign something to it, you would do: twoDimensions(1,1) = 999 , or to retrieve it's data, you would do something like PRINT twoDimensions(1,1) .
The top right "+" would be (10,1), bottom right is (10,3), and bottom left is (1,3) .
All programming languages can use fixed arrays, meaning they are a known and unchanging size. This is important because in our current case, if I tried to do: PRINT twoDimensions(0,0) , QBasic would throw an error because the array indexes do not exist, and the same would happen with:
Math works the same, so if you wanted to increment a number by 5 in the array, you could do:
twoDimensions(1,1) = twoDimensions(1,1) + 5
Looping through a two dimensional array is pretty easy, too. Think of it as looping through an array that has, instead of INTEGER, another array.
FOR firstIndexes = 1 TO 10
FOR secondIndexes = 1 TO 3
PRINT twoDimensions( firstIndexes, secondIndexes )
If you're still stuck on this question, show me what you tried and we can work on it further.
For the last question, it looks like you're over-complicating your code, and as a result, getting a little confused (don't worry, it happens to the best of us).
Your label structure is good, but the code in those portions isn't correct. Let's break down what the assignment is asking:
1.a. Create a file for writing called Employee
1.b. Populate the file with data
1.c. Close the file from writing
2.a. Open the Employee file for reading
2.b. Get the data from the file and output it to the screen with headings
2.c. Close the file from reading
3.a. Open the Employee file for reading
3.b. Get the data from the file and output it to the screen with headings, AND also calculate the gross pay of each employee
3.c. Close the file from reading
It may be unnecessary to open/close the file twice between tasks 2 and 3, but I'll leave that up to you. It's always good practice to close the file as soon as your done with it.
So, these are the labels that I would use:
'' Step 1a,b,c
'' Step 2a,b,c
'' Step 3a,b,c
There are some considerations you are going to need to take into account when coding this assignment. First, is that since there are math calculations, it's going to be really important to use the appropriate numerical variable types for some variables. For instance, you will need to do calculations using the pay rate and hours worked. Pay rate looks like it uses decimals, so you will want to use either SINGLE or DOUBLE (because it's smaller numbers, SINGLE should be fine). Hours looks like it is just an INTEGER, but be careful when doing calculations with INTEGERs and decimal numbers, because QBasic will always make it those calculations become INTEGERs (meaning it will chop off the decimal places, even if those decimal numbers are important!). I'd recommend storing both as SINGLE, to avoid any accidental decimal problems. This means using the ! suffix instead of $ (which is string).
So your WRITE statement would look more like this: WRITE #1, Nam$, PayR!, HorW!
But this also isn't taking into account that your LoadFile label/code doesn't actually read your DATA statement, which is also not properly written (after your semi-colons, you should have another DATA statement before the same of the employees)
With all this in mind, you should give this assignment another shot, and show me any code you're having problems with.
I hope that points you in the right direction,