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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jan 13, 2015, 6:17 AM EST
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