Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic C++ problems
#41
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.
Reply
#42
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.
Reply
#43
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:Confusedtring str = "Hello World";
char[] str = "Hello World";
char* str = "Hello World";
const char* str = "Hello World";
Reply
#44
Locked Wrote: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:Confusedtring str = "Hello World";
char[] str = "Hello World";
char* str = "Hello World";
const char* str = "Hello World";

I do string str = "Hello World"
Reply
#45
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.
Reply
#46
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:
" Consider this data sequence: "fish bird reptile reptile bird bird bird mammal fish". Let's define a SINGLETON to be a data element that is not repeated immediately before or after itself in the sequence. So, here there are four SINGLETONs (the first appearance of "fish", the first appearance of "bird", "mammal", and the second appearance of "fish").

Write some code that uses a loop to read a sequence of words, terminated by the "xxxxx". The code assigns to the variable n the number of SINGLETONs that were read. (For example in the above data sequence it would assign 4 to n). Assume that n has already been declared but not initialized. Assume that there will be at least one word before the terminating "xxxxx". "
Reply
#47
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.
Reply
#48
Bumping cause I have another midterm.

Code:
string reverse(string s)
{
char temp;
int i = 0;
int j = s.size() – 1;
while (i < j)
{
??? WHAT GOES HERE ???
}
return s;
}

Options for ??? WHAT GOES HERE ??? are:
Code:
A.temp = s.substr(0, i)
s = s.substr(0,j) + temp;

B. temp = s.at(i);
s.at(i) = s.at(j);
s.at(j) = temp;

C.temp = s.substr(j);
s = temp + s.substr(i, j);

D.s.at(i) = s.at(j);
s.at(j) = s.at(i);

E.temp = s.at(i);
s = s.substr(i + 1);
s += temp;

6.s = s.substr(j) + s.substr(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).
Reply
#49
None of the above, actually, since none of them move i and j.
But if we ignore that, then yes, it's B.
Reply
#50
There is a faster way to do this.

Code:
string reverse(string s)
{
  return string(s.rend(), s.rbegin());
}

But if you want to do it manually..

Code:
string reverse(string s)
{
  char temp;
  int length = s.length() - 1;
  int numSwaps = s.length() >> 1;

  for(int i = 0; i < numSwaps; i++)
  {
    temp = s[i];
    s[i] = s[length-i];
    s[length-i] = temp;
  }
  return s;
}
Reply
#51
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());
Reply
#52
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:Confusedtring 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.
Reply
#53
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.
Reply
#54
Wow that's your professor's code?

Holy shit that's awful.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)