C# help

Calling all nerds! Talk tech in this forum.

Moderator: Boni

User avatar
samueeL
Finnish Flash
Posts: 3755
Joined: Sun May 27, 2007 10:06 pm
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

C# help

Post 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;
        }
   }
Last edited by samueeL on Tue Dec 20, 2011 6:48 pm, edited 2 times in total.
User avatar
Jake
Administrator
Administrator
Posts: 3196
Joined: Wed Jun 27, 2007 12:31 pm
First name: Jake :O
Age: 25
Gender:
Last.fm Username: Boneloner
Twitter: broughjc
Location: Portsmouth, England
Contact:

Re: C# help

Post by Jake »

1st thing's 1st, there's a stray semicolon on line 12.

Rest is soon to come.
Image
<3 Sumfan


Donate to TNS:
[donate][/donate]
User avatar
samueeL
Finnish Flash
Posts: 3755
Joined: Sun May 27, 2007 10:06 pm
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Post 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
User avatar
Jake
Administrator
Administrator
Posts: 3196
Joined: Wed Jun 27, 2007 12:31 pm
First name: Jake :O
Age: 25
Gender:
Last.fm Username: Boneloner
Twitter: broughjc
Location: Portsmouth, England
Contact:

Re: C# help

Post 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
Image
<3 Sumfan


Donate to TNS:
[donate][/donate]
User avatar
samueeL
Finnish Flash
Posts: 3755
Joined: Sun May 27, 2007 10:06 pm
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Post 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
Last edited by samueeL on Tue Dec 20, 2011 7:29 pm, edited 1 time in total.
SickofEveryone
Chuck
Posts: 2224
Joined: Wed Feb 16, 2011 11:02 pm
First name: Al
Age: 17
Gender:
n00b: Myself
Instrument 1: Guitar
Instrument 2: Drums and Harmonica
Instrument 3: Bass and Piano
Location: USA

Re: C# help

Post by SickofEveryone »

I thought this was going to be about C#, the chord.
Image
User avatar
Jables
Chuck
Posts: 2010
Joined: Mon Jul 12, 2010 6:42 pm
First name: Jables
Age: 21
Gender:
AKA: J.B, Jabables, Vanilla Bear
360 Gamer Tag: The Sinful Baby
Instrument 1: Bass
Instrument 2: Guitar
Instrument 3: Bouzouki
Location: Pontyclun
Contact:

Re: C# help

Post by Jables »

SickofEveryone wrote:I thought this was going to be about C#, the chord.
Same :2cool4u:
Image
User avatar
samueeL
Finnish Flash
Posts: 3755
Joined: Sun May 27, 2007 10:06 pm
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Post 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;
        }
    }
}
User avatar
Boni
Administrator
Administrator
Posts: 5831
Joined: Sat May 19, 2007 12:17 pm
First name: Christopher
Age: 28
Gender:
PSN ID: BoniBoyBlue
Twitter: @boniboyblue
Location: Scotland
Contact:

Re: C# help

Post by Boni »

Hmm, seems I was a little late.
[donate][/donate]
User avatar
Jables
Chuck
Posts: 2010
Joined: Mon Jul 12, 2010 6:42 pm
First name: Jables
Age: 21
Gender:
AKA: J.B, Jabables, Vanilla Bear
360 Gamer Tag: The Sinful Baby
Instrument 1: Bass
Instrument 2: Guitar
Instrument 3: Bouzouki
Location: Pontyclun
Contact:

Re: C# help

Post by Jables »

I could watch that static thingy go all day :2cool4u:
Image
User avatar
samueeL
Finnish Flash
Posts: 3755
Joined: Sun May 27, 2007 10:06 pm
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Post 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.
User avatar
Jake
Administrator
Administrator
Posts: 3196
Joined: Wed Jun 27, 2007 12:31 pm
First name: Jake :O
Age: 25
Gender:
Last.fm Username: Boneloner
Twitter: broughjc
Location: Portsmouth, England
Contact:

Re: C# help

Post by Jake »

You just need to use a loop? to do anything you want?

And you can't add a 3rd array?
Image
<3 Sumfan


Donate to TNS:
[donate][/donate]
User avatar
samueeL
Finnish Flash
Posts: 3755
Joined: Sun May 27, 2007 10:06 pm
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Post 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;
        }   

    }
}
User avatar
Jake
Administrator
Administrator
Posts: 3196
Joined: Wed Jun 27, 2007 12:31 pm
First name: Jake :O
Age: 25
Gender:
Last.fm Username: Boneloner
Twitter: broughjc
Location: Portsmouth, England
Contact:

Re: C# help

Post 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]);
}
Image
<3 Sumfan


Donate to TNS:
[donate][/donate]
User avatar
samueeL
Finnish Flash
Posts: 3755
Joined: Sun May 27, 2007 10:06 pm
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Post 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!
User avatar
Jake
Administrator
Administrator
Posts: 3196
Joined: Wed Jun 27, 2007 12:31 pm
First name: Jake :O
Age: 25
Gender:
Last.fm Username: Boneloner
Twitter: broughjc
Location: Portsmouth, England
Contact:

Re: C# help

Post 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
Image
<3 Sumfan


Donate to TNS:
[donate][/donate]
User avatar
samueeL
Finnish Flash
Posts: 3755
Joined: Sun May 27, 2007 10:06 pm
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Post 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.
User avatar
Jake
Administrator
Administrator
Posts: 3196
Joined: Wed Jun 27, 2007 12:31 pm
First name: Jake :O
Age: 25
Gender:
Last.fm Username: Boneloner
Twitter: broughjc
Location: Portsmouth, England
Contact:

Re: C# help

Post 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
Image
<3 Sumfan


Donate to TNS:
[donate][/donate]
User avatar
samueeL
Finnish Flash
Posts: 3755
Joined: Sun May 27, 2007 10:06 pm
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Post 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?
User avatar
Jake
Administrator
Administrator
Posts: 3196
Joined: Wed Jun 27, 2007 12:31 pm
First name: Jake :O
Age: 25
Gender:
Last.fm Username: Boneloner
Twitter: broughjc
Location: Portsmouth, England
Contact:

Re: C# help

Post 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
Image
<3 Sumfan


Donate to TNS:
[donate][/donate]
Post Reply