what problem am i facing?????

This is my code for a program:

#include<iostream>
using namespace std;
int main()
{
int n=0,b=0;
float *a=new float [n];
cin>>n;
for(int m=0;m<n;++m)
cin>>a[m];
for(int i=0;i<n;++i)
for(int j=i+1;j<n;++j)
for(int k=0;k<n;++k)
if ((a[i]+a[j])/2==a[k])
++b;
cout<<b;
return 0;
}

But i am getting the following problem:

State: Runtime Error (ABRT)

Test Case #0
Runtime Error: ABRT
Description: Aborted (got SIGABRT)
Runtime: 0.01

Pls help!!!!
n needs to have the user input a value before you do

float *a=new float [n];
Wow code tags would be handy here, with the three nested loops and nested if.

I'm sure your problem is the fact that your array is size 0, it can't actually hold anything, so trying to access any index of that array is gonna be memory that doesn't belong to it.

Basically, this program is nonsense.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std; 
int main() 
{ 
int n=0,b=0; 
float *a=new float [n]; 
cin>>n; 
for(int m=0;m<n;++m) 
cin>>a[m]; 
for(int i=0;i<n;++i) 
for(int j=i+1;j<n;++j) 
for(int k=0;k<n;++k) 
if ((a[i]+a[j])/2==a[k]) 
++b; 
cout<<b;
return 0;
} 
Formatting... Do you not believe in tabs? Anyways, my point earlier about the array still stands.

EDIT:
What are you actually trying to do? This program makes no sense, I can't even begin to figure out your goal with it.
Last edited on
Basically this program will do the foll job:

You are given a sequence of integers a1, a2, ..., aN. An element ak is said to be an average element if there are indices i, j (with i ≠ j) such that ak = (ai + aj) / 2.
In the sequence
3 7 10 22 17 15
for i=1, j=5 and k=3, we get ak = (ai + aj)/2. Thus a3 = 10 is an average element in this sequence. You can check that a3 is the only average element in this sequence.
Consider the sequence
3 7 10 3 18
With i=1, j=4 and k=1 we get ak = (ai +aj)/2. Thus a1=3 is an average element. We could also choose i=1, j=4 and k=4 and get ak=(ai +aj)/2. You can check that a1 and a4 are the only average elements of this sequence.
On the other hand, the sequence
3 8 11 17 30
has no average elements.
Your task is to count the number of average elements in the given sequence.
Input format
The first line contains a single integer N indicating the number of elements in the sequence. This is followed by N lines containing one integer each (Line i+1 contains ai). (You may assume that ai + aj would not exceed MAXINT for any i and j).
Output format
The output must consist of a single line containing a single integer k indicating the number of average elements in the given sequence.
Test Data:
You may assume that N ≤ 10000. Further, you may assume that in 30% of the inputs N ≤ 200 and that in 60% of the inputs N ≤ 5000.
Example:
We illustrate the input and output format using the above examples:
Sample Input 1:
6
3
7
10
17
22
15
Sample Output 1:
1
Sample Input 2:
5
3
7
10
3
18
Sample Output 2:
2
Sample Input 3;
5
3
8
11
17
30
Sample Output 3:
0

Also i have made a change in the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std; 
int main() 
{ 
int b=0, n=0;
cin>>n; 
float *a=new float [n];  
for(int m=0;m<n;++m) 
cin>>a[m]; 
for(int i=0;i<n;++i) 
for(int j=i+1;j<n;++j) 
for(int k=0;k<n;++k) 
if ((a[i]+a[j])/2==a[k]) 
++b; 
cout<<b;
return 0;
} 
Last edited on
Topic archived. No new replies allowed.