[ create a new paste ] login | about

Link: http://codepad.org/w74H4J6I    [ raw code | output | fork ]

C++, pasted on Jan 29:
#include <iostream>

int Sum(const int* arr, int size, int index = 0)
{
  if(index == size)
    return 0;
 
  return (arr[index] % 2 == 0)? arr[index] + Sum(arr, size, index + 1) : Sum(arr, size, index + 1);
}

void Replace(int* arr, int size, int n, int index = 0)
{
  if(index == size)
    return;

  if(arr[index] < 0)
    arr[index] = n;

  Replace(arr, size, n, index + 1);
}

void Output(const int* arr, int size, int index = 0)
{
  if(index == size)
    return;

  std::cout << arr[index] << " ";

  Output(arr, size, index + 1);
}

int main()
{
  int       arr[] = {1, 2, -3, 4, 5, 6};
  const int size  = sizeof(arr) / sizeof(arr[0]);

  int sum = Sum(arr, size);
  
  Replace(arr, size, sum);

  Output(arr, size);

  return 0; 
}


Output:
1
1 2 12 4 5 6 


Create a new paste based on this one


Comments: