Page 3 of 3 FirstFirst 123
Results 41 to 54 of 54
  1. Default Re: Basic C++ problems


    No idea what pointers are.

  2. Default Re: Basic C++ problems


    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.

  3. Default Re: Basic C++ problems


    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::string str = "Hello World";
    char[] str = "Hello World";
    char* str = "Hello World";
    const char* str = "Hello World";

  4. Default Re: Basic C++ problems


    I do string str = "Hello World"

  5. Default Re: Basic C++ problems


    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.

  6. Default Re: Basic C++ problems


    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.

    Except he has a problem regarding it actually.


  7. Default Re: Basic C++ problems


    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.

  8. Default Re: Basic C++ problems


    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).

  9. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Basic C++ problems


    None of the above, actually, since none of them move i and j.
    But if we ignore that, then yes, it's B.

  10. Default Re: Basic C++ problems


    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;
    }

  11. Default Re: Basic C++ problems


    Please tell me you're writing this yourself because that function coming from an instructor is pretty bad. (No offence, OP)

    @Fiel;

    Or save yourself the function rewrite and do std::reverse(str.begin(),str.end());

  12. Default Re: Basic C++ problems


    Arguably the best solution since there's only one assignment for std::string 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.

  13. Default Re: Basic C++ problems


    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.

  14. Default Re: Basic C++ problems


    Wow that's your professor's code?

    Holy shit that's awful.

  15.  

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •