Author 
Message 
CyCLoBoT

Posted: Fri May 09, 2003 2:08 pm Post subject: Help with Arrays 


Is there anyway I can call two arrays to do an arithmetic operation like
Array 1(int) has Array 2 (char)
2 no value
no value +
3 no value
The output would be like
2+3 = 5






Sponsor Sponsor



Blade

Posted: Fri May 09, 2003 2:24 pm Post subject: (No subject) 


well i'm not sure what you mean... but if you use
code:  var num1:array 1..2 of int:=init(5,3)
var num2:array 1..2 of int
num2(1):=num1(1) * num1(2)
put num2(1)
num2(2):=num2(1) * num1(1)
put num2(2) 
you should get 15 for num2(1) and
75 for num2(2)
so you can do actions with arrays... they are just more than one variable with the same name.... if you want more info read
http://www.compsci.ca/bbs/viewtopic.php?t=366






CyCLoBoT

Posted: Fri May 09, 2003 2:43 pm Post subject: (No subject) 


Blade what I mean is that u see the user enters in an expression with maximum of four variables. let say (a+b)/(cd). After they enter this, I need to ask the user for the values of those four variables(a,b,c,d). After they enter the values, I have to solve that equation. So what I tried to do was to store in the operators in one array [(,+,)/,(,,)] and the values for the variables in other. I want to then combine those together to solve the equation.






Homer_simpson

Posted: Fri May 09, 2003 2:53 pm Post subject: (No subject) 


oh ic what you mean i think that's possible but what's the point of doing that...?






CyCLoBoT

Posted: Fri May 09, 2003 3:06 pm Post subject: (No subject) 


I making this calculator that does basic arithmetic but the user has to enter in the expression and the values he/she wants to calculate. I would appreciate it if someone can help me out






Homer_simpson

Posted: Fri May 09, 2003 4:18 pm Post subject: (No subject) 


oh ic...
i will create the code when i find the time...






Blade

Posted: Fri May 09, 2003 5:47 pm Post subject: (No subject) 


i dunno how you would store the operator in a variable or if you even can(i tried i tho and it doesnt work lol).... i would make a menu for each operator...






CyCLoBoT

Posted: Sat May 10, 2003 12:25 pm Post subject: (No subject) 


can someone please at least give me some idea as to how to approach this program. I really have no clue what to do






void

Posted: Sat May 10, 2003 1:44 pm Post subject: (No subject) 


make it simple....ask the user how many numbers (exluding the answer) are involved....create an array using that....then get the user to enter the operators and other stuff...using getch....so that if the ord of the getched key is equal to that of the addition sign...then get the next number...and then add the two numbers together...as for parantheses....im out of ideas...maybe try creating a procedure that stores the numbers and operators entered after a paranthese...and have your program exit at the point when the user enters a closing parantheses.....thats all i can think of rite now....






CyCLoBoT

Posted: Sat May 10, 2003 1:46 pm Post subject: (No subject) 


void u see I can't ask the user to enter number by number and calculate at the same time. The user has to enter in the whole expression first and then only I can ask them to enter in the values for the variables and then solve the expression






Blade

Posted: Sat May 10, 2003 2:20 pm Post subject: (No subject) 


i tried it voids way of using getch, chr, and ord and it wont work properly..
so.. make a menu
1. addition
2. subtraction
3. division
4. mutiplication
... and anything else you want
then when they choose 1 use a loop, and they will keep entering numbers till they enter something like 1 so
code:  put "Enter numbers (exit with 1)"
loop
exit when somevar = 1
end loop 
then you will have a variable that gets the number they entered, then you will have another variable that keeps adding the number... use the same method for everything else... but... i'll have to think a lil harder as to how you could incorperate other operators into it... like 2+5*3.. because my method is ok if you are only gonna use one operator... but this should get you started
but you cant use the *,+,,/ or div in variables, because once they are stored they are no longer procedures... so i'm not sure how you would do it, unless you keep total and you allow them to change what they are doing....
so you have a button that brings up a menu when they are adding, and then they change it to multiply, you keep the total, and go back into the loop, except this time they are multipying the numbers... then they wanna divide, so they press that button,and the menu comes back up in another window, and they choose divide, then they go back into the loop and divide..... thats all i can think of, if someone knows a way where you can do it voids way then post it.. cuz i wanna know too






Tony

Posted: Sat May 10, 2003 2:36 pm Post subject: (No subject) 


here's how I think of it... load in all the numbers into an array and all the operations into another array.
Sort the operation array acording to BDMAS. Now you just have ifelfif statments for one of the operations. Do that operation between the two numbers around the operation symbol, remove the symbol from the array and replace two numbers with 1 result...
might sound confusing, but I think thats how it should be done...

Tony's programming blog. DWITE  a programming contest. 




Blade

Posted: Sat May 10, 2003 3:07 pm Post subject: (No subject) 


you cant store the operations into a variable... if you do
code:  var operation:string(30):="+" 
or anything other type of variable it wont work.. try
code:  var operation:string(5):="+"
put 3 operation 4 
it wont work.. it'll give the error: is not an operation and hence cannot be called or something liek that, i tried int, and real and it says its the wrong type...






Tony

Posted: Sat May 10, 2003 3:31 pm Post subject: (No subject) 


no, thats not the idea... it should work more like
code: 
var operation:string
put "what math operation?"
get operation
put "2 ", operation, " 3 = "..
if operation = "+" then
put 2 + 3
elsif operation = "" then
put 2  3
elsif operation = "*" then
put 2*3
elsif operation = "/" then
put 2/3
end if


Tony's programming blog. DWITE  a programming contest. 




Blade

Posted: Sat May 10, 2003 3:42 pm Post subject: (No subject) 


ooo, ok.. that does make more sense... hahaha < stupid...






