Frage

my programming teacher gave me this problem to code it in c : given array of N integers A and a number K. During a turn the maximal value over all Ai is chosen, let's call it MAX. Then Ai = MAX - Ai is done for every 1 <= i <= N. Help Roman to find out how will the array look like after K turns.

Input

The numbers N and K are given in the first line of an input. Then N integers are given in the second line which denote the array A.

Output

Output N numbers on a single line. It should be the array A after K turns.

Constraints

* 1 <= N <= 10^5
* 0 <= K <= 10^9
* Ai does not exceed 2 * 10^9 by it's absolute value.

Example

Input:
4 1
5 -1 7 0

Output:
2 8 0 7

and my code to this problem is :

#include<stdio.h>
#include<stdlib.h>
#include<math.h>


long int Max(long int *arr, int low, int high)
{
long int max,i;

max = arr[low];
for(i=0;i<=high;i++)
{
    if(max<=arr[i])
      max = arr[i];
}

  return max;
}

 /* Driver program to test above function */
 int main()
 {
    long int max,*arr;
    long int n,k,c1,c2,c3,i,j;

    c1 = (long int)pow(10,5);
    c2 = (long int)pow(10,9);
    c3 = 2*c2;
    scanf("%ld %ld",&n,&k);

    if(n<1||n>c1)
      exit(1);
    else if(k<0||k>c2)
      exit(1);
    else
    {
    arr = (long int *)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
      {
        scanf("%ld",&arr[i]);
        if(abs(arr[i])>c3)
          exit(1);
      }
    if(k%2 == 0)
      {
        for(i=0;i<2;i++)
        {
            max = Max(arr, 0, n-1);
            for(j=0;j<n;j++)
            {   
                arr[j] = max-arr[j];
                if(abs(arr[j])>c3)
                            exit(1);
            }
        }
      }
    else if(k%2 != 0)
    {
        max = Max(arr, 0, n-1);
                    for(j=0;j<n;j++)
                       {       
                        arr[j] = max-arr[j];
                            /*if(abs(arr[j])>c3)
                              exit(1);*/
                       }
    }
    /*  for(m=0;m<n;m++)
                printf("%ld ",arr[m]);
        printf("\n");*/

    for(i=0;i<n;i++)
      printf("%ld ",arr[i]);

    printf("\n");
   }  
   return 0;
 }

i executed this code on gcc compiler in ubuntu, it is working perfectly with all the constraints satisfied but when I uploaded this code on my teacher's portal which has a compiler and executed the code, it said Runtime error -

nzec which means a non-zero exception which is used to signify that main() does not have "return 0;" statement or exception thrown by c++ compiler.

Please, can anyone help me what is wrong in my code as there is a return 0; statement in my code. Please Help.

Everyone has pointed out multiple use of exits ... Can I reduce them using any other way in place of exit()?

War es hilfreich?

Lösung

My guess is that it has to do with the various exit(1) statements you have for error conditions.

Andere Tipps

As pointed out by Dave Costa, exit(1) could be the cause

Another possible problem is the size of the allocated array:

arr = (long int *)malloc(sizeof(int)*n);

should be:

arr = malloc(sizeof(long int)*n);

And note that you don't need to use pow for constants:

c1 = (long int)pow(10,5);
c2 = (long int)pow(10,9);

could be replaced with:

c1 = 1e5L;
c2 = 1e9L;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top