considering interger list
i want recursive function for the list adding all elements except the last element
example
a[1,2,3,4,5]
output
a[10,5]
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jan 13, 2015, 11:17 AM
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var a = new List
RecursiveAdd(a);
// check it worked
foreach(int i in a) Console.Write("{0} ", i);
Console.ReadKey();
}
static void RecursiveAdd(List
{
if(a == null || a.Count <= 1) throw new ArgumentException("List can't be null amd must have more than 1 element");
if(a.Count == 2) return;
a[0] += a[a.Count - 2];
a.RemoveAt(a.Count - 2);
RecursiveAdd(a);
}
}
The output should be:
10 5