C# help

Calling all nerds! Talk tech in this forum.

Moderator: Boni Boy Blue

C# help

Postby samueeL on Tue Dec 20, 2011 6:21 pm

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

samueeL
Finnish Flash
 
Posts: 3638
Joined: Sun May 27, 2007 10:06 pm
Location: Jyvaskyla, Finland
First name: Samuel
Age: 21
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Postby Jake on Tue Dec 20, 2011 6:42 pm

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

Rest is soon to come.
Image
<3 Sumfan


Donate to TNS:
User avatar

Jake
Administrator
 
Posts: 3136
Joined: Wed Jun 27, 2007 12:31 pm
Location: Portsmouth, England
First name: Jake :O
Age: 20
Gender:
Last.fm Username: Boneloner

Re: C# help

Postby samueeL on Tue Dec 20, 2011 6:46 pm

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

samueeL
Finnish Flash
 
Posts: 3638
Joined: Sun May 27, 2007 10:06 pm
Location: Jyvaskyla, Finland
First name: Samuel
Age: 21
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Postby Jake on Tue Dec 20, 2011 6:55 pm

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:
User avatar

Jake
Administrator
 
Posts: 3136
Joined: Wed Jun 27, 2007 12:31 pm
Location: Portsmouth, England
First name: Jake :O
Age: 20
Gender:
Last.fm Username: Boneloner

Re: C# help

Postby samueeL on Tue Dec 20, 2011 7:18 pm

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.
User avatar

samueeL
Finnish Flash
 
Posts: 3638
Joined: Sun May 27, 2007 10:06 pm
Location: Jyvaskyla, Finland
First name: Samuel
Age: 21
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Postby SickofEveryone on Tue Dec 20, 2011 7:28 pm

I thought this was going to be about C#, the chord.
Image
User avatar

SickofEveryone
Chuck
 
Posts: 2224
Joined: Wed Feb 16, 2011 11:02 pm
Location: USA
First name: Al
Age: 17
Gender:
n00b: Myself
Instrument 1: Guitar
Instrument 2: Drums and Harmonica
Instrument 3: Bass and Piano

Re: C# help

Postby Jables on Tue Dec 20, 2011 8:05 pm

SickofEveryone wrote:I thought this was going to be about C#, the chord.

Same :2cool4u:
In Malky We Trust <0> \0/ <0>

I'm Not Dangerous, I'm Just Weird
Image
I want to give Brian Fallon a hug.
User avatar

Jables
Does This Look Infected?
 
Posts: 1983
Joined: Mon Jul 12, 2010 6:42 pm
Location: Pontyclun, Wales..... Hold my sheep
First name: Jables
Age: 17
Gender:
n00b: cladam41
AKA: Jabables, Vanilla Bear
360 Gamer Tag: The Sinful Baby
Instrument 1: Bass
Instrument 2: Deep Fried Trumpet
Instrument 3: Guitar

Re: C# help

Postby samueeL on Tue Dec 20, 2011 8:12 pm

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

samueeL
Finnish Flash
 
Posts: 3638
Joined: Sun May 27, 2007 10:06 pm
Location: Jyvaskyla, Finland
First name: Samuel
Age: 21
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Postby Boni Boy Blue on Tue Dec 20, 2011 11:34 pm

Hmm, seems I was a little late.
User avatar

Boni Boy Blue
Administrator
 
Posts: 5778
Joined: Sat May 19, 2007 12:17 pm
Location: Scotland
First name: Christopher
Age: 22
Gender:
PSN ID: boniboyblue
Last.fm Username: BoniBoyBlue
Twitter: @BoniBoyBlue

Re: C# help

Postby Jables on Tue Dec 20, 2011 11:41 pm

I could watch that static thingy go all day :2cool4u:
In Malky We Trust <0> \0/ <0>

I'm Not Dangerous, I'm Just Weird
Image
I want to give Brian Fallon a hug.
User avatar

Jables
Does This Look Infected?
 
Posts: 1983
Joined: Mon Jul 12, 2010 6:42 pm
Location: Pontyclun, Wales..... Hold my sheep
First name: Jables
Age: 17
Gender:
n00b: cladam41
AKA: Jabables, Vanilla Bear
360 Gamer Tag: The Sinful Baby
Instrument 1: Bass
Instrument 2: Deep Fried Trumpet
Instrument 3: Guitar

Re: C# help

Postby samueeL on Wed Dec 21, 2011 12:26 am

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

samueeL
Finnish Flash
 
Posts: 3638
Joined: Sun May 27, 2007 10:06 pm
Location: Jyvaskyla, Finland
First name: Samuel
Age: 21
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Postby Jake on Wed Dec 21, 2011 12:39 am

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:
User avatar

Jake
Administrator
 
Posts: 3136
Joined: Wed Jun 27, 2007 12:31 pm
Location: Portsmouth, England
First name: Jake :O
Age: 20
Gender:
Last.fm Username: Boneloner

Re: C# help

Postby samueeL on Wed Dec 21, 2011 12:57 am

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

samueeL
Finnish Flash
 
Posts: 3638
Joined: Sun May 27, 2007 10:06 pm
Location: Jyvaskyla, Finland
First name: Samuel
Age: 21
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Postby Jake on Wed Dec 21, 2011 1:19 am

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:
User avatar

Jake
Administrator
 
Posts: 3136
Joined: Wed Jun 27, 2007 12:31 pm
Location: Portsmouth, England
First name: Jake :O
Age: 20
Gender:
Last.fm Username: Boneloner

Re: C# help

Postby samueeL on Wed Dec 21, 2011 1:26 am

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

samueeL
Finnish Flash
 
Posts: 3638
Joined: Sun May 27, 2007 10:06 pm
Location: Jyvaskyla, Finland
First name: Samuel
Age: 21
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Postby Jake on Wed Dec 21, 2011 1:28 am

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:
User avatar

Jake
Administrator
 
Posts: 3136
Joined: Wed Jun 27, 2007 12:31 pm
Location: Portsmouth, England
First name: Jake :O
Age: 20
Gender:
Last.fm Username: Boneloner

Re: C# help

Postby samueeL on Wed Dec 21, 2011 1:32 am

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

samueeL
Finnish Flash
 
Posts: 3638
Joined: Sun May 27, 2007 10:06 pm
Location: Jyvaskyla, Finland
First name: Samuel
Age: 21
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Postby Jake on Wed Dec 21, 2011 1:53 am

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:
User avatar

Jake
Administrator
 
Posts: 3136
Joined: Wed Jun 27, 2007 12:31 pm
Location: Portsmouth, England
First name: Jake :O
Age: 20
Gender:
Last.fm Username: Boneloner

Re: C# help

Postby samueeL on Wed Dec 21, 2011 2:10 am

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

samueeL
Finnish Flash
 
Posts: 3638
Joined: Sun May 27, 2007 10:06 pm
Location: Jyvaskyla, Finland
First name: Samuel
Age: 21
Gender:
PSN ID: samueel41
Instrument 1: Guitar
Instrument 2: Piano
Instrument 3: Drums

Re: C# help

Postby Jake on Wed Dec 21, 2011 2:15 am

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:
User avatar

Jake
Administrator
 
Posts: 3136
Joined: Wed Jun 27, 2007 12:31 pm
Location: Portsmouth, England
First name: Jake :O
Age: 20
Gender:
Last.fm Username: Boneloner

Next

Return to Tech Talk

Who is online

Users browsing this forum: No registered users and 1 guest