Need some help with Home work

Nov 24, 2018 at 6:23pm
Just got some home work where my teacher asked me to write some functions.
I honestly have no idea where to start...

first is a function that sums all the values from 0 to n.It accepts one parameter which is the value to sum to inclusively.

the next one is a customized method that recursively calculates the length of the string passed as an argument.

Any help is much appreciated!!

Nov 24, 2018 at 6:33pm
Start simple with the first function.
In main you just get the input of n and call the function sum with n as parameter.
In sum you need only a simple for loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

/*
 * Returns the sum of all numbers from 0 to n inclusive
 */
int sum(int n)
{
  // TODO - implement me
}

int main() 
{
  
}
Nov 24, 2018 at 6:36pm
And what is that for loop?
Last edited on Nov 24, 2018 at 6:37pm
Nov 24, 2018 at 6:40pm
the loop sums all the values from 0 to n, although this is also doable by a known equation … you can directly compute the answer instead, but you should know how to do the loop or you will never get anywhere.
Last edited on Nov 24, 2018 at 6:42pm
Nov 24, 2018 at 6:41pm
how would you write that?
and ouch my feelings
i seriously dont know how to write the loop
Last edited on Nov 24, 2018 at 6:44pm
Nov 24, 2018 at 6:42pm
Give it a try
Nov 24, 2018 at 6:47pm
int i = 0;
for(int i = 0; i < n; i++)
cout << i

i know that isnt right but its what i know
Last edited on Nov 24, 2018 at 6:47pm
Nov 24, 2018 at 6:56pm
Declaring variables with the same name can cause problems. It's not good to do int i = 0 both inside and outside the loop.

You want 0 to n inclusive. Your loop isn't reaching 'n' right now.

You need a second variable to store the result of your calculation. For each number i from 0 to n, add i to the result. Return the result from your function. Remember to initialize the result to zero before the loop.
Nov 24, 2018 at 7:00pm
result = 0;
for(int i = 0; i <= n; i++) {
result = result + i;
}
return result;

Is this correct????
Last edited on Nov 24, 2018 at 7:04pm
Nov 24, 2018 at 7:06pm
Yes, just remember to declare result with a type, and remember all your semi-colons.

int result = 0;

Also, it is perfectly fine to do this:
result = result + i;
but this is the same thing and reads nicer:
result += i;
The latter statement combines the addition and assignment.
Nov 24, 2018 at 7:06pm
@chris simms
It's always fine to learn by asking questions (unless you're on Stack Overflow), but this is some really basic stuff. I think you'd be much better off if you just worked through some C++ tutorials.

(https://www.learncpp.com/ is my recommendation)
Nov 24, 2018 at 7:13pm
oh wait, the function needs to be recursive! how would i do that now?
Last edited on Nov 24, 2018 at 7:17pm
Nov 24, 2018 at 9:00pm
No for loop, then:

1
2
3
4
int sum_first_n_natural_numbers(int n) 
{
  return n <= 0? 0: n + sum_first_n_natural_numbers(n - 1);
}
Nov 24, 2018 at 11:45pm
good job getting the loop.
if you are being asked to do recursion and struggled with a for loop, you need to put in some study time as you are falling behind. And I am not trying to hurt your feelings, I am trying to warn you before you get so far behind it can't be fixed.


recursion is a function that calls itself and needs a base case. See how Mbozzi flipped the logic so it would stop on zero, counting from n down to 0 instead of up, to make it easier to write? So it calls itself with the input minus 1 until the input it zero, then pops back out with the sum.

if you call it with 5, it calls itself with 5+ sum of everything to 4.
4 + sum of everything to 3. 3+ sum to 2. 2+ sum to 1. 1+ sum to zero.
zero returns zero. 1 returns 1+0. 2 returns 2+ the previous result (1+0). 3 returns 3+ the previous result (2+1+0, stored as 3). and so on.



Topic archived. No new replies allowed.