Page 1 of 2
C# help
Posted: Tue Dec 20, 2011 6:21 pm
by samueeL
Alright, I know I suck but bare with me and plz give me quick answer so I can chill on my vacation... :D
In the main program I need to make two arrays and put some numbers in them like I did. Alright so the problem is with subprogram: it's supposed to return
array with those numbers I used in the main program arrays. (in this case 1, 2, 3 & 5) How do I do it?
So:
Code: Select all
public class TaulukotYhteen
{
static void Main(string[] args)
{
int[] t1, t2;
t1 = new int[2] { 1, 2 };
t2 = new int[2] { 3, 5 };
Leikkaa(t1,t2);
}
public static int Leikkaa(int[] t1, int[] t2)
{
int[] t3 = new int[t1 + t2]; ?!?!?!
x
x
return t3;
}
}
Re: C# help
Posted: Tue Dec 20, 2011 6:42 pm
by Jake
1st thing's 1st, there's a stray semicolon on line 12.
Rest is soon to come.
Re: C# help
Posted: Tue Dec 20, 2011 6:46 pm
by samueeL
Oups... Didn't mean to put it there.
Alright so I made int[] t3 to subprogram so now Jake, give me your helping hand. How do I combine t1 and t2? <3
Re: C# help
Posted: Tue Dec 20, 2011 6:55 pm
by Jake
samueeL wrote:Oups... Didn't mean to put it there.
Alright so I made int[] t3 to subprogram so now Jake, give me your helping hand. How do I combine t1 and t2? <3
Something like...:
Code: Select all
var list = new List<int>();
list.AddRange(t1);
list.AddRange(t2);
int[] t3 = list.ToArray();
?
One of many ways :P
Re: C# help
Posted: Tue Dec 20, 2011 7:18 pm
by samueeL
Thanks, that helped a lot! Any idea how I can print that info with Console.WriteLine?
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
public class TaulukotYhteen
{
/// <summary>
/// Pääohjelma, jossa alustetaan kaksi taulukkoa kokonaisluvuilla
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
int[] t1, t2;
t1 = new int[2] { 1, 2 };
t2 = new int[2] { 3, 5 };
int[] t3 = Leikkaus(t1, t2);
Console.WriteLine(t3);
Console.ReadKey();
}
/// <summary>
/// Aliohjelma joka yhdistää taulukoiden t1 ja t2 alkiot samaan taulukkoon t3 ja palauttaa sen
/// </summary>
/// <param name="t1"></param>
/// <param name="t2"></param>
/// <returns></returns>
public static int[] Leikkaus(int[] t1, int[] t2)
{
var list = new List<int>();
list.AddRange(t1);
list.AddRange(t2);
int[] t3 = list.ToArray();
return t3;
}
}
}
This gives me blank :e
Re: C# help
Posted: Tue Dec 20, 2011 7:28 pm
by SickofEveryone
I thought this was going to be about C#, the chord.
Re: C# help
Posted: Tue Dec 20, 2011 8:05 pm
by Jables
SickofEveryone wrote:I thought this was going to be about C#, the chord.
Same
Re: C# help
Posted: Tue Dec 20, 2011 8:12 pm
by samueeL
Ah, I made it this way finally, if anyone is interested. Thanks Jake!
Code: Select all
using System;
public class TaulukotYhteen
{
static void Main(string[] args)
{
int[] t1, t2;
t1 = new int[]{ 1, 2 };
t2 = new int[] { 3, 5 };
Leikkaus(t1, t2);
}
public static int[] Leikkaus(int[] t1, int[] t2)
{
int[] t3 = new int[t1.Length + t2.Length];
Array.Copy(t1, t3, t1.Length);
Array.Copy(t2, t3, t2.Length);
return t3;
}
}
}
Re: C# help
Posted: Tue Dec 20, 2011 11:34 pm
by Boni
Hmm, seems I was a little late.
Re: C# help
Posted: Tue Dec 20, 2011 11:41 pm
by Jables
I could watch that static thingy go all day
Re: C# help
Posted: Wed Dec 21, 2011 12:26 am
by samueeL
You're not late guys, my answer wasn't correct. I was supposed to "cut" the lists instead of copying and use loop in the subprogram (for example for/while etc.) Can you guys help out? :e Main program can basically only have those arrays or you can use lists too.
Re: C# help
Posted: Wed Dec 21, 2011 12:39 am
by Jake
You just need to use a loop? to do anything you want?
And you can't add a 3rd array?
Re: C# help
Posted: Wed Dec 21, 2011 12:57 am
by samueeL
Jake wrote:You just need to use a loop? to do anything you want?
And you can't add a 3rd array?
I need to use loop to add the numbers of those two main program arrays together. I gotta do it with loop.
Main program can only have 2 arrays and the subprogram returns one array that has the numbers from two arrays of the main program.
So Main program = 2 arrays/lists
Subprogram = loop + array/list
Here's example I did using lists:
Code: Select all
using System;
public class ListsTogether
{
/// <summary>
/// In the main program we initialize two lists and add numbers into them
/// </summary>
static void Main(string[] args)
{
List<int> lista1 = new List<int>();
list1.Add(2);
list1.Add(4);
list1.Add(6);
list1.Add(8);
List<int> list2 = new List<int>();
list2.Add(1);
list2.Add(3);
list2.Add(5);
list2.Add(7);
Cut(lista1, lista2); ?????????
}
/// <summary>
/// Subprogram that adds list1 and list2 together with a loop and puts them into list3 and returns that
/// </summary>
public static List<int> Cut(List<int> list1, List<int> list2)
{
//LOOP(S) THAT ADDS LIST1 AND LIST2 INTO LIST3 HERE
return list3;
}
}
}
Re: C# help
Posted: Wed Dec 21, 2011 1:19 am
by Jake
So you want... Something like this?
Code: Select all
for (int i = 0; i < list1.Count; i++)
{
list3.add(list1[i]);
}
for (int i = 0; i < list2.Count; i++)
{
list3.add(list2[i]);
}
Re: C# help
Posted: Wed Dec 21, 2011 1:26 am
by samueeL
Jake wrote:So you want... Something like this?
Code: Select all
for (int i = 0; i < list1.Count; i++)
{
list3.add(list1[i]);
}
for (int i = 0; i < list2.Count; i++)
{
list3.add(list2[i]);
}
Ahh thank you, it does indeed seem to work! So this should be okay for my teacher... :D If this passes, I'll blow you!
Re: C# help
Posted: Wed Dec 21, 2011 1:28 am
by Jake
samueeL wrote:Jake wrote:So you want... Something like this?
Code: Select all
for (int i = 0; i < list1.Count; i++)
{
list3.add(list1[i]);
}
for (int i = 0; i < list2.Count; i++)
{
list3.add(list2[i]);
}
Ahh thank you, it does indeed seem to work! So this should be okay for my teacher... :D If this passes, I'll blow you!
wtf are you studying and how did you manage to get this far avoiding loops? haha :P
Re: C# help
Posted: Wed Dec 21, 2011 1:32 am
by samueeL
Jake wrote:samueeL wrote:Jake wrote:So you want... Something like this?
Code: Select all
for (int i = 0; i < list1.Count; i++)
{
list3.add(list1[i]);
}
for (int i = 0; i < list2.Count; i++)
{
list3.add(list2[i]);
}
Ahh thank you, it does indeed seem to work! So this should be okay for my teacher... :D If this passes, I'll blow you!
wtf are you studying and how did you manage to get this far avoiding loops? haha :P
Studying pedagogics and computer stuff at university and believe me, avoiding them has been hard but I've managed :D So basically IT teacher... So I don't REALLY need to know this stuff, I just need to pass courses! And loops are pain in the ass.
Re: C# help
Posted: Wed Dec 21, 2011 1:53 am
by Jake
samueeL wrote:Jake wrote:samueeL wrote:Jake wrote:So you want... Something like this?
Code: Select all
for (int i = 0; i < list1.Count; i++)
{
list3.add(list1[i]);
}
for (int i = 0; i < list2.Count; i++)
{
list3.add(list2[i]);
}
Ahh thank you, it does indeed seem to work! So this should be okay for my teacher... :D If this passes, I'll blow you!
wtf are you studying and how did you manage to get this far avoiding loops? haha :P
Studying pedagogics and computer stuff at university and believe me, avoiding them has been hard but I've managed :D So basically IT teacher... So I don't REALLY need to know this stuff, I just need to pass courses! And loops are pain in the ass.
Learning something like VB first would have helped :P
Re: C# help
Posted: Wed Dec 21, 2011 2:10 am
by samueeL
Jake wrote:samueeL wrote:Jake wrote:samueeL wrote:Jake wrote:So you want... Something like this?
Code: Select all
for (int i = 0; i < list1.Count; i++)
{
list3.add(list1[i]);
}
for (int i = 0; i < list2.Count; i++)
{
list3.add(list2[i]);
}
Ahh thank you, it does indeed seem to work! So this should be okay for my teacher... :D If this passes, I'll blow you!
wtf are you studying and how did you manage to get this far avoiding loops? haha :P
Studying pedagogics and computer stuff at university and believe me, avoiding them has been hard but I've managed :D So basically IT teacher... So I don't REALLY need to know this stuff, I just need to pass courses! And loops are pain in the ass.
Learning something like VB first would have helped :P
Indeed... But slowly getting hang of things.
Is there a command that deletes those list1 and list2 after using them in loop?
Re: C# help
Posted: Wed Dec 21, 2011 2:15 am
by Jake
samueeL wrote:Jake wrote:samueeL wrote:Jake wrote:samueeL wrote:Jake wrote:So you want... Something like this?
Code: Select all
for (int i = 0; i < list1.Count; i++)
{
list3.add(list1[i]);
}
for (int i = 0; i < list2.Count; i++)
{
list3.add(list2[i]);
}
Ahh thank you, it does indeed seem to work! So this should be okay for my teacher... :D If this passes, I'll blow you!
wtf are you studying and how did you manage to get this far avoiding loops? haha :P
Studying pedagogics and computer stuff at university and believe me, avoiding them has been hard but I've managed :D So basically IT teacher... So I don't REALLY need to know this stuff, I just need to pass courses! And loops are pain in the ass.
Learning something like VB first would have helped :P
Indeed... But slowly getting hang of things.
Is there a command that deletes those list1 and list2 after using them in loop?
http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx
*edit* Short answer is: You do not need to. As soon as it goes out of scope, the garbage collector will eat it. .NET is good at that :P