pseudocode...

T

train

Guest
anyone else knowing pseudocode...

how would you write the pseudocode for the program to determine the sum of all odd numbers from 1 - 99...

I didn't use a loop, and my instructor was not happy - and it didn't even ask for a loop...

isn't the purpose of programming to write as little as possible...

I'll show my "program commands" after a few posts...
 

Spiderman

Administrator
Staff member
I don't know about pseudocode (I mean, I do, but I can write it better in code), but I'd say:

x=0
y=1
while y< 100 do
x=x+y
y+y+2
end-while
 
S

Svenmonkey

Guest
Well, I think I know how to do it in Java, but it seems too simple so I'm probably wrong.

int sum, i=1;
for (i<=100)
{
if (i%2=1)
sum = i + sum;
i = i + 1;
else if (i%2 = 0)
i = i + 1;
}
System.out.println (+ sum);

Quite easily translated to pseudocode, there.
 
E

EricBess

Guest
In pseudocode:

Initialize accumulator
Loop from 1 to 99, stepping by 2
Within each loop, add loop value to accumulator

I'm assuming you took advantage of formulas to avoid a loop?

In C# (or C++):

{
int i,accum;

accum = 0;
for (i = 1; i +=2; i <= 99) accum += i;
}
 
T

train

Guest
Bess got it...

Actually it was a formula I've know since high scool... - don't know any C yet... trying to self-teach it though...

It was more like this...

START
READ ENDNUM
SUM = [(ENDNUM + 1) / 2] ^ 2
WRITE SUM
STOP

And I can prove this for any sequence adding sequential odds beginning with 1...

but he insisted on a loop....
 
S

Svenmonkey

Guest
No, they didn't.

Go back to your shoe now, and never come back.
 
T

train

Guest
yeah - the course leads into visual basic before moving on to C...
 
S

Svenmonkey

Guest
C is a programming language. It was improved to make C++, there, which you should have heard of before.
 
F

FmK-AnC

Guest
ive never gotton a c++ in school... whats that like a b? b-?
 
S

Svenmonkey

Guest
C++ is probably the most widely used programming language out there.
 
S

Shiro, Time Devourer

Guest
main()
{
int ctr;
int total;

for (ctr=1; ctr=99; ctr++)
{
total+=ctr;
}
printf("%d", total);
}
0+1+2+3+4+5.....al the way to +99. I like C.
 
Top