In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of March 31st
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #04 exercise sheet]


In this problem you should submit a file containing the function as described (without any main function). Inside the function do not print anything that was not asked!

[PII027] Maximum Difference

Max and Lucas are on high alert - Vecna is out there, and they need to find a way to detect his presence before it’s too late.

They’ve been tracking temperature fluctuations in Hawkins, suspecting that extreme changes might indicate Vecna's influence. To confirm their theory, they need to analyze an array of temperature readings and find the maximum absolute difference between any two consecutive measurements.

The Problem

Write a function int max_diff(int a[], int n) that receives an array a[] of size n and returns the largest absolute difference between two adjacent values in the array.

Constraints

The following limits are guaranteed in all the test cases that will be given to your program:

-100 ≤ a[i] ≤ 100       Numbers in the array
2 ≤ n ≤ 100       Size of the array

Submission

You should submit a .c file containing the requested function, without any main function and without printing anything. You can however create additional methods, if you need them.

Mooshak will use the following code to link to your function, read the inputs and call your method, printing its result.

#include <stdio.h>

// Forward declaration of function to implement
int max_diff(int [], int);

int main(void) {

  // Read array of n integers
  int n;
  scanf("%d", &n);
  int a[n];
  for (int i=0; i<n; i++)
    scanf("%d", &a[i]);

  // Print the array (assume size >= 1)
  printf("a = [%d", a[0]);
  for (int i=1; i<n; i++)
    printf(",%d", a[i]);
  printf("]\n");

  // Call the max_diff function and print the result
  printf("max_diff(a, %d) = %d\n", n, max_diff(a, n));
  
  return 0;
}

Example Input 1 Example Output 1
5
10 -3 42 20 5
a = [10,-3,42,20,5]
max_diff(a, 5) = 45

Explanation of Input 1

The largest temperature difference is 45, between -3 and 42.


Example Input 2 Example Output 2
10
3 -5 1 1 7 7 3 3 10 11
a = [3,-5,1,1,7,7,3,3,10,11]
max_diff(a, 10) = 8

Explanation of Input 2

The largest temperature difference is 8, between 3 and -5.


Programming II (CCINF1002)
DCC/FCUP - University of Porto