problem witth sum of elements in array with recursion

I must do the sum of elements in arry with recursion and I must use codeblocks.
I have "cannot open output file, name.exe permission denied"
What's problem?

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 <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>

using namespace std;

int somma(int A[], int n)
{if (n=0)
return 0;
else
return A[n] + somma (A, n-1);
}

int main()
{
int n;
cin>>n;
int A[n];
cin>>A[n];
cout<<somma(A, n);
}
 
Last edited on
Problem is nothing to do with your code.

Problem is you are still running the program you made. Stop running it.
But there are lots of issues with the code.
> {if (n=0)
Using = where you should be using ==

> return A[n] + somma (A, n-1);
A[n] is an out-of-bound memory access, based on your initial call in main.

> int A[n];
This isn't valid C++, since variable length arrays are not supported.

> cin>>A[n];
This is an out of bound memory access as well.
Also, it does NOT automatically read in n integers and populate the array for you.



Excuse me salem but I don't understand well the problem

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 <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>

using namespace std;

int somma(int A[], int n)
{if (n==0)
return 0;
else
return A[n] + somma (A, n-1);
}

int main()
{
int n;
cin>>n;
int A[n];
cin>>A[n];
cout<<somma(A, n);
}
 


probably so is right



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

using namespace std;


int somma(int A[], int n)
{if (n==0)
return 0;
else
return A[n-1] + somma (A, n-1);
}

int main()
{

int A[5] = {1, 2, 3, 4, 5};
int n=5;
cout<<somma(A, n);
}

Last edited on
Well your 2nd one looks good.

But the first one needs a loop if you want to input N integers.
I can't understand this kind of loop...

is it right?

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


#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>

using namespace std;


int somma(int A[], int n)
{if (n==0)
return 0;
else
return A[n-1] + somma (A, n-1);
}

int main()
{

  int n=7;
  int A[n];
  for(int i=0; i<n; i++)
  {
    cin>>A[i];
  }

cout<<somma(A, n);
}
Last edited on
With this I must search if all the numbers of array are greatest then 10.... but I have problems.. always with recursion (and bool).

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
38
39
40

#include <cstdlib>

#include <ctime>

#include <cmath>

#include <iostream>


using namespace std;



bool tutti(int A[], int n)

{if (n<=10)

  return false;

else

return tutti (A,n-1);

}


int main()

{

int A[6] = {11, 21, 101, 41, 51,61};

int n=6;


cout<<tutti(A, n);

}
Last edited on
> if (n<=10)
You need to be comparing each A[n] with 10.
Not checking to see if you have less than 10 elements left in your array.

> With this I must search the number greatest then 10
You should try to solve the problem using a loop first.
If for no other reason, to make sure you actually understand the problem.

Always result 0..

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
38
39
40

#include <cstdlib>

#include <ctime>

#include <cmath>

#include <iostream>


using namespace std;


bool tutti(int A[], int n)

{

  if (A[n]<=10)

    return false;

  else

    return tutti (A,n-1);

}


int main()

{

  int A[6] = {11, 21, 101, 41, 51,61};

  int n=6;

  cout<<tutti(A, n);

}
You're supposed to check ALL the elements of the array.

Not bail out completely when the first element less than 10 is found.

Again, try and write it as a simple loop BEFORE you try and make it recursive.

Typing in random recursive code won't get you anywhere until you actually understand the problem.
2nd problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

bool tutti(int ar[], int size){
	if(size==0) return true;
	if(ar[size-1]<=10) return false;
	tutti(ar, size-1);
}

int main(){
	int aa[5] ={12,14,16,18,20};
	int ab[5] ={12,14,16,8,20};
	
	cout << tutti(aa,5)<< endl;
	cout << tutti(ab,5)<< endl;
}
Thanks!!!!
Topic archived. No new replies allowed.