Computer Science Canada

Adding array variables together.

Author:  Brendon [ Sat Jan 12, 2008 7:26 pm ]
Post subject:  Adding array variables together.

First post Wink
Anyways, I got kicked out of school, and thus I have to continue my computer tech course at home. Im having trouble getting these array's to add up. Where I have put the "?????" I dont know the line of code to have it add up all the values in the array. Before I did it, without the array by simply sub:=total(x) + total(x) ect for the number of values there were. Now, with the sentenial loop where there could be any amount of "total(x)" variables, I am unsure of how you would make the command to add them up. I hope you follow my question. Rolling Eyes


Here is my code. I have highlighted the line I am unsure of in bold. Any help is GREATLY appreciated.


code:
%brendon soetemans
%array4

var item:array 1..100 of string
var quantity,price,total:array 1..100 of real
var sub,pst,gst,final:real
var x:int

x:=1

loop
put"(press end to end loop) Enter name of item # ",x,"..."..
get item(x):*
cls
exit when item(x)="end"
cls


put"Enter price of ",item(x),"...$"..
get price(x)
cls

put"Enter quantity purchased of ",item(x),"..."..
get quantity(x)

cls
total(x):=price(x) * quantity(x)
[b]sub:=???????????????????[/b]     [i]Usually I would have put sub:=total(x) + total (x) + total (x) for how many array inputs[/i]
gst:= sub * 0.06
pst:= sub * 0.08
final:= sub + gst + pst
x:=x+1
end loop

locatexy(0,300)
put"Item":10,"Quantity":10,"   Cost":10,"  Total":10
put" "
for q:1..x-1
put item(q):10,quantity(q):5,price(q):10,total(q):10
end for
put" "
put"The sub total cost is $",sub:0:2
put"The gst cost is $",gst:0:2
put"The pst cost is $",pst:0:2
put"The total cost is $",final:0:2

Author:  Tony [ Sat Jan 12, 2008 7:31 pm ]
Post subject:  RE:Adding array variables together.

if you know how many element you have in an array so far (with a use of a counter variable, for example), you could use a for-loop to go through all of those variables.

Author:  Clayton [ Sat Jan 12, 2008 7:53 pm ]
Post subject:  RE:Adding array variables together.

Also, GST has gone down 1% Wink

Author:  StealthArcher [ Sat Jan 12, 2008 8:36 pm ]
Post subject:  RE:Adding array variables together.

For Great Justice.

But yeah, for loop, set up to add the values is best, for a challenge, make it caluculate if each object gets charged either type of tax, and have it calculate it in that foor loop, item by item.

Author:  Brendon [ Sat Jan 12, 2008 9:33 pm ]
Post subject:  RE:Adding array variables together.

I have never done anything like this before....would anyone mind editing the source so it works?

im sorry, im not all that great with turing.

Author:  StealthArcher [ Sat Jan 12, 2008 9:35 pm ]
Post subject:  RE:Adding array variables together.

We don't do work for you here.

If it's my query that gets you, think about multidimensional arrays, make another 2 dimensions for pst applicable and gst applicable, then as each items price is checked, check for pst/gst applicability, and add it to the item's price, then add the total to the main total.

Author:  Tony [ Sat Jan 12, 2008 10:19 pm ]
Post subject:  Re: RE:Adding array variables together.

Brendon @ Sat Jan 12, 2008 9:33 pm wrote:
would anyone mind editing the source so it works?

That is against forum's policy. You are better of figuring this out on your own anyway, it's not difficult
Turing:

var nums : array 1..10 of int := init (1,2,3,4,5,6,7,8,9,10)
var total : int := 0
for i: 1..10
   total += nums[i]
end for
put "the total sum of the array is: ", total


: