Beginners programming help

Discuss anything programming related.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Beginners programming help

Post by bumlove »

I'm working thru some online lessons and I've run aground as it were,
This program should tell me how many times the letter "g" appears in the word "debugging"
but alas the programs doesn't move from the letter "d" so I always get an letter count as zero,

Code: Select all

        private void button1_Click(object sender, EventArgs e)
        {
            int LetterCount = 0;
            string strText = "Debugging";
            string Letter;
            
            for (int i = 0; i < strText.Length; i++)
            {
                Letter = strText.Substring(0, 1);

                if (Letter == "g")
                {
                    LetterCount++;
                }
            }

            textBox1.Text = "g appears " + LetterCount + " times";
        }
my tutorial says I should be thinking Loops and substrings.
Last edited by bumlove on Thu Jul 28, 2011 9:57 pm, edited 1 time in total.
User avatar
JacksonCougar
Huurcat
Posts: 2460
Joined: Thu Dec 06, 2007 11:30 pm
Location: Somewhere in Canada

Re: Begginers programming help

Post by JacksonCougar »

you could do this:

Code: Select all

string needle = "g";
string haystack = "Glorus glowered as Gloria's green goblins gagged Glorus";
int count = 0;
for(int i = 0; i < haystack.Length - needle.length; i++)
{
          if(haystack.Substring(i, needle.length) == needle)
          {
                    count++;
          }
}
Your problem is in the Substring method call: you always ask for the same substring
.Substring(0, 1); //you need to change the index, and the length should be the length of whatever string you want to find.
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

Re: Begginers programming help

Post by Zaid »

JacksonCougar wrote:you could do this:

Code: Select all

string needle = "g";
string haystack = "Glorus glowered as Gloria's green goblins gagged Glorus";
int count = 0;
for(int i = 0; i < haystack.Length - needle.length; i++)
{
          if(haystack.Substring(i, needle.length) == needle)
          {
                    count++;
          }
}
Your problem is in the Substring method call: you always ask for the same substring
.Substring(0, 1); //you need to change the index, and the length should be the length of whatever string you want to find.
Good answer my young padawan
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Begginers programming help

Post by bumlove »

Thanks it now works, but I don't understand
JacksonCougar wrote: Your problem is in the Substring method call: you always ask for the same substring
.Substring(0, 1); //you need to change the index, and the length should be the length of whatever string you want to find.
the number in bold (0) is the start point (letter "D") the second number is how many characters are used at once, so what did you mean?


Fixed up

Code: Select all

        private void button1_Click(object sender, EventArgs e)
        {
            int LetterCount = 0;
            string strText = "Debugging";
            string Letter ="g";
            
            for (int i =0; i <= strText.Length - Letter.Length;  i++)
            {                
                if (strText.Substring(i, Letter.Length) == Letter)
                {
                    LetterCount++;
                }
            }
            textBox1.Text = "g appears " + LetterCount + " times";
        }
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

Re: Begginers programming help

Post by Zaid »

bumlove wrote:Thanks it now works, but I don't understand
JacksonCougar wrote: Your problem is in the Substring method call: you always ask for the same substring
.Substring(0, 1); //you need to change the index, and the length should be the length of whatever string you want to find.
the number in bold (0) is the start point (letter "D") the second number is how many characters are used at once, so what did you mean?
The way a substring works is Substring(Starting Letter Index,Number Of Letters);
You had Substring(0,1);
That was always going to be 'D';
Since D is index 0;
string strText = "Debugging";
str.Index(0) = 'D';
str.Index(1) = 'e';
str.Index(2) = 'b';
...etc
So for example
str.Substring(2,3);
Start Index is 2 so that would be 'b';
The length is 3 so that would be 3 letters long so the substring would equal ("bug")

So Jackson was saying that through every loop you check if Substring(0,1) equals 'g', and you need to change the start index to the next letter
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Begginers programming help

Post by OwnZ joO »

There's more than one way to skin a guerilla, another way to do it is use the strings indexer(I think that's what it's called?) property, basically on a string you can do something like str[index] and it will return the character at that index. But yeah Jackson pointed you in the right direction also.

Code: Select all

        private void button1_Click(object sender, EventArgs e)
        {
            int LetterCount = 0;
            string strText = "Debugging";
            char Letter ='g';
            
            for (int i =0; i < strText.Length;  i++)
            {                
                if(strtxt[i] == g)
                   LetterCount++;
            }
            textBox1.Text = "g appears " + LetterCount + " times";
        }
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Begginers programming help

Post by bumlove »

Zaid wrote:So Jackson was saying that through every loop you check if Substring(0,1) equals 'g', and you need to change the start index to the next letter
yep I get what the numbers represent but I think the way jackson solved it was perhaps deeper than the lessons I've had before, is there not a simpler way to move the index along per loop?

*edit thanks Ownz jo0, you got there while I was posting.

Now that I have it working I can take it apart and try put different things

Anywho, on to arrays.

Thanks all
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

Re: Begginers programming help

Post by Zaid »

I posted exactly what he posted but I deleted it once I saw that you were trying to use substrings
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Begginers programming help

Post by OwnZ joO »

Keep in mind that strings are really arrays of characters when you're learning about arrays. There's a little more to it than that, but that's what they are under the hood.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Begginers programming help

Post by bumlove »

Hi again all, I'm progressing (still slowly but a bit faster than before)
I'm now on to string manipulation but to help the learning process I'm trying to add things I already know to the new exercises I'm trying
So the exercise I was on asks to insert a designed string into another designed string at a designed index position, and to bring up 2 confirmation boxes with the old string and the new string information.
a piece of piss I'm sure you'll agree,
So I had a go at making it a runtime exercise, it works and all but would like to see what/how others think

Code: Select all

 
       private void button13_Click(object sender, EventArgs e)
        {
            string oldtext = textBox6.Text;
            string texttoadd = textBox7.Text;
            string newtext;

            int posit;
            posit = int.Parse(textBox9.Text);

            newtext = oldtext.Insert(posit, texttoadd);

            MessageBox.Show(oldtext);
            MessageBox.Show(newtext);

            textBox8.Text = newtext;
        }
User avatar
JacksonCougar
Huurcat
Posts: 2460
Joined: Thu Dec 06, 2007 11:30 pm
Location: Somewhere in Canada

Re: Begginers programming help

Post by JacksonCougar »

That looks about good. Keep in mind that strings are supposed to me immutable in C# so every method call that modifies a string actually returns a copy of that string in a new memory location, so if you find yourself doing extensive processing on a string, consider using a StringBuilder class instead.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Begginers programming help

Post by bumlove »

JacksonCougar wrote:..... Keep in mind....
Z-O-O-O-O-M
============>


, :roll:
(way over my head atm but it's here for when I do get there so thanks)
Last edited by bumlove on Thu Jul 28, 2011 10:01 pm, edited 1 time in total.
User avatar
troymac1ure
Keeper of Entity
Posts: 1282
Joined: Sat Aug 09, 2008 4:16 am
Location: British Columbia, Canada, eh
Contact:

Re: Begginers programming help

Post by troymac1ure »

bumlove wrote:
JacksonCougar wrote:..... Keep in mind....
============>


, :roll:
(way over my head atm but it's here for when I do get there so thanks)
What he's saying is that when you make a string, such as "Hello World!", it allocates 12 bytes of memory for that string. If you INSERT "New " into the string, there's no longer enough memory allocated to hold the string, so (although you don't see it) the computer allocates a new block of memory that is 16 bytes long and copies the new string to that location. Not a big deal, but if you are doing lots of these you end up with slower response, etc due to always having to allocate/deallocate memory.
I've never used StringBuilder, but I assume it allocates a large block of memory to allow for byte movements within the given block (aka no need to allocate/deallocate new memory)

For example the first time you allocate the string (0x0000 in our example) you have your Hello World!, but when you insert "New " it now allocates a new section (at 0x040 in our example again) and you now have code to do that and the orginal is still saved if using the .Insert() method as the string returned doesn't modify the input string.

Code: Select all

0000 48 65 6C 6C .. 20 .. .. .. 6C 64 21 00 00 00 00 Hello World!....
0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0040 48 65 6C 6C .. 20 .. 65 .. 20 .. .. .. 6C 64 21 Hello New World!
0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
Anyways, probably too much writing for not much, but meh.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Begginers programming help

Post by bumlove »

troymac1ure wrote:......
s-w-o-o-s-h
============>, 8-)
( just at head height now thanks)
Last edited by bumlove on Thu Jul 28, 2011 10:02 pm, edited 1 time in total.
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Begginers programming help

Post by OwnZ joO »

troymac1ure wrote: What he's saying is that when you make a string, such as "Hello World!", it allocates 12 bytes of memory for that string. If you INSERT "New " into the string, there's no longer enough memory allocated to hold the string, so (although you don't see it) the computer allocates a new block of memory that is 16 bytes long and copies the new string to that location. Not a big deal, but if you are doing lots of these you end up with slower response, etc due to always having to allocate/deallocate memory.
I've never used StringBuilder, but I assume it allocates a large block of memory to allow for byte movements within the given block (aka no need to allocate/deallocate new memory)
Here's some example code:

Code: Select all

string hello = "Hello";
string world = "World";
string helloWorld = hello;
helloWorld += " " + world;
After this code the variables point to:

Code: Select all

hello -> "Hello"
world -> "World"
helloWorld -> "Hello World"
Notice how hello was not changed even though it was pointed to by helloWorld and we changed that variable.
This is because when you use + on strings it creates a new string each time it happens(Note: The compiler translates a+= b to a = a + b)
So the helloWorld variable(that stores a pointer to a string) had a new string assigned to it that was the concatenation of "Hello", " ", and "World" and that all happened automagically by .Net without you having to worry about mashing the 3 strings together. When you use + on strings a lot though there is a lot of copying and memory allocation going on behind the scenes which can really decrease performance, especially with really big strings when you add only one character at a time.

Troy I would imagine that StringBuilder is pretty similar to something like List<char> where it would have an array that doubles in size when it needs to reallocate memory, except it has AppendLine and Append instead of Add(char) methods.
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

Re: Beginners programming help

Post by Zaid »

If you are still learning C# I would be more than happy to help you out
Last edited by Zaid on Tue Jun 09, 2015 8:18 am, edited 1 time in total.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Beginners programming help

Post by bumlove »

I've been plodding along with getting my head round C# with web pages and Grim's, Anthony's @ and Goldbl4d3's tutorials/solution tuts and I've hit the point where to learn more I need to make something.
Now I was going to try making a cube collision editor (original huh? Coll first then Mode, phmo and Bloc and finally pulling it altogether with a option for hmlt but I think I have the math logic to retain the 0.00000s that has squares looses ) within entity, but there's just a bit too much going on for me to figure out what object name is holding the meta,
so anyways I'll try to make a standalone item
just a question or 3 before I start
1 Grims H2app programing tutorial: flawed because?... not broken in to classes?
2 Anthonys tutorial/solution: why is it so slow? (loosely coded? specifics please)
3 GoldblAd3's Max/Min ROF tutorial/solution: Is this a good standard to try copy?

Other code I should look at?
User avatar
XZodia
Staff
Posts: 2208
Joined: Sun Dec 09, 2007 2:09 pm
Location: UK
Contact:

Re: Beginners programming help

Post by XZodia »

links to the tutorials would be helpful...
Image
JacksonCougar wrote:I find you usually have great ideas.
JacksonCougar wrote:Ah fuck. Why must you always be right? Why.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Beginners programming help

Post by bumlove »

I've tried to type a reply for over 30 mins and every time the libary login screws me over,

tutorials no bother any longer I can start to see the differnece between good and bad

I think I need to set up a 2d array so that I can resize plane distances, I dont have the stomach to sit any longer to explain again

ctrl + a ctrl + C submit
User avatar
troymac1ure
Keeper of Entity
Posts: 1282
Joined: Sat Aug 09, 2008 4:16 am
Location: British Columbia, Canada, eh
Contact:

Re: Beginners programming help

Post by troymac1ure »

bumlove wrote:ctrl + a ctrl + C submit
I always do this if I've written over a one sentance reply (on any website). Learned my lesson after multiple times of losing huge replies...
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Beginners programming help

Post by bumlove »

Well I'm getting into it with goldbl4de's RoF app and I'm amazed how given a few informational pointers (objectindex and the like) even I can bodge, butcher and bang together functioning code,

My proudest bit of bodgery yet was connecting bloc hlmt coll mode and phmo meta to a single page, since I still don't fully grasp whats going on in blades work, I figured my own way to get to the meta start of my chosen blocs hlmt

Something like

Code: Select all

br.position = metaStart + 56;  //this is the offset for hlmt ident within bloc
int index position = br.readInt16(); // I noticed if you read like this as opposed to int32 the numbering is nice matg = 0 etc
br.position = objecIndex + 4 + (indexposition * 16); // bodge on bodge on bodge!
// I know next to nothing about code but this still makes me feel both good and dirty
int  nextMetaStart = br.readsingle();
br.position = nextMetaStart;
as I'm new to this please forgive that it's RAF (not royal air force, but rough as f***)

so anywho I'm about to abandon blades code and start again from scratch now that I'm beginning to get why you use classes
(even though my program doesn't yet write to the map but it recalculates 26 pieces of meta from a X, Y, Z and a button click)
I'll go for a standalone version to be peer reviewed before fixing it into entity


a couple of questions now

1 If the first short of an Ident = its index position, what does the second short stand for?
2 Specifically to my program, bounding radii, for small blocs (0.45 size)
I work a radius out as (size/6) * 5 = bounding R 0.375 as it is in the game,
but for larger blocs (0.9 size) my math gives me a R of 0.75 when in game it = 0.7 (not a huge difference but I'm curious why) I know it's a different story for phmo bounding radii I haven't done the math yet.
User avatar
XZodia
Staff
Posts: 2208
Joined: Sun Dec 09, 2007 2:09 pm
Location: UK
Contact:

Re: Beginners programming help

Post by XZodia »

bumlove wrote:1 If the first short of an Ident = its index position, what does the second short stand for?
What do you mean by index position? You've sparked my interest here, investigating now...

Edit: FFS what idiot decided that the tag id should be read as a signed integer? If read as an unsigned integer, the id's go from 0 to n -_-
TagId....tag index more like...
This information could make so many things, so much faster to do...
bumlove wrote:2 Specifically to my program, bounding radii, for small blocs (0.45 size)
I work a radius out as (size/6) * 5 = bounding R 0.375 as it is in the game,
but for larger blocs (0.9 size) my math gives me a R of 0.75 when in game it = 0.7 (not a huge difference but I'm curious why) I know it's a different story for phmo bounding radii I haven't done the math yet.
Where are you getting these values from?
Image
JacksonCougar wrote:I find you usually have great ideas.
JacksonCougar wrote:Ah fuck. Why must you always be right? Why.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Beginners programming help

Post by bumlove »

XZodia wrote:: FFS what idiot decided that the tag id should be read as a signed integer? If read as an unsigned integer, the id's go from 0 to n -_-
TagId....tag index more like...
This information could make so many things, so much faster to do...
Woop I discovered something (6 years late) amazing what a fresh pair of untutored eyes can see
(infact, fuck I deserve some kind of award, I nominate Clueless yet observant)
I didn't try Uint I just tried a short and it worked (I suppose there is no chance of having more than 65000 tags)

As for the radii, bloc has one, collision>pathfinding spheres has one, this is not to do with AI but more like where the collisions centre point [like a node] is, sounds wierd I know but moving the Centre Z position means you can leave plane 0 [the base] where it is and just adjust plane1 making the math a bit easier the path finding sphere's radius I'm guessing needs to be representive of a collisions volume, or like if the radius in bloc isn't enlarged, items vanish when they should be in view, like wise phmo [different sizing rules apply], I've had things I could jump up through when approaching from below,

What with I can has squares being squiff and still leaving other entries to be manually altered I think I chose a simple yet rewarding task for my first H2 program, I still believe it should be in entity (a blue + option from bloc)

maybe I getting giddy but I'm liking this programing lark
User avatar
XZodia
Staff
Posts: 2208
Joined: Sun Dec 09, 2007 2:09 pm
Location: UK
Contact:

Re: Beginners programming help

Post by XZodia »

Yes you are getting giddy...

btw http://code.google.com/p/open-sauce/sou ... umIndex.cs

shows that it is 2 unsigned shorts, the second is a salt value (i dont know what that means either)
Image
JacksonCougar wrote:I find you usually have great ideas.
JacksonCougar wrote:Ah fuck. Why must you always be right? Why.
User avatar
Grimdoomer
Admin
Posts: 1835
Joined: Sun Dec 09, 2007 9:09 pm

Re: Beginners programming help

Post by Grimdoomer »

XZodia wrote:Yes you are getting giddy...

btw http://code.google.com/p/open-sauce/sou ... umIndex.cs

shows that it is 2 unsigned shorts, the second is a salt value (i dont know what that means either)
Could mean a couple different things. In this case it's used to make each tag id unique.
Don't snort the magic, we need it for the network.
Post Reply