The Handshake Program

In this program im counting the number of handshakes between certain number of people such that each person shakes hands with the other person only once.
The first line is my number of test cases
And after that each test case has an integer val <10^6

For eg: -
INPUT

2
1
2

OUTPUT
0
1

My code is

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
#include <iostream>
using namespace std;
void main()
{
	cout<<"Enter the number of test cases"<<endl;
	long t;
	cin>>t;
	long *hShake=new long[t];
	for(int i = 0; i<t; i++)
		cin>>hShake[i];
	cout<<endl<<endl<<endl;
	long val,sum=0,ctr=0;
	long *nArr=new long[t];
	int i=0;
	while(i<t)
	{
		val=hShake[i];
		for(long j = 0; j <val; j++)
		{
			nArr[j]=1;
		}
		for(long j=0; j<val-1; j++)
		{
			for(long l=ctr; l<val-1; l++)
			sum=sum+nArr[ctr]*nArr[l+1];
			ctr++;
		}
		cout<<sum<<endl;
		i++;
		sum=0;
		ctr=0;
	}
}


When im entering the test case as 1 and a value such as 50 it doesnt work and program closes but when i enter the value 10 i get the answer . Why is this happenning ?
If you enter 1 and 50, consider what happens on line 13. You allocate an array of length 1. Now, look at lines 18 to 27. Do you treat it as array of length 1?
Topic archived. No new replies allowed.