the assignment i am working on ask to write a function that uses a loop to find the sum of the squares of all integers between 1 and n. then asks "what is the order of your functions?"

my question, which may be obvious is what is n? and what does is the second question looking for "what is the order of your function?". will someone please clarify for me as I am new to C++

Thank you in advance

Recommended Answers

All 8 Replies

my question, which may be obvious is what is n?

Whatever value you want to solve to. If you want the sum of squares from 1 to 5, it would be 5; if you want the sum of squares from 1 to 10, it would be 10.

and what does is the second question looking for "what is the order of your function?".

http://en.wikipedia.org/wiki/Big_O_notation#Common_orders_of_functions

could i get some help on getting started? i have no idea how to find the sum of the squares of all integers. would the following be right?

int main()
{
for (int i = 2; i <= n; i++)
sum = i + n;

return 0;
}

If i were a value between 1 and n, then i * i would be a square of that value.

Start out with a sum of zero. Set up a loop to go from 1 to n (inclusive). Each time through the loop add the squared value to the sum.

like this? or am i still way off?

int main()
{

int count=0;
    while (count<=n)
    {
        count = count + i^i
    }

return 0;
}

this is what i have. the program compiles without error but is returning 0 as a value

int main()
{
int n;
int sum;

for (int i = 0; i <= n; i++)
sum = i*i + i;

cout << "the sum is: " << sum;
return 0;
}

Have you been introduced to functions yet? I would suspect a function taking an int argument as n would be what is invisioned:

int sum_of_squares(int n)

This function might then be called in main, for example, like this:

int answer = sum_of_squares(5);

>int sum;

Start out with a sum of zero.

>for (int i = 0; i <= n; i++)

Set up a loop to go from 1 to n (inclusive).

>sum = i*i + i;

i * i would be a square of that value.

Each time through the loop add the squared value to the sum.

thank you, i was close just needed to add the function. you had given me a link for the second part of the question but i do not understand how to find the order of the function through the link. could you explain that please.

i do not understand how to find the order of the function through the link. could you explain that please.

I'm not that good at such things, and the answer is in that article -- but I usually need to read it many times over.

If a function f(n) can be written as a finite sum of other functions, then the fastest growing one determines the order of f(n).

Looking at what is there for the function you've written, if n is 1, the loop executes one time.
If n is 2, the loop executes 2 times.
If n is 3, the loop executes 3 times.

My guess is linear.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.