Page 1 of 2
Beginners programming help
Posted: Tue Jul 26, 2011 3:53 pm
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.
Re: Begginers programming help
Posted: Tue Jul 26, 2011 4:17 pm
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.
Re: Begginers programming help
Posted: Tue Jul 26, 2011 6:51 pm
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
Re: Begginers programming help
Posted: Tue Jul 26, 2011 7:10 pm
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";
}
Re: Begginers programming help
Posted: Tue Jul 26, 2011 7:43 pm
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
Re: Begginers programming help
Posted: Tue Jul 26, 2011 11:33 pm
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";
}
Re: Begginers programming help
Posted: Tue Jul 26, 2011 11:40 pm
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
Re: Begginers programming help
Posted: Wed Jul 27, 2011 12:39 am
by Zaid
I posted exactly what he posted but I deleted it once I saw that you were trying to use substrings
Re: Begginers programming help
Posted: Wed Jul 27, 2011 4:55 am
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.
Re: Begginers programming help
Posted: Thu Jul 28, 2011 4:10 pm
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;
}
Re: Begginers programming help
Posted: Thu Jul 28, 2011 4:15 pm
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.
Re: Begginers programming help
Posted: Thu Jul 28, 2011 4:37 pm
by bumlove
JacksonCougar wrote:..... Keep in mind....
Z-O-O-O-O-M
============>
,
(way over my head atm but it's here for when I do get there so thanks)
Re: Begginers programming help
Posted: Thu Jul 28, 2011 6:13 pm
by troymac1ure
bumlove wrote:JacksonCougar wrote:..... Keep in mind....
============>
,
(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.
Re: Begginers programming help
Posted: Thu Jul 28, 2011 6:26 pm
by bumlove
troymac1ure wrote:......
s-w-o-o-s-h
============>,
( just at head height now thanks)
Re: Begginers programming help
Posted: Thu Jul 28, 2011 10:48 pm
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.
Re: Beginners programming help
Posted: Thu Oct 13, 2011 11:49 pm
by Zaid
If you are still learning C# I would be more than happy to help you out
Re: Beginners programming help
Posted: Tue Jan 31, 2012 10:53 am
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?
Re: Beginners programming help
Posted: Tue Jan 31, 2012 2:43 pm
by XZodia
links to the tutorials would be helpful...
Re: Beginners programming help
Posted: Thu Feb 02, 2012 4:04 pm
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
Re: Beginners programming help
Posted: Fri Feb 03, 2012 1:15 am
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...
Re: Beginners programming help
Posted: Mon Feb 06, 2012 10:22 am
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.
Re: Beginners programming help
Posted: Mon Feb 06, 2012 1:49 pm
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?
Re: Beginners programming help
Posted: Mon Feb 06, 2012 2:29 pm
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
Re: Beginners programming help
Posted: Mon Feb 06, 2012 4:58 pm
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)
Re: Beginners programming help
Posted: Tue Feb 07, 2012 9:03 pm
by Grimdoomer
Could mean a couple different things. In this case it's used to make each tag id unique.