C++ program to find the difference of adjacent elements of the array using class

Given an array, we have to find the difference of adjacent elements of the array using the class and object approach.
Submitted by Shubh Pachori, on September 13, 2022

Example:

Input: 
[0]: 1
[1]: 3
[2]: 5
[3]: 7
[4]: 9
[5]: 2
[6]: 4
[7]: 6
[8]: 8
[9]: 10

Output: 
Difference of index 0 and 9 is -9
Difference of index 1 and 8 is -5
Difference of index 2 and 7 is -1
Difference of index 3 and 6 is 3
Difference of index 4 and 5 is 7

C++ code to find the difference of adjacent elements of the array using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Array {
  // private data member
 private:
  int arr[10];

  // public member functions
 public:
  // getArray() function to insert the array
  void getArray() {
    for (int index = 0; index < 10; index++) {
      cout << "[" << index << "]:";
      cin >> arr[index];
    }
  }

  // differenceArray() function to find difference of the
  // adjacent elements of the array
  void differenceArray() {
    // initialising int type variables
    // to perform operations
    int index_1, index_2;

    // for loop to add all adjacent elements of the array
    for (index_1 = 0, index_2 = 9; index_1 <= 9 / 2; index_1++, index_2--) {
      cout << "Difference of index " << index_1 << " and " << index_2 << " is "
           << arr[index_1] - arr[index_2] << endl;
    }
  }
};

int main() {
  // create object
  Array A;

  // calling getArray() function 
  // to insert array
  A.getArray();

  cout << "\n";

  // calling differenceArray() function 
  // to find difference of
  // all adjacent elements of the array
  A.differenceArray();

  return 0;
}

Output:

[0]:10
[1]:9
[2]:8
[3]:7
[4]:6
[5]:5
[6]:4
[7]:3
[8]:2
[9]:1
Difference of index 0 and 9 is 9
Difference of index 1 and 8 is 7
Difference of index 2 and 7 is 5
Difference of index 3 and 6 is 3
Difference of index 4 and 5 is 1

Explanation:

In the above code, we have created a class Array, one int type array data members arr[10] to store the elements of the array, and public member functions getArray() and differenceArray() to store the array elements and to find difference of all adjacent elements of the array.

In the main() function, we are creating an object A of class Array, reading the inputted array by the user using getArray() function, and finally calling the differenceArray() member function to find difference of all adjacent elements of the array. The differenceArray() function contains the logic to find difference of all adjacent elements of the array and printing the result.

C++ Class and Object Programs (Set 2) »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.