User Tools

Site Tools


c:c_adjacent_differences_of_elements_in_array

C - C++ Adjacent differences of elements in array

#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
 
int diff (int x, int y) {return x+y;}
 
int main () {
  int a[] = {1,2,3,4,5,6,7,8,9,10};
  int b[10];
 
  cout<<"Given Elements:";
 
  for (int i=0; i<10; i++) cout << a[i] << ' ';
    adjacent_difference (a, a+10, b);
 
  cout << "\nDifference between adjacent numbers:";
 
  for (int i=0; i<10; i++) 
    cout << b[i] << ' ';
 
  cout << endl;
 
  adjacent_difference (a, a+10, b, multiplies<int>());
 
  cout << "Multiplying adjacent numbers using adjacent_difference: ";
 
  for (int i=0; i<10; i++) 
    cout << b[i] << ' ';
 
  cout << endl;
 
  adjacent_difference (a, a+10, b, diff);
 
  cout << "adding adjacent numbers using adjacent_difference:";
 
  for (int i=0; i<10; i++) 
    cout << b[i] << ' ';
 
  cout << endl;
  cout<<"Partial Sum:";
 
  partial_sum(&a[0], &a[10], &b[0]);
 
  for (int i=0; i<10; i++) 
    cout << b[i] << ' ';
 
  return 0;
}
c/c_adjacent_differences_of_elements_in_array.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki