My This code is giving segmentation Fault, Please Tell me about Segmentation Fault

#include
#include
using namespace std;
int main() {
int n,target,s,l,r,count;
cin >> n;
int arr[n];

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

}
cin >> target;
sort(arr, arr + n);

for (int i = 0; i < n; i++) {
	s = target - arr[i];
	l = i + 1;
	r = n - 1;
	count = 0;
	while (l < r) {
		count = arr[l] + arr[r];
		if (count == s) {
			count = 0;
			cout << arr[i] << ", " << arr[l] << "and" << arr[r] << endl;
			l++;
			r--;
		}
		else if (count < s) {
			l++;
		}
		else if (count > s) {
			r--;
		}
	}
}
return 0;

}

Hey @nikhilthechamp871
Please share your code in Coding Blocks IDE

How do I share it on coding blocks

Go to https://ide.codingblocks.com/
paste ur code
press ctrl+S
click save
paste the url here

Hey @nikhilthechamp871
I think u have pasted the wrong code :slight_smile:

Hey @nikhilthechamp871
Change 1

int arr[1000]

change 2

  		cout << arr[i] << ", " << arr[l] << " and " << arr[r] << endl; //added space before and after and

yeah I tried it in python and it is giving wrong output

doing it again now…

btw can you tell me why segmentation error occurs…??

When u try to access illegal memory (not define or negative array index or something)

formal definition :
A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed