In: Computer Science
This program has to be written in assembly language using emu8086. Please choose the .EXE template. The requirements and the action is contained in the docx file.
You’ve been hired by Spacely Sprockets and they need to be able to write order information out to a file. Using all of the knowledge that we’ve gained in the last few sections on structures and files, you need to write a program that does the following:
Here’s a sample run:
Order Number: 105
Customer ID: 405
Sprocket Type: Awesome Atomic Sprocket
Part Quantity: 3
Order Total: 105.74
Struct written to file C:\asm\test.txt
Order Number: 105
Customer ID: 405
Sprocket Type: Awesome Atomic Sprocket
Part Quantity: 3
Order Total: 105.74
Program Complete!
Hints: Think procedures! You can simplify the code and your debugging life by writing procedures. For example, a print structure routine, a save to file routine, a read from file routine, et cetera. Secondly, DO NOT code this application in a single shot – incrementally build up each piece of logic otherwise you will get lost when things don’t work right.
Solution :-
know that floating point addition is not associative: (a +
b) + c
in general does not equal a + (b + c)
.
So this algorithm for sum can give a different result depending on
the order of the input:
float naive_sum(float[] input) {
float accumulator = 0;
for (float x : input) {
accumulator += x;
}
return accumulator;
}
Is it possible to make this order-independent, so that it returns the same result even when the input is shuffled? I'm not trying to reduce the rounding error: I just want it to be order-independent.
One idea is to sort the input first:
If you want to display something on screen you can use the
print()
function. An example of code that prints to
screen in IDLE is shown below:
print("Hello World")
This is what it would look like when run in IDLE:
Inputting Data
If you want the user to enter data into the program, you can use
the input()
function. An example of code that will ask
the user to enter their name and display it on screen using
print()
is shown below:
name = input("What is your name? ") # displays the message on screen and stores the input from the user in a variable called name
print("Hi "" +name)
This is what it would look like when run in IDLE:
Example program 1 - Enter a Word
The code for the program below will allow the user to enter a
word and store it in a variable called word. It will then use the
print()
function to output the word that they
entered.
word = input("Please enter a word ")
print("You entered the word " + word)
When run in IDLE:
Example program 2 - Address Program
The code for the program below will allow the user to enter
various pieces of information and store them in different
variables. The print()
function is then used to output
all of the information.
number = input("Enter your house number: ")
street = input("Enter your street name: ")
town = input("Enter your town/city: ")
county = input("Enter your county: ")
postcode = input("Enter your postcode: ")
print("\nAddress Details:\n" + "Street: " + number + " " + street + "\nTown/City: " + town + "\nCounty: " + county + "\nPostcode: " + postcode)
When run in IDLE:
You can concatenate (join together) variables with strings in a
print()
function. In the address example
print("Street: " + number + " " + street + "\nTown/City: " +
town)
will combine the strings “Street†and
“Town/City†with the variables number, street and town.
\n
is used to start a new line when it is displayed
on screen.
Variables
A variable is used to temporarily store a piece of data.
For example:
number1 = 10
In the code above the variable is called number1
and the value it is storing is 10. Variables can hold any type of
data. Using variables makes it easier for people to understand what
is going on.
For example:
cost = 15
VAT = 3
total_cost = cost + VAT
Casting Variables
Python will automatically decide what type of data a variable should be, sometimes this isn’t right, therefore you will have to convert/cast variables to a different data type.
Integer
The program below will multiply a number by 5. When data is
input from the user it will store it as a string. You will need to
convert the variable number
to an integer
before performing a calculation. An example of how you do this is
shown below:
number = input("Enter a whole number ")
answer = int(number) * 5 #converts the variable number to an integer and multiplies it by 5.
print(answer)
float sort_sum(float[] input) {
return naive_sum(sort(input));
}
sort
doesn't have to put the floats in numeric
order; it just has to to satisfy sort(input) ==
sort(shuffle(input))
. I think this works, but it's no longer
constant space and linear time the way naive_sum
was.
Another idea is to make the accumulator be a huge integer type: big enough to fit any float without rounding. If floats have an 11-bit exponent, you would need around 2^11 bits, which comes out to around 2000 bits.
float fixedpoint_sum(float[] input) {
int2048 accumulator = 0;
for (float x : input) {
accumulator += float_to_fixed(x);
}
return fixed_to_float(accumulator);
}