0

how remove this error tried everything.... this program is about finding 5 closest number from a array... in main part i simply take array, num and size and passes through the function

void printclosest(int arr[], int x, int n)
{
int diff[30];
int i,j,k,p,a;
 for (i = 0; i < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (arr[i] > arr[j])
            {
                a =  arr[i];
                arr[i] = arr[j];
                arr[j] = a;
            }
        }
    }

for(i=0;i<n;i++)
{
diff[i]=abs(a[i]-x);

}


for (k = 0; k < n; ++k)
    {
        for (p = k + 1; p < n; ++p)
        {
            if (diff[k] > diff[p])
            {
                a =  arr[k];
                arr[k] = arr[p];
                arr[p] = a;
            }
        }
    }
    for(i=0;i<5;i++)
  { printf("%d",arr[i]);
   }
    }
4
  • 1
    Please include the line number when posting the error! Commented Apr 23, 2015 at 6:11
  • 1
    Please give some more detail about what you expect to happen. And please format your code properly. Commented Apr 23, 2015 at 6:12
  • First of all, please format your code so it's readable. Second of all, the compiler should be telling you what line the error is on. What line is it? Commented Apr 23, 2015 at 6:13
  • @Martze You are given a sorted list of N numbers and a number Num. Write a program to find the five numbers that are closest (numerically) to Num. A number x in the array is closest to Num if |Num-x| is the smallest among all such x possible. Note: If the Num is present in given list, then it should be in the output. Commented Apr 23, 2015 at 6:17

1 Answer 1

3

a is declared as int, yet you try to use it as an array here:

diff[i]=abs(a[i]-x);
Sign up to request clarification or add additional context in comments.

5 Comments

thanks buddy for help it compiled but i think my logic is incorrect i tried to get 5 closest numbers from array can you tell me where i went wrong
@avenger Post a new question, include the details and format the code. And read this: stackoverflow.com/help/how-to-ask
You are given a sorted list of N numbers and a number Num. Write a program to find the five numbers that are closest (numerically) to Num. A number x in the array is closest to Num if |Num-x| is the smallest among all such x possible.
@2051 i don't have limit to post questions now plz help
@avenger Sorry about the time limit, you will simply have to wait.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.