pseudocode problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main(){

	int number = 0;
	int sum = 0;

	cout << "Enter how many numbers input : ";
	cin  >> number;



	for( int i = 0 ; i <= number ; i ++ ){
		sum += i;
	}

	cout << "Total : " << sum << endl;

	system( "pause" );
	return 0;
}


this a simple code that from question want me to do
it's not hard by i just can't really know how to write in pseudocode

Write the pseudocode that uses a loop to find the sum of the squares of all integers between 1 and n. What is the running time (Big-O) of your algorithm?

I know my Big-O is = O(n)

but can someone teach me how to writ pseudocode base on my program?
i know it's simple but for me hard caus ei not so familiar
1
2
3
4
5
6
7
8
9
10
11
12
13
main

integer number , sum

input x

for n = 0 to x
   sum += n
end for

write sum

end main


isn't correct for my pseudocode?
Last edited on
sum of the squares


Looks like you're just getting the sum. As far as writing pseudocode, I'd follow along with how 'Introduction to Algorithms' does it, unless told otherwise by your instructor.

http://highered.mcgraw-hill.com/olcweb/cgi/pluginpop.cgi?it=gif::600::388::/sites/dl/free/0070131511/25320/on_line_maximum.gif::ON-LINE-MAXIMUM
Last edited on
@boorad

what you mean by sum of the square?

i already sum += n ?

sum += n is same with
sum = sum + n
instead of
sum += n
you should say
sum += (n*n)
but why?

sum += n*n
is equal
sum + n*n?

my input 4

should be 1 + 2 + 3 + 4
i can't get it why
If your input is 6, your output shouldn't be 1+2+3+4+5+6 BUT 1*1+2*2+3*3+4*4+5*5+6*6. Therefore in your pseudo code, your sum would be sum+=(iterator_value*iterator_value).
@matri X .
if my input is 6

answer will be 21


but if yours
1*1+2*2+3*3+4*4+5*5+6*6
answer will be 1+4+9+16+25+36 already

so how



someone help?
The question asks for "sum of the squares".
The square of the number n is n*n

Therefore the sum of the squares of the integers from 1 to 6 is
1*1 + 2*2 + 3*3 + 4*4 + 5*5 + 6*6
= 91

This is what your algorithm need to achieve.
sorry. is my fault. i didn't see sum of the square . i thought sum of the number. sorry. haha

and did i write pseudocode with write format?

isn't wrong? or?
Last edited on
Topic archived. No new replies allowed.