Pseudocode

DrewChan

Expert Member
Joined
May 19, 2010
Messages
4,937
Reaction score
3
Location
Cape Town
Ello, anyone in the know please help-

How would you sum a group of integers in an array-

say the array was already loaded with 30 positive integers.
 
total = 0

for each value in array
add value to total


Pseudcode can have normal code in it, it just needs to be generic and should not be specific to any language. In the above example each language will be able to implement it
 
Hmm i made a big mess up

I did something in the range of

for x = 0 to 29
sum = sum(x+1)
next x
 
it should be something like

sum=0
for i from 1 to 30
sum=sum+array
end for


blargh that was it - how can I be so stupid..... sigh

after that wascounting those larger than average

k=0
for x = 0 to 29
if x > average
then k = k + 1
display "amount larger than average equals" k
endif
end
 
i !helping u
for i = won't do your homework
while u = should be learning

Hilariously funny - if my textbook had an example of adding up integers in an array I wouldn't have asked here, additionally this was not part of my homework.

Also those are illegal variable names :D
 
dim a as integer
dim b as integer
dim c as integer
dim d as integer
dim e as integer
dim f as integer
dim g a s integer
....
dim z as integer
dim answer as integer

answer = a+b+c+d+e+f+g+h+i j+k+l+m+n+o+p+q+r+s+t+u+v+w+x+y+z

messagebox.show "hello, world"
 
it should be something like

sum=0
for i from 1 to 30
sum=sum+array
end for


'Cept a lot of languages are 0 indexed so you'll run into a bit of an index out of bounds exception there.
 
Reset #n (n12.2) #n-tab (n10.2/1:10)

add #n-tab(*) to #n

Or

you can do a for loop
 

$numbers = array(1,2,3,4,5,6,7,8,9,10... 30);
var_dump(eval(implode('+',$numbers)));


PHP can do some truly bad things.
 
Top
Sign up to the MyBroadband newsletter
X