Array

Develop a function that finds the greatest difference between two consecutive elements stored in an array. Develop a 9 element array of doubles to test your code. Print to the screen which two numbers have the greatest difference as well as the value of the difference. Finally include an overloaded version of the function that will work if the array is composed of integers. Include your code used to test this function.
C++ has a couple of functions that can make this almost trivial: adjacent_difference() and max_element().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <cmath>

template<typename Iter>
void show_max_difference(Iter beg, Iter end)
{
    // calculate the absolute differences
    std::vector<double> diffs;
    std::adjacent_difference(beg, end, back_inserter(diffs), [](double x, double y){return std::abs(x-y);});

    // find the largest one
    std::vector<double>::iterator it = std::max_element(diffs.begin()+1, diffs.end());
    size_t idx = std::distance(diffs.begin(), it);
    std::cout << "The largest difference is " << *it << " between " << beg[idx-1] << " and " << beg[idx] << '\n';
}
int main()
{
    double a[9] = {1.93, -4.68, -5.05, -8.08, -2.95, -0.43, 7.03, -7.20, 9.86};
    show_max_difference(a, a+9);
    int a2[9] = {9, -4, -5, -8, -3, 0, 7, -7, 9};
    show_max_difference(a2, a2+9);
}

online demo: http://ideone.com/HmwmuV
Last edited on
Thank you for the reply. This is what I have so far. I am only a beginner programer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cmath>

using namespace std;

void GreatestDifference(int Array[], int size)
{
	int GreatestDiff = 0;
	int Difference = 0;
	int ArrayDiff[ 7 ];

			
for(int i = 1; i < size; i++)
{	
	ArrayDiff[i - 1] = Array[i -1] - Array[ i ];
}

for(int i = 1; i < 7; i++)
{
	if(ArrayDiff[i -1] > Difference)
	{
		Difference = GreatestDiff;
		
		cout << GreatestDiff << "\t" << ArrayDiff[i - 1] << endl;
	}
}

}

int main(void)
{
	int iNumbers[ 9 ] = {10,15,25,40,60,85,115,150,190};
		
	GreatestDifference(iNumbers, 9);
}
Here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
using namespace std;

void difference(double numbers[9])
{
    for(int i = 0;i < 8;i++)
    {
        cout << numbers[i] << " - " << numbers[i+1] << " = ";
        cout << numbers[i] - numbers[i+1] << endl;
    }
    cout << endl << "Reverse: " << endl;
    for(int i = 8;i > 0;i--)
    {
        cout << numbers[i] << " - " << numbers[i-1] << " = ";
        cout << numbers[i] - numbers[i-1] << endl;
    }
    cout << endl;
}

void Gdifference(double numbers[9])
{
    double maxnumber = -99999999;
    double leastnumber = 99999999;
    for(int i = 0;i < 9;i++)
    {
        if(numbers[i] > maxnumber)
        {
            maxnumber = numbers[i];
        }
        if(numbers[i] < leastnumber)
        {
            leastnumber = numbers[i];
        }
    }

    cout << "Biggest number: " << maxnumber << endl;
    cout << "Smallest number: " << leastnumber << endl;
    cout << "Greatest Difference: " << endl;
    cout << maxnumber << " - " << leastnumber << " = " << maxnumber - leastnumber << endl;
    cout << leastnumber << " - " << maxnumber << " = " << leastnumber - maxnumber << endl;
    cout << endl;
}

int main()
{
    double numbers[9] = {1,2,4,7,11,16,22,29,37};
    double numbers2[9] = {1.1, 35.3,2.4,8.5,3.24,26.4,6.1,57.8,33.3};

    cout << "Set 1" << endl;
    difference(numbers);
    Gdifference(numbers);
    cout << "Set 2" << endl;
    difference(numbers2);
    Gdifference(numbers2);

    system("PAUSE");
    return 0;
}
Last edited on
Cubbi gave a full valid working solution using proper practices. There is no need to further complicate the design and implementation and allow for bugs to get in with more ease.
How would there be bugs, and my program fits specifications. And I guess some extra stuff.
Bugs thrive in a complex environment. The simpler the code, the less bugs.
closed account (D80DSL3A)
Also, regarding "my program fits specifications" I'm afraid it does not.
Your Gdifference() function finds the greatest difference between any two elements, which is not the given problem.

Bugs? It won't even solve the non-given problem if all of the values are negative or >= 10^8.

EDIT: The usual way around that is to assign maxnumber = leastnumber = numbers[0], then start making comparisons at the 2nd element.
Last edited on
You could always make maxnumber -999999 etc. and leastnumber bigger. And assigning it to the first number is a bug. And It does meet specifications, Difference() calculates differences between 2 consecutive numbers, GDifference() calculates greatest difference. So it does fit specifications.
Topic archived. No new replies allowed.