Question :
Write a program to calculate and return the sum of distances between the adjacent numbers
in an array of positive integers.
Note:
You are expected to write code in the findTotalDistance function only which will receive the
first parameter as the number of items in the array and second parameter as the array
itself. You are not required to take input from the console.
Example
Finding the total distance between adjacent items of a list of 5 numbers
Input
input1: 5
input2: 10 11 7 12 14
Output
12
Explanation
The first parameter (5) is the size of the array. Next is an array of integers. The total
of distances is 12 as per the calculation below:
10-11=1
11-7=4
7-12=5
12-14=2
Total distance = 1+4+5+2 = 12
Answers
Answer:
do on your own
Explanation:
question is for your knowledge testing not for googling
Answer: C Program is given below.
Concept : Arrays in C language
Given : You are expected to write code in the findTotalDistance function
only which will receive the first parameter as the number of items
in the array and second parameter as the array itself. You are not
required to take input from the console.
To Find : Writing a program to calculate and return the sum of distances
between the adjacent numbers in an array of positive integers.
Explanation:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int findTotalDistance(int n, int arr[])
{
int difference, sum=0;
for( int i= 0; i<n-1; i++)
{
difference = abs(arr[i]-arr[i+1]);
sum = sum + difference;
}
return sum;
}
int main()
{
int n;
scanf("%d",&n);
int array[n];
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
scanf("%d",&start);
int result = findTotalDistance(n, array);
printf("\n%d",result);
return 0;
}
#SPJ3