| erock 88 (8) | |||||
I created a jumble word game, well I guess I should say I copied it from a tutorial in a book and then for practice I added a point tracker. It deducts points anytime you ask for a hint or get the word wrong. I wanted to see if this was a decent way to carry out this action in my program. The code is below:
I read over the code just like I do with all the tutorials I read just to ensure that I understand exactly what is going on. I admit that there is a section that my understanding is a little shacky on. Any help would be awazing! I'll explain my troubles below: On the section of code with the comment 'jumble the word' I am not 100 percent sure as to what is going on or how it is occuring The code is :
I understand the core parts like how each index is being defined as a random value based on time and all that. The problem is with the last two lines of code. From my understading it appeats that jumble[index1] is being sext equal to jumbleindex 2 which is clear but then jumbleindex2 is being redefined as what was set for the char value for the 1st index. Let me be specific with my question by using pseudocode say we have a = C b = A c = T storedindex = C let see we want to print the word cat to the console, this is pretty straight forward. Now lets say we want to scarmble the words C and A like a = b b = storedindex //recall sotredindex = c so now when we run the console program again we get cout << a << b << c ACT I do not understant why this works. To me it seems that when a is set = b it will then see that b = storeindex which = C. So to me actually CCT would be displayed to the console. Anlyzing the code that I posted should give you an idea(hopefully) as to the question I am asking and all on all why the section of the code works the way it does. Any help would be amazing! I know, I'm probaly too stupid to be a programmer haha thanks | |||||
|
Last edited on
|
|||||
| erock 88 (8) | |
|
So I believe I have figured out the answer to the second part of the question by thinking about how the programming was working all on my ownsome. If anyone would like to weigh in and verify my accuracy it would be appreciated. I believe the way variables work is they follow the assignment of the most recent but previouse assignment. So by calling a it looks and sees that it has been assigned to b but follows the assignment of the most recent b. When I call b I get the newest assignment of b which is the last line before the cout line and it uses this assigned b value which was stored as c. I have terrible grammer good day | |
|
|
|
| fun2code (1227) | |||||
The code in question, lines 7,8,9 above:
results in the values of jumble[index1] and jumble[index2] being swapped. The variable 'temp' is serving as "auxiliary memory" for the swap (to temporarily save a value). This would be similar:
This should leave a='A', b='C' (and c='T' still). ie, the values of a and b were swapped. So, cout << a << b << c; should give "ACT"
| |||||
|
Last edited on
|
|||||
| erock 88 (8) | |
|
Oh okay! This makes sense. This way the program does not lose any values or repeart char's that are not suppose to be repeated? The word stays the word but still gets jumbled. Thanks! | |
|
Last edited on
|
|