Question:
Could someone please break this pseudocode down for me so that I may better understand it?
Dsijjin
2007-10-31 06:10:33 UTC
I have the answer for the following question. I just need someone to explain it a tad different for me :) For instance, why did the user use a FOR statement etc and why did he or she start the code with sum=0.

QUESTION:
Write pseudocode for a program that allows the user to enter two integer values, a and b. Have the program do the following:

A. Find the sum of all whole numbers in the interval [a,b].
B. Find the product of all the odd numbers in the interval [a,b].

ANSWER:
a)
sum = 0
for integer i from a to b do
sum = sum + i

b)
product = 1
for integer i from a to b do
if i mod 2 = 1 then product = product*i

I just need some help trying to better understand the above. This is my first time doing this ;/

btw, ty tron and bm for helping me witht he original answer.
Three answers:
Bob R
2007-10-31 06:35:43 UTC
Ok, think of "sum" as a cardboard box that can hold a number. Before you start, what number's going to go in that box? We'll say 0 for now. I'll come back to this.



For integer i from a to b do



A 'for loop' is a typical way of doing something repeatedly a number of times. Think of an integer as a way of saying 'we're using numbers here'. i is another box, like sum.



Here's what happens.



A box with 'sum' written on it has the number 0 in it.

We're going to look at a box called 'a'. Whatever number's in that box we're going to put in a box called 'i'.

Now, we're going to keep increasing the number in the box called 'i' until it equals the value in another box called 'b'.

Every time we increase it, we're going to do this:

sum = sum + i



Which means take the number out of the box called 'sum', add the number in the box called 'i' and put it back in 'sum.



Let's try an example.



Suppose a=2 and b=4

sum = 0

we start the for loop...

i = 2

sum=sum + i (sum=0+2=2)

i = 3

sum = sum + i (sum = 2 +3 = 5)

i = 4

sum = sum + i (sum = 5 +4 = 9)

woa, i = 4, let's stop.



sum now equals 9.



In programming terms, the 'boxes' above are called variables. They're just a place that stores the value of a number (whatever variables are supposed to store, they always actually store numbers).



Now look back at that example. If you'd started that code with sum=1, you'd end up with 10 instead of 9...



Let's look at 'b'.

It's essentially doing the same thing for the first two lines.

The third line is a bit more complicated. Let's look at 'mod'.

Modulus is 'the remainder'. If I have 1050 mod 1000, I'm left with 50. 50 is the remainder.

So what is "i mod 2". Well:

0 mod 2 = 0

1 mod 2 = 1

2 mod 2 = 0

3 mod 2 = 1

4 mod 2 = 0



.... it tells you which numbers are 'odd'.



So this line is saying:

IF i is an odd number THEN multiply product by it.



An example.



Suppose a = 2 and b = 4

product = 1

i = 2

if i is an odd number ... no need to continue, it isn't.

i = 3

if i is an odd number, then product = 1 * 3 = 3

i = 4

if i is an odd number ... it isn't.

i is the same as b, stop.



product was 3!



Hope that explains it. Email if you're still unsure.
abdelrahmaner
2007-10-31 06:26:16 UTC
Hi,



Simply,



A)

suppose i have two number a=2, b=4



i want to sum number in between

i create a for loop looping on the interval "From a to b" and sum the interval values



Note: the pseudocode above is incomplete



int sum = 0;

for( i =a;i
{

sum += i;

}



Code is wrritten in C#



B)

consider the same two numbers



we want for find product of odd numers(that have a remainder when divided by 2)



int pro = 1;//Intializtion

for(i=a;i
{

if ( i%2 >0)

{

pro = pro * i;

}



i wish that could help

}
mnguyen_333
2007-10-31 06:26:39 UTC
sum is initialize to 0 otherwise the value will be unknown.

If you add something to unknow what do you get ? No one

knows. Think how you would sum up a list of thing.

for example, 2 5 1. Before you start adding your total is

0, after 2, total 2 after 5 total 7 and last one 8.



For is statement to repeat something over and over,

but you have to have a beginning and and end otherwise

it will never finish. In this case you start at A and end at B.

for example (for i=0; i<3; ++i) means go from 0 to 3 or repeat this loop 3 times.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...