A
Arash_89
Guest
Hello,
I have overwrite problem in listPrint.Add(list);
For example: my input is : 2, 4, 6 and 8
I expected listPrint have 3 elements .
Element one: 6, 10, 14
Element two: 16, 24
Element Three: 40
But listPrint have two element. 40 and 40
How can I fix this problem?
Thanks
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharp
{
class Program
{
static List<List<int>> listPrint = new List<List<int>>();
static void Recursively(List<int> list)
{
if (list.Count == 1)
{
Console.WriteLine(list[0]);
return;
}
for (int i = 0; i < list.Count - 1; i++)
{
list = list + list[i + 1];
}
list.RemoveAt(list.Count - 1);
if (list.Count != 1)
listPrint.Add(list); // OVERWRTIE PROBLEM
//Print(list);
Recursively(list);
}
static void Print(List<int> list)
{
for (int i = 0; i < list.Count; i++)
{
Console.Write(list + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
List<int> mylist = new List<int>();
int i = 0;
int numberOfElements;
Console.WriteLine("Number of Elements??");
numberOfElements = Convert.ToInt32(Console.ReadLine());
while(i < numberOfElements)
{
Console.WriteLine("Number? ");
mylist.Add(Convert.ToInt32(Console.ReadLine()));
i++;
}
Console.WriteLine();
Recursively(mylist);
}
}
}
Continue reading...
I have overwrite problem in listPrint.Add(list);
For example: my input is : 2, 4, 6 and 8
I expected listPrint have 3 elements .
Element one: 6, 10, 14
Element two: 16, 24
Element Three: 40
But listPrint have two element. 40 and 40
How can I fix this problem?
Thanks
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharp
{
class Program
{
static List<List<int>> listPrint = new List<List<int>>();
static void Recursively(List<int> list)
{
if (list.Count == 1)
{
Console.WriteLine(list[0]);
return;
}
for (int i = 0; i < list.Count - 1; i++)
{
list = list + list[i + 1];
}
list.RemoveAt(list.Count - 1);
if (list.Count != 1)
listPrint.Add(list); // OVERWRTIE PROBLEM
//Print(list);
Recursively(list);
}
static void Print(List<int> list)
{
for (int i = 0; i < list.Count; i++)
{
Console.Write(list + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
List<int> mylist = new List<int>();
int i = 0;
int numberOfElements;
Console.WriteLine("Number of Elements??");
numberOfElements = Convert.ToInt32(Console.ReadLine());
while(i < numberOfElements)
{
Console.WriteLine("Number? ");
mylist.Add(Convert.ToInt32(Console.ReadLine()));
i++;
}
Console.WriteLine();
Recursively(mylist);
}
}
}
Continue reading...