2012-10-28, 10:18 PM
Locked Wrote:How would you store the text? What data type do you use that it doesn't involve arrays or pointers?
No idea what pointers are.
|
Basic C++ problems
|
|
2012-10-28, 10:18 PM
Locked Wrote:How would you store the text? What data type do you use that it doesn't involve arrays or pointers? No idea what pointers are.
2012-10-28, 10:21 PM
Locked Wrote:How would you store the text? What data type do you use that it doesn't involve arrays or pointers? I think you're thinking beyond his scope. If he's using strings it's probably for something simple, like storing a word into a variable with cin and printing the same thing out later with cout. I don't think you can do more than that without treating them like arrays of characters, which he obviously isn't doing if he's not doing arrays.
2012-10-28, 10:25 PM
FabledGumbo Wrote:I think you're thinking beyond his scope. If he's using strings it's probably for something simple, like storing a word into a variable with cin and printing the same thing out later with cout. I don't think you can do more than that without treating them like arrays of characters, which he obviously isn't doing if he's not doing arrays. When you declare a variable in C++, you must specify the data type. Example: int a = 0. a is an integer type int and is assigned the value of 0. Strings are the same way. You have either std: tring str = "Hello World";char[] str = "Hello World"; char* str = "Hello World"; const char* str = "Hello World";
2012-10-28, 10:26 PM
Locked Wrote:When you declare a variable in C++, you must specify the data type. I do string str = "Hello World"
2012-10-28, 10:34 PM
He's using namespace std, meaning he'd just declare the variable as a string. My point is that he doesn't have any decent tools at his disposal at this point in time to solve this problem using strings.
2012-10-28, 10:35 PM
Imagine Wrote:I do string str = "Hello World" Okay. That kind of brings me back to the original std thing. std is the namespace of the standard. You use this (std::<object>) to specify that the object comes from the standard library. When you use "using namespace std;" you're dragging the entire std namespace into your code so you wouldn't write std::<object>, rather you'd write <object> instead. This is looked down upon and I hope that they eventually teach you to stop, but for now I guess it doesn't matter. FabledGumbo Wrote:He's using namespace std, meaning he'd just declare the variable as a string. My point is that he doesn't have any decent tools at his disposal at this point in time to solve this problem using strings. Except he has a problem regarding it actually. Imagine Wrote:How would I deal with:
2012-10-28, 11:23 PM
I'm not familiar with how cstring works, but if you can't compare one string to another string using an if statement, I don't think he or I could solve that problem. Regardless, it seems like the focus of the problem is to use a loop to determine if two things are the same or not, not fight with a bunch of syntax with strings that you can't properly do without arrays or pointers.
2012-11-18, 09:50 PM
Bumping cause I have another midterm.
Code: string reverse(string s)Options for ??? WHAT GOES HERE ??? are: Code: A.temp = s.substr(0, i)The purpose of the function is to to flip around a string (i.e testing turns in gnitset). I would assume i is incremented in this problem and that j is decremented. With that assumption, would the correct answer be B? My logic for B is that we have a temp variable to hold of s.at(i) so that it won't be lost, and that once s.at(i) is set to s.at(j), s.at(j) is given the char stored in s.at(i) through temp. Then the loop would keep going until i = j (odd # of letters) or i = j + 1 (even # of letters).
2012-11-18, 10:10 PM
None of the above, actually, since none of them move i and j.
But if we ignore that, then yes, it's B.
2012-11-18, 10:18 PM
There is a faster way to do this.
Code: string reverse(string s)But if you want to do it manually.. Code: string reverse(string s)
2012-11-18, 10:19 PM
Please tell me you're writing this yourself because that function coming from an instructor is pretty bad. (No offence, OP)
[MENTION=1]Fiel[/MENTION]; Or save yourself the function rewrite and do std::reverse(str.begin(),str.end());
2012-11-18, 10:29 PM
Locked Wrote:Or save yourself the function rewrite and do std::reverse(str.begin(),str.end()); Arguably the best solution since there's only one assignment for std: tring and no copy constructors used. Win win for all. But I don't think we answered his question hahaha. I thought he was asking how to flip a string around.
2012-11-18, 11:42 PM
Fiel Wrote:-snip- It was a MC quiz question, so there's no point on making it better. The point was to test if I can trace through loops, not make the code better. |
|
« Next Oldest | Next Newest »
|