0
#include <bits/stdc++.h>

using namespace std;

int SegmentTree(int ss, int se, int si, int arr[], int &tree[]) {
  if (ss == se) {
    tree[si] = arr[ss];
    return arr[ss];
  }
  int mid = (ss + se) / 2;
  tree[si] = SegmentTree(ss, mid, ((2 * si) + 1), arr, tree) +
             SegmentTree(mid + 1, se, ((2 * si) + 2), arr, tree);
  return tree[si];
}

int main() {
  int n;
  cout << "Enter the size of the array: ";
  cin >> n;
  int arr[n];
  cout << "Enter the elements of the array: ";
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cout << "Our input array is:\n";
  for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  int tree[(4 * n)] = {0};
  cout << "\nOur output Segment tree for this array would be:\n";
  SegmentTree(0, n - 1, 0, arr, tree);
  for (int i = 0; i < (4 * n); i++) {
    cout << arr[i] << " ";
  }
  return 0;
}

Following are the errors: 'ss' was not declared in this scope 'se' was not declared in this scope 'tree' was not declared in this scope 'si' was not declared in this scope Please help me to find out what is wrong with this code. I'm not getting it. I use to write functions like this before and they use to work. But now it is not working.

Biffen
  • 6,249
  • 6
  • 28
  • 36
Aashwin
  • 1
  • 1
  • 2
    ([Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)) – Biffen Jun 11 '21 at 11:43
  • 4
    `int arr[n];` is illegal in standard C++ as the value of `n` must be known at compile time, not run time. Prefer `std::vector arr(n);` instead for this purpose. Same for `tree` later for the same reason. – Cory Kramer Jun 11 '21 at 11:43
  • 1
    `int &tree[]` is not how you declare a parameter as a reference to an array of unknown bound, you want `int (&tree)[]` (or `int tree[]`, as you never modify `tree`) – Caleth Jun 11 '21 at 11:45
  • 1
    The errors that follow are a result first error. With a valid declaration of the function, they go away. In general, if you are confused by errors, you need to look at them in the order they were reported, and ask about *all* of them – Caleth Jun 11 '21 at 11:50
  • 1
    "Not initialized" and "not declared" are not the same thing. – molbdnilo Jun 11 '21 at 12:28

1 Answers1

0

try the following code:

#include<iostream>
using namespace std;

int SegmentTree(int ss, int se, int si, int arr[], int tree[]) {
  if (ss == se) {
    tree[si] = arr[ss];
    return arr[ss];
  }
  int mid = (ss + se) / 2;
  tree[si] = SegmentTree(ss, mid, ((2 * si) + 1), arr, tree) +
             SegmentTree(mid + 1, se, ((2 * si) + 2), arr, tree);
  return tree[si];
}

int main() {
  int n;
  cout << "Enter the size of the array: ";
  cin >> n;
  int arr[n];
  cout << "Enter the elements of the array: ";
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cout << "Our input array is:\n";
  for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  int tree[(4 * n)] = {};
  cout << "\nOur output Segment tree for this array would be:\n";
  SegmentTree(0, n - 1, 0, arr, tree);
  cout<<endl;
  for (int i = 0; i < (4 * n); i++) {
    cout << tree[i] << " ";
  }
  return 0;
}