Help me please :)

I have to wright C++ code which calculates sum of numbers from 1 to n divided by 2 using recursion, but my program keeps crashing. (sorry for bad English)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include<iostream>
using namespace std;

int vw( int n,int sum,  int a)
{
  if(a%2 == 0)
  {
  return vw(n, sum + a, a + 1);
  }

  if (n = a)
  {
    cout << "E";
    return 1;
  }

   else
  {
    cout << sum/2;
    return vw(n, sum, a + 1); 
  }
}

int main()
{
 int n;
 int a = 1;
 int sum = 0;
 cout << "Enter n: ";
 cin >> n;
 
 vw(n,sum,a);
}
Last edited on
Compile with more warnings enabled.
1
2
3
4
5
$ g++ -Wall foo.cpp
foo.cpp: In function ‘int vw(int, int, int)’:
foo.cpp:11:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (n = a)
            ^

IE, watch your use of = where you meant ==

> cout << sum/2;
> return vw(n, sum, a + 1);
Aren't you meant to recurse with sum/2 as well?


I corrected code but I get exit status -1. (I forgot to mention that numbers have to be even )


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
using namespace std;

// calculating sum of even numbers from n to 1 divided by 2
int vw( int n,int sum,  int a)
{
  if(a%2 == 0) 
  {
  return vw(n, (sum + a) / 2, a + 1);
  }

  if (n == a)
  {
    cout << sum;
    return 1;
  }

   else
  {
    return vw(n, sum, a + 1); 
  }
}

int main()
{
 int n;
 int a = 1;
 int sum = 0;
 cout << "Enter n: ";
 cin >> n;
 
 vw(n,sum,a);
}
What do you expect to see?

> sum of numbers from 1 to n divided by 2
What does this mean?

Like this, only recursive?
1
2
3
int sum = 0;
for ( int i = 1 ; i <= n ; i++ ) sum += i;
cout << sum / 2;


Your recursion doesn't end, so it blows the stack.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// calculating sum of even numbers from n to 1 divided by 2
int vw( int n,int sum,  int a)
{
  cout << "n=" << n << ", sum=" << sum << ", a=" << a << endl;
  if(a%2 == 0)
  {
  return vw(n, (sum + a) / 2, a + 1);
  }

  if (n == a)
  {
    cout << sum;
    return 1;
  }

   else
  {
    return vw(n, sum, a + 1);
  }
}


It's up up and away!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ ./a.out 
Enter n: 2
n=2, sum=0, a=1
n=2, sum=0, a=2
n=2, sum=1, a=3
n=2, sum=1, a=4
n=2, sum=2, a=5
n=2, sum=2, a=6
n=2, sum=4, a=7
n=2, sum=4, a=8
n=2, sum=6, a=9
n=2, sum=6, a=10
n=2, sum=8, a=11
n=2, sum=8, a=12
n=2, sum=10, a=13
n=2, sum=10, a=14
n=2, sum=12, a=15
n=2, sum=12, a=16
n=2, sum=14, a=17
n=2, sum=14, a=18
// etc etc until crash 


I need to write this program using recursion:

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

int main() 
{
  int sum = 0 ;
  int n;

  cout << "n: ";
  cin >> n;

  for(int i = 1; i <= n; i++)
  {
    if (i%2 == 0)
    {
    cout << i <<  " ";
    sum = sum + i;
    }
  }
  
  cout << endl;
  cout <<"sum/2 = " <<sum/2 ;
  return 0;
}
Let's start with a simpler exercise of just calculating the sum of all integers from 1 to n.
Just to demonstrate how to turn a for loop into a recursive loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int sumOfInts ( int start, int end ) {
    if ( start <= end ) {
        return start + sumOfInts(start+1,end);
    } else {
        return 0;
    }
}

int main()
{
 int n;
 cout << "Enter n: ";
 cin >> n;
 cout << sumOfInts(1,n) << endl;
}
what does "start +" do?
 
return start + sumOfInts(start+1,end);
Last edited on
Get used to the idea of adding cout statements to code you don't understand.
1
2
3
4
5
6
7
    if ( start <= end ) {
        cout << "Adding " << start << " to the result" << endl;
        return start + sumOfInts(start+1,end);
    } else {
        cout << "Reached the end" << endl;
        return 0;
    }
Thank you salem c! It works finaly :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int func(int start, int end)
{
	if (start <= end && start%2 == 0)
	{
    cout << start << " ";
		return start + func(start + 1, end);
	}
  
  if(start%2 != 0)
  {
    return func(start + 1, end);
  }

	else
	{
    cout << endl;
    cout << "result: ";
		return 0;
	}
}

int main()
{
	int n;

	cout << "Enter n: ";
	cin >> n;

	cout <<(func(1, n))/2;
	return 0;
}

Topic archived. No new replies allowed.