May 4, 2019 at 8:57am UTC
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 May 4, 2019 at 9:07am UTC
May 4, 2019 at 9:41am UTC
Problem is nothing to do with your code.
Problem is you are still running the program you made. Stop running it.
May 4, 2019 at 10:03am UTC
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.
May 4, 2019 at 11:53am UTC
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 May 4, 2019 at 12:02pm UTC
May 4, 2019 at 12:06pm UTC
Well your 2nd one looks good.
But the first one needs a loop if you want to input N integers.
May 4, 2019 at 12:12pm UTC
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 May 4, 2019 at 2:16pm UTC
May 4, 2019 at 1:17pm UTC
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 May 4, 2019 at 2:03pm UTC
May 4, 2019 at 1:43pm UTC
> 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.
May 4, 2019 at 2:29pm UTC
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.