-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hi everyone, I would like to add one question which would be a gainer to the bar of practise in array. #189
Comments
#include // Function to generate Special Triangle
|
how to work this
…On Wed, 30 Jun 2021 at 19:05, Harshit Singh ***@***.***> wrote:
#include
using namespace std;
// Function to generate Special Triangle
void printTriangle(int A[] , int n)
{
// Base case
if (n < 1)
return;
// Creating new array which contains the
// Sum of consecutive elements in
// the array passes as parameter.
int temp[n - 1];
for (int i = 0; i < n - 1; i++)
{
int x = A[i] + A[i + 1];
temp[i] = x;
}
// Make a recursive call and pass
// the newly created array
printTriangle(temp, n - 1);
// Print current array in the end so
// that smaller arrays are printed first
for (int i = 0; i < n ; i++)
{
if(i == n - 1)
cout << A[i] << " ";
else
cout << A[i] << ", ";
}
cout << endl;
}
// Driver function
int main()
{
int A[] = { 1, 2, 3, 4, 5 };
int n = sizeof(A) / sizeof(A[0]);
printTriangle(A, n);
}
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#189 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ASKBFYL4AYXDTTMA54NA7ALTVMMSBANCNFSM47SLIOGA>
.
|
using VS code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Question is - Given an array of integers, print a sum triangle from it such that the first level has all array elements. From then, at each level number of elements is one less than the previous level and elements at the level is be the Sum of consecutive two elements in the previous level.
Input : A = {1, 2, 3, 4, 5}
Output : [48]
[20, 28]
[8, 12, 16]
[3, 5, 7, 9]
[1, 2, 3, 4, 5]
Explanation :
Here, [48]
[20, 28] -->(20 + 28 = 48)
[8, 12, 16] -->(8 + 12 = 20, 12 + 16 = 28)
[3, 5, 7, 9] -->(3 + 5 = 8, 5 + 7 = 12, 7 + 9 = 16)
[1, 2, 3, 4, 5] -->(1 + 2 = 3, 2 + 3 = 5, 3 + 4 = 7, 4 + 5 = 9)
The text was updated successfully, but these errors were encountered: