C++ help because im terrible at it :(

Page 3 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
Status
Not open for further replies.

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Just throwing this out there, but the point of the exercise, coming near the beginning of the term as it does, is likely to see if the students understand and can use nested for loops.

In that context, Tweet's solution is probably the most appropriate since if the OP started using pointer arithmetic, when pointers have likely not been covered yet, would be a little suspect. Of course, he should have discussed the solution rather than just typing it in like that.

Equally of course, he should have used code tags...everyone knows this.

While I appreciate the comment, I don't recommend using any more time than I have trying to explain it was just a homework problem. Thanks though.

I especially like being called Tweet. Haha.

And on writing it out, yeah I agree. I felt for the guy, ever been in a similar situation? I'll just make sure not to do it again as per the mod lol.
 

Patterner

Senior member
Dec 20, 2010
227
0
0
Pretty sure it wasn't in nested for loops, as they are totally unnecessary. I would say this was an assignment looking at array usage.

Err, define "totally unnecessary".

And that could be, though by this time in the term I doubt they've covered enough control structures to do this another way than with a nested loop.
 

Patterner

Senior member
Dec 20, 2010
227
0
0
While I appreciate the comment, I don't recommend using any more time than I have trying to explain it was just a homework problem. Thanks though.

I especially like being called Tweet. Haha.

And on writing it out, yeah I agree. I felt for the guy, ever been in a similar situation? I'll just make sure not to do it again as per the mod lol.

Heh, sorry for the unintended tweaking (thank you, be here all week, tip your waitress) of you there...was too lazy to scroll and see what your username was.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Heh, sorry for the unintended tweaking (thank you, be here all week, tip your waitress) of you there...was too lazy to scroll and see what your username was.

Haha! Ironically that's the nature of this thread. I'll try to make sure not be too lazy to optimize someone else's basic C++ homework assignment. May the gods forgive me.

But don't worry, I forgive you.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Err, define "totally unnecessary".

And that could be, though by this time in the term I doubt they've covered enough control structures to do this another way than with a nested loop.

It isn't needed. You don't need any special control structure to handle the situation either. So long as you understand how an array works you can do this without using 2 for loops. (I sent a PM of the code that doesn't use a nested for loop to you.)
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
It isn't needed. You don't need any special control structure to handle the situation either. So long as you understand how an array works you can do this without using 2 for loops. (I sent a PM of the code that doesn't use a nested for loop to you.)

If it's the same code that declares and initializes a variable inside a for loop that you PM'ed me by mistake, don't worry, I'll still let you turn it in.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
If it's the same code that declares and initializes a variable inside a for loop that you PM'ed me by mistake, don't worry, I'll still let you turn it in.

lol, you still can't say WHY that is bad. I've already given you a good explanation of why I use that function.

For those wondering, the code in question is this.
Code:
   for (size_t i = 0; i < sizeof(stringToSearch); ++i)
   {
      // Convert the charactors to their uppercase equivelents.
      char val = toupper(stringToSearch[i]);

Tweak doesn't like the fact that I use toupper. He thinks it is too costly and that just subtracting 32 from the character would have be much better.
 

KCfromNC

Senior member
Mar 17, 2007
208
0
76
Just throwing this out there, but the point of the exercise, coming near the beginning of the term as it does, is likely to see if the students understand and can use nested for loops.

Could also be to understand characters and their integer representation, which always gives beginners problems. In this case learning that (ch - 'a') gives values from 0 .. 25 and are an appropriate array index would be part of the lesson.

The typical double-nested loop homework is matrix math or various types of sorting. This one feels more like realizing that ASCII is just a number to a computer. Hard to tell without additional context,though.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
lol, you still can't say WHY that is bad. I've already given you a good explanation of why I use that function.

For those wondering, the code in question is this.
Code:
   for (size_t i = 0; i < sizeof(stringToSearch); ++i)
   {
      // Convert the charactors to their uppercase equivelents.
      char val = toupper(stringToSearch[i]);

Tweak doesn't like the fact that I use toupper. He thinks it is too costly and that just subtracting 32 from the character would have be much better.

Actually I didn't have an issue with TOUPPER, again you don't understand what I'm saying. I told you I wouldn't have used toupper simply because its another function, not because its slow and retarded like all the code I write.

And the problem is with it being IN the for loop. You just declared a variable N times (length of the string) for no good reason. This takes up space. Put the declaration of val outside the for loop to avoid this wasted space.

Thanks.

EDIT:

Also you spelled "equivalents" and "characters" wrong, lol.
 
Last edited:

Patterner

Senior member
Dec 20, 2010
227
0
0
It isn't needed. You don't need any special control structure to handle the situation either. So long as you understand how an array works you can do this without using 2 for loops. (I sent a PM of the code that doesn't use a nested for loop to you.)

Looks good, though the use of memset, size_t and sizeof() would likely rule it out for OP, you could do those less efficiently using things the OP probably *has* learned.

I was objecting to what sounded like a blanket statement that nested loops are totally unnecessary.
 

Patterner

Senior member
Dec 20, 2010
227
0
0
Could also be to understand characters and their integer representation, which always gives beginners problems. In this case learning that (ch - 'a') gives values from 0 .. 25 and are an appropriate array index would be part of the lesson.

The typical double-nested loop homework is matrix math or various types of sorting. This one feels more like realizing that ASCII is just a number to a computer. Hard to tell without additional context,though.

Yeah, that made sense from what Cogman said...might also be a good teachable moment about off by one errors in there, too.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Looks good, though the use of memset, size_t and sizeof() would likely rule it out for OP, you could do those less efficiently using things the OP probably *has* learned.

I was objecting to what sounded like a blanket statement that nested loops are totally unnecessary.

ah. No, nested loops are totally necessary. Just, not here. As for the memset and size_t, those were strictly for speeds sake.
 

KCfromNC

Senior member
Mar 17, 2007
208
0
76
And the problem is with it being IN the for loop. You just declared a variable N times (length of the string) for no good reason. This takes up space.

No it doesn't. Any good compiler is going to keep the intermediate result in a register (if I'm guessing the rest of the function correctly). Even if it has to spill the result it'll still just use 1 stack slot in either case. It's not like there's a 1 to 1 correlation that the running code creates memory every time it sees "char val" in the loop begin run. Even dumb compilers are way beyond that.
 
Last edited:

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
No it doesn't. Any good compiler is going to keep the intermediate result in a register (if I'm guessing the rest of the function correctly).

Of course it isn't a code problem, it's a compiler problem rofl. The Cogaman wrote it. It is infallible just like his English. You guys are just desperate now. Any good compiler would take apart my nested for loops for me too. Gotta love those compilers.

EDIT:

While limiting variables to its' scope is good practice, in this case the main is the scope due to how short the program is. Quite honestly either method would be near equal to the compiler depending on the compiler. I'm just giving you a hard time.
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Actually I didn't have an issue with TOUPPER, again you don't understand what I'm saying. I told you I wouldn't have used toupper simply because its another function, not because its slow and retarded like all the code I write.

And the problem is with it being IN the for loop. You just declared a variable N times (length of the string) for no good reason. This takes up space. Put the declaration of val outside the for loop to avoid this wasted space.

Thanks.

EDIT:

Also you spelled "equivalents" and "characters" wrong, lol.
What is wrong with using "another function". You still haven't explained why that is bad. Programming with functions is a fact of life.

As for the using more memory, you really don't understand what happens the compiler, do you.

var being declared inside a loop doesn't mean that each time the loop restarts it is being smashed, on the contrary, that memory location is saved and reused each time the loop restarts By declaring it inside the loop, I have caused my program to use less memory, because when the loop exits that location in the stack is freed up. Declaring it outside the loop would have resulted in a higher memory usage because it couldn't be removed from the stack until the end of the function.

Even that in mind, the compiler is smart enough to see that this variable that is only used inside the loop is used frequently inside the loop, it doesn't allocate any memory and instead uses a GP register to store the results of the function. In other words, it doesn't even touch the stack. Had it been declared outside of the loop, however, I couldn't have guaranteed that that register wouldn't have eventually been stored in the stack after the loop finished.

As for the spelling issues, yep, I suck at spelling.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
No it doesn't. Any good compiler is going to keep the intermediate result in a register (if I'm guessing the rest of the function correctly). Even if it has to spill the result it'll still just use 1 stack slot in either case. It's not like there's a 1 to 1 correlation that the running code creates memory every time it sees "char val" in the loop begin run. Even dumb compilers are way beyond that.

See my post above. Even if the compiler was completely retarded and only implemented what I wrote, it would STILL use less memory than what Tweak thought it would use.
 

ObscureCaucasian

Diamond Member
Jul 23, 2006
3,934
0
0
And the problem is with it being IN the for loop. You just declared a variable N times (length of the string) for no good reason. This takes up space. Put the declaration of val outside the for loop to avoid this wasted space.

Are you implying that code like

Code:
for (int i = 0; i < 100; i++) {
    int j = i;
}

Would use more memory than something like:

Code:
int j;
for (int i = 0; i < 100; i++) {
    j = i;
}
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
What is wrong with using "another function". You still haven't explained why that is bad. Programming with functions is a fact of life.

As for the using more memory, you really don't understand what happens the compiler, do you.

var being declared inside a loop doesn't mean that each time the loop restarts it is being smashed, on the contrary, that memory location is saved and reused each time the loop restarts By declaring it inside the loop, I have caused my program to use less memory, because when the loop exits that location in the stack is freed up. Declaring it outside the loop would have resulted in a higher memory usage because it couldn't be removed from the stack until the end of the function.

Even that in mind, the compiler is smart enough to see that this variable that is only used inside the loop is used frequently inside the loop, it doesn't allocate any memory and instead uses a GP register to store the results of the function. In other words, it doesn't even touch the stack. Had it been declared outside of the loop, however, I couldn't have guaranteed that that register wouldn't have eventually been stored in the stack after the loop finished.

As for the spelling issues, yep, I suck at spelling.

My lord Cogbro, this is where you need to settle down. I said *I* wouldn't have used a function, I didn't say it was WRONG. Jebus. I never said anything was particularly wrong about your program. You asked me what I would have done so I answered *basically* what you did but not use "toupper" because you can do the math with subtraction.

Any programmer understands there is an infinite number of ways to solve a problem. This will always be true. You do things a bit different than I would just based on the use of ++var vs var++. I only use ++var when I need to.

As far as using less memory, I'm not sure it is any different in such a small program. Then again we come back to the argument of being uber super ultimate optimization. I'll try not to give you a hard time anymore. I didn't think you'd take it so seriously.
 

KCfromNC

Senior member
Mar 17, 2007
208
0
76
Of course it isn't a code problem, it's a compiler problem rofl.

It's not a problem at all, except that what you think will happen doesn't actually exist.

Code:
int count_char(const char *stringToSearch, int letterCounts[26])
{
   for (size_t i = 0; stringToSearch[i]; ++i)
   {
      // Convert the charactors to their uppercase equivelents.
      int val = toupper(stringToSearch[i]);
      letterCounts[val - 'A'] += 1;
   }
   return 0;
}

With gcc -O :

Code:
_Z10count_charPKcPi:
	.fnstart
.LFB12:
	@ args = 0, pretend = 0, frame = 0
	@ frame_needed = 0, uses_anonymous_args = 0
	stmfd	sp!, {r3, r4, r5, lr}
	.save {r3, r4, r5, lr}
	mov	r5, r0
	mov	r4, r1
	ldrb	r0, [r0, #0]	@ zero_extendqisi2
	cmp	r0, #0
	beq	.L2
.L3:
	bl	toupper
	sub	r0, r0, #65
	ldr	r3, [r4, r0, asl #2]
	add	r3, r3, #1
	str	r3, [r4, r0, asl #2]
	ldrb	r0, [r5, #1]!	@ zero_extendqisi2
	cmp	r0, #0
	bne	.L3
.L2:
	mov	r0, #0
	ldmfd	sp!, {r3, r4, r5, pc}
	.fnend

Show me where the code is adding extra space each time through the loop. Here's a hint : it isn't. It uses the same register to store the result of toupper each time. It pushes a few regs at the start of the function and doesn't touch the stack pointer until the end. Memory and register use is constant regardless of the size of the string - there's nothing changing there at all if the string is 1 or a million characters long.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
It's not a problem at all, except that what you think will happen doesn't actually exist.

Code:
int count_char(const char *stringToSearch, int letterCounts[26])
{
   for (size_t i = 0; stringToSearch[i]; ++i)
   {
      // Convert the charactors to their uppercase equivelents.
      int val = toupper(stringToSearch[i]);
      letterCounts[val - 'A'] += 1;
   }
   return 0;
}

With gcc -O :

Code:
_Z10count_charPKcPi:
	.fnstart
.LFB12:
	@ args = 0, pretend = 0, frame = 0
	@ frame_needed = 0, uses_anonymous_args = 0
	stmfd	sp!, {r3, r4, r5, lr}
	.save {r3, r4, r5, lr}
	mov	r5, r0
	mov	r4, r1
	ldrb	r0, [r0, #0]	@ zero_extendqisi2
	cmp	r0, #0
	beq	.L2
.L3:
	bl	toupper
	sub	r0, r0, #65
	ldr	r3, [r4, r0, asl #2]
	add	r3, r3, #1
	str	r3, [r4, r0, asl #2]
	ldrb	r0, [r5, #1]!	@ zero_extendqisi2
	cmp	r0, #0
	bne	.L3
.L2:
	mov	r0, #0
	ldmfd	sp!, {r3, r4, r5, pc}
	.fnend

Show me where the code is adding extra space each time through the loop. Here's a hint : it isn't. It uses the same register to store the result of toupper each time. It pushes a few regs at the start of the function and doesn't touch the stack pointer until the end. Memory and register use is constant regardless of the size of the string - there's nothing changing there at all if the string is 1 or a million characters long.

Did you intentionally ignore the rest of the post?

Just how one poster picked on using size_t to teach someone new how to program, I wouldn't start by declaring variables inside a loop either. While this is ultimately more efficient in many scenarios, early programmers can be confused by scope. Then again, I don't know why you'd guys see the humor when you didn't look at the problem from a basic C++ homework standpoint anyway.

Sorry.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Ok, guys this one has gone far enough, and I'm sure the OP has long fled for some forum where they just give you the answer to get a rep bump. MSDN perhaps. If you guys want to continue this topic, open another thread and keep it civil. Should be fun to watch. Meanwhile, we can all hope the OP now gets that 'A' is stored as a number.

Locked for humanitarian reasons
Markbnj
Programming moderator
 
Status
Not open for further replies.
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |