How to write arrays to tab delimited text file? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have two arrays -
int[] array1 = { 1, 2};
int[] array2 = { 6, 7};
I want to write them in text file delimited by tab.
Final Result -
1 6
2 7
Please suggest how to do this?

Assuming both arrays are of the same length:
File.WriteAllLines(filename,
Enumerable.Range(0, arr1.Length)
.Select(i => string.Format("{0}\t{1}", arr1[i], arr2[i]));

You could do something like this:
public void writeFile(int[] array){
using (streamWriter sw = new StreamWriter(filename, true){
for(int i = 0; i < array.Length; i++){
sw.WriteLine(array[i] + "\t");
}
}
}
Then you would just call the write method like this:
writeFile(array1);
writeFile(array2);

Related

How to add List's all values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
of course I can use for() {}
but is there any function which can add all values in array?
if do you know, please tell me.
thank you.
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[a.Length];
a.CopyTo(b, 0);
b.ToList().ForEach(c=>{
System.Console.WriteLine(c);
});
using System.Linq;
then...
var array = new double[] { 1.1, 2.2, 3.3 };
var sum = array.Sum(value=>value);
Console.WriteLine($"Sum is {sum}");

How do increment an integer that is part of a string at each iteration of loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i have a string like this S02E01 and now i want to run it through a loop like 20 times but at each iteration i want to increment the number after 'E' so that we have for example S02E01,S02E02,S02E03 ..S0210,...S02E20. Please help me.
Its really simple by using some string functions: Substring, PadLeft or ToString
string mystring = "S02E00";
var template = mystring.Substring(0, mystring.Length - 2);//template = "S02E"
for(int i = 0;i <= 20; i++)
{
var result = template + i.ToString("00");
Console.WriteLine(result);
}
you could use PadLeft too => var result = template + i.ToString().PadLeft(2, '0');

Sort C# array while adding numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have this array and I want that every time the user inputs a number, the program immediately makes the sort. How can I do that? For example, if I input 6 and then 3, immediately takes the 3 to the first position. And then if I put 2, immediately take it to the first position and sort the others (2,3,6). Then if I put 1, takes it to the first position, sorting the others(1,2,3,6) and so on
int[] a = new int[5] ;
for (int i = 0; i < a.Length; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
As #juharr suggest you can use System.Collections.Generic.SortedSet:
static void Main(string[] args)
{
var list = new SortedSet<int>();
int count = 5;
for ( int i = 0; i < count; i++ )
list.Add(Convert.ToInt32(Console.ReadLine()));
foreach (int item in list)
Console.WriteLine(item);
Console.ReadKey();
}
add the numbers to a list and call list.sort()
List<int> list = new List<int> { 6, 3 };
list.Sort();

How to call my int array class to ad my dropdownlist [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public static int[] ddDuration()
{
int[] ddarray = new int[] { 1, 2, 3, 4, 5 };
return ddarray;
}
How to make in c# to call my int array to who any page in my DropDownList
//and I am try to show in my DropDownList
//like that some thing
Duration dm = new Duration();
//dm.ddDuration;
//dropdduration.item.AddRang(dm.ddDuration);
If I got this right, you're trying to add an array of integers as items to a DropDownList.
var collection = ddDuration();
int len = collection.length;
for(int i=0; i<len; i++){
DropDownList1.Items.Insert(i, new ListItem(collection[i].toString(), ""));
}
Insert's first parameter is the position (index) in the Items collection and the second parameter takes a ListItem; of which constructor can be build upon its content (string in this case).

Making an array ints and replace the order of number I write [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I try to do, is for example if I write five numbers 1,2,3,4,5 after this it should print 5,4,3,2,1
int[] numbers = new int[5];
for (int i = 0; i < numbers.Length ; i++)
{
Console.Write("");
numbers[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < numbers.Length; i++)
{
}
If you need just to print them in a reverse order, but you want to keep them stored as you inserted them, change the second loop like this:
for (int i=numbers.Length-1; i >= 0; i--){
Console.WriteLine(numbers[i]);
}
You can print the array with a reverse loop as in the previous answer, or you can do that in a single line:
Console.WriteLine(string.Join(",", numbers.Reverse()));

Categories

Resources