c++ homework help

Discussion in 'Mac Programming' started by bravens52, Mar 30, 2011.

  1. bravens52 macrumors regular

    Joined:
    Jul 16, 2009
    #1
    Hello im trying to work on this assignment and im modifying a previous assignment and i really need help and i am stuck .. my header file and declaration file are as follow

    .. if anyone could be of any help that would be greatly appreciated..
     

    Attached Files:

  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    You should at least explain where you're stuck or what the problem is.

    You should also explain what you've tried to get unstuck or solve the problem yourself.

    If you started with working code (presumably from prior assignment), then you should start with the working code and modify it one small step at a time. Start with the simplest things first. Make each addition or change work correctly before adding or changing anything else. Create tests to make sure things work correctly. Run every test whenever you finish each step, to make sure you haven't broken anything.

    This is a basic engineering strategy, and not specific to C++, or even software. Trying to make everything new work all at once is a recipe for failure.
     
  3. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #3
    i am stuck on .. MyString::MyString(const char* s)

    This constructor for the MyString class should initialize a new MyString object to the C string s. The required logic is:

    1. Set the string size for the new object to the length of the C string s.
    2. Set the string capacity for the new object to the next multiple of 16 that is greater than the string size (see the Programming Notes section for how to do this).
    3. Use the string array pointer for the new object to allocate an array of char. The size of the new string array should be the string capacity plus one.
    4. Copy the C string s into the string array.

    for number 1 .. i simply did length=s; and 2 is where i get stuck at.. thats where i need held first and foremost to move forward .. im assuming we did not learn that in class
     
  4. RiskyMr macrumors newbie

    Joined:
    Apr 8, 2009
    #4
    The key is to read the assignment carefully:
    "You will now need three data members: a pointer to a char..."
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    You can do this pretty easily:
    Code:
    length = strlen(s); //Setting length to s is nonsense
    capacity = ((length/16)+1)*16;
    store = new char[capacity+1];
    strcpy(store,s);
    Just copying this isn't going to do you much good. If you have no idea how to do this you need to talk to your teacher about getting help.

    -Lee
     
  6. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #6
    its like i understand it when you write it out .. its just i always mess up and get confused doing it alone.. i guess practice will make perfect.. thx
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Well what does the Programming Notes section say? There wasn't any such section in any of the files you posted.

    If you don't have a document with a Programming Notes section to refer to, then you're missing an important part of what you're expected to have.


    For grins, try doing it without using division or multiplication (or the equivalent shifting).
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Was there a time when you were comfortable with the assignments? Was there a place when things stopped making sense? Do you need to re-read some notes or your book to cement earlier concepts? If you are given an assignment and have no idea where to even begin, your inability to complete the assignment is not the real problem. It is a symptom of the real problem of you not having a firm grasp on the material. Were there problems in earlier chapters that were not assigned that you can work through? Can the teacher provide you with small supplementary programming problems to help shore up your understanding?

    When you read an assignment, can you break things down? You need to get the length of a C string. OK, have you done that before? If not, do you know what characteristics a C string has? How do you know when you've gotten to the end? How would you write strlen yourself if it wasn't available in the libraries? Once you have the length, how would you find the next multiple of 16 larger than that number? You could loop tracking multiples of 16 until this value is larger than the length you found. What if you found how many blocks of sixteen could fit into the length? How would you find that? A truncating/integer division could tell you that. Once you know how many sixteens fit into the length, you know you just need to be able to get one more block of sixteen into the capacity. So you just need to bump up what you found by one. OK, so now you have the number of sixteens you want your capacity to be. Good news, so now to get the total just multiply by sixteen. When it's all written out like that it seems a lot easier to think of it in terms of a loop, but once the logic is written out actually coding it is pretty straight-forward.

    Are you solving the presented problems on paper first or trying to come up with code first? Are you able to write down steps you'd take to get a solution on paper? What if you wrote down a sample string:
    My cat is the coolest cat on the block, and is really good at making pancakes.

    So how would you figure out how long that is? Count each character? When do you stop? On paper you just stop at the end. In code you have to look for a null to know you're at the end, but otherwise the concept is the same. How would you find the next multiple of sixteen greater than the length? Do you just know what the next multiple of 16 is? What if you didn't? Well, you could write them out until you found one greater than your length. In code, this would be a loop. If you thought of just dividing by 16 that's fine, too.

    I think you need to make an appointment with your instructor. They're not there to trick you and make you fail. You're responsible for your own success, but they can help, it's their job.

    -Lee
     

Share This Page