Thread: Need Help On an Assignment

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    225

    Need Help On an Assignment

    I'm working on an assignment and am very new to a lot of the concepts it wants me to do.

    I have not finished the code yet. As of now I simply want to see if my code looks correct and I have some questions about the assignment itself. Below is my assignment is below that is my current code.

    I haven't yet gotten to member function 1, so you don't have to read there and beyond yet. All I've done is added the names to the header.

    ~~~

    Instructions: In this lab you will create a class for a Dynamic Array of Strings. This class may be useful for future projects. The class will provide the following functionality:

    Get a string from the array

    Add a string to the front or back of the array

    Delete a string from the front or back of the array

    Get the number of strings in the array

    Get the maximum capacity of the array

    Clear the array

    Sort the array

    Get the array as a comma separated list

    Ability to grow on demand

    For Extra Credit you can add the following:

    Save the array to a file

    Retrieve a previous saved array from a file

    Class –DynamicStringArray:
    Contains a dynamic array of dynamic strings.

    Private Data Members:
    A dynamic array of dynamic strings, i.e. a dynamic array of string*s.
    An integer to hold the maximum capacity of your array.
    An integer to hold the current size of your array.

    Default Constructor:
    Initializes the array to 10 elements and initializes the elements to NULL.

    Overloaded Constructor:
    Initializes the array via an unsigned integer parameter. If the parameter's valueis not valid (i.e. 0) set the array to a capacity of 10. Initializes all elements to NULL.

    Destructor:
    Frees the memory for all strings
    Frees the memory for the dynamic string* array

    Member Function 1:
    Named At. Has one parameter, anunsigned integer that corresponds to a location in the array. If that location doesn't exist in the array throw the string "Invalid Location". If the location does exist return a pointer to the string.

    Member Function 2:
    Named GetFirst. Returns a pointer to the first string in the array. If the array is empty throw the string "Array Empty".

    Member Function 3:
    Named GetLast. Returns a pointer to the first string in the array. If the array is empty throw
    the string "Array Empty".

    Member Function 4:
    Named AddFront. Has one parameter, a dynamic string. Adds this string to the beginning of the array. Note: this requires you to shift all the elements in the array. If the array is full increase the capacity of the array (call the IncreaseCapacityfunction) and then add the dynamic string.

    Member Function 5:
    Named AddBack. Has one parameter, a dynamic string. Adds this string to the end of the array. If the array is full increase the capacity of the array (call the IncreaseCapacityfunction) and then add the dynamic string.

    Member Function 6:
    Named DeleteFront. Deletes the first element in the array. Note: this requires you to shift all the elements in the array. If the array is empty throw the string "Array Empty".

    Member Function 7:
    Named DeleteBack. Deletes the last element in the array. If the array is empty throw the string "Array Empty".

    Member Function 8:
    Named GetSize. Returns an unsigned integer that is the current size of the array. This is a const function.

    Member Function 9:
    Named GetCapacity. Returns an unsigned integer that is the current maximum capacity of the array. This is a const function.

    Member Function 10:
    Named Empty. Returns a Boolean. True if the array's size is 0. This is a const function.

    Member Function 11:
    Named Clear. Deletes all of the dynamic strings in the array and sets the size of the array to 0.

    Member Function 12:
    Named Sort. Sorts the array A-Z. You must use the private function ToUpper as this helps when comparing strings of mixed case.

    Member Function 13:
    Named ToString. Returns a string containing all of the strings in
    the array, comma separated.

    Overloaded Friend operator<<:

    Outputs a comma separated list of all of the strings in the array.

    Private Member Function 1:
    Named IncreaseCapacity. Increases the capacity of the array by 10. Note: this requires you making a second dynamic array and "copying" over the original data.

    Private Member Function 2:
    Named ToUpper. Takes a string as a parameter and returns that
    same string with all the characters uppercased.


    ~~~

    Code:
    #include "dynamic_string_array.h"
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cctype>
    #include <sstream>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::stringstream;
    
    DynamicStringArray::DynamicStringArray() {
      int *myarray[10] = {};
    }
    
    DynamicStringArray::DynamicStringArray(int arraymax = 10) {
      arraymax_ = arraymax;
      int *myarray[arraymax] = {};
    }
    
    DynamicStringArray::~DynamicStringArray() {
      delete[] arraysize_;
    }
    Code:
    #ifndef DYNAMIC_STRING_ARRAY_H
    #define DYNAMIC_STRING_ARRAY_H
    #include <string>
    #include <climits>
    
    class DynamicStringArray {
    private:
      int arraymax_;
      int arraysize_;
      IncreaseCapacity();
      ToUpper();
    public:
      DynamicStringArray();
      At(unsigned int Loc);
      GetFirst();
      GetLast();
      AddFront(string *str);
      AddBack(string *str);
      DeleteFront();
      DeleteBack();
      GetSize();
      GetCapacity();
      Empty();
      Clear();
      Sort();
      ToString();
    }
    
    #endif /* DYNAMIC_STRING_ARRAY_H */
    ~~~
    Questsions:

    1) As of now does my code look correct? I want to make sure I at least have the basics done before moving on to the rest of it.

    2) What does the assignment mean when it says "return a pointer"?

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I just wrote a post that I swear just disappeared when I posted lol.

    To try to hit the same points, here is a nice tutorial on pointers:

    http://pw1.netcom.com/~tjensen/ptr/ch1x.htm

    Here is a StackOverflow thread showing some different way you might be able to create and initialize a dynamic array using the 'new' keyword:

    http://stackoverflow.com/questions/9...alise-an-array

    Some things I noticed in your code:

    1. The methods in the class declaration will require valid return types (to the left of the method name).

    2. The following code in the constructor function creates local variable (it disappears after the function is done) that has a type of "array of pointer to int". This means it is an array with 10 elements capable of holding the addresses of int types:
    Code:
    DynamicStringArray::DynamicStringArray() {  int *myarray[10] = {};
    }
    3. In the destructor function, you shouldn't call delete on variables that you haven't allocated with the new keyword.
    Last edited by Alpo; 03-24-2015 at 07:54 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    1. Some of this I can figure out (like 10 is bool), but I have no idea what the return type would be on something like the first function. Can you tell me?

    2. So I'm guessing that needs to be changed?
    Code:
    int *myarray = new int[10];
    int *myarray[10] = {};
    Would this work for setting up the array and declaring everything NULL?

    3. So would I need to do the above for that too?

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    For the things where the return is unspecified, I would return nothing (void).

    For #1 the return is a pointer to a 'string'. I'm not sure if this means a pointer to char or a pointer to the std::string. Assuming it's a char*:

    Code:
     // Return pointer to char:
     char* DynamicStringArray::at(const unsigned int index)
     {
         // Check: index < 0 || index >= m_arraySize
         // if so, throw an error
         // if not, return element at m_array[index]
     }
    The actual array itself is supposed to contain these "dynamic string" elements. The purpose of the class seems to be memory management of these strings. So the array containing the strings will need to be a data member of the class (I always like to indicate something is a member with the 'm_' prefix). So if the type of a dynamic string is a pointer to char, then a pointer to that would be a "pointer to pointer to char":

    Code:
     class DynamicStringArray
     {
         // ...
     private:
         char** m_array;
         static const int m_defaultIncrease = 10;
         int m_arraySize;
         // ...
     }
    Then the default constructor (the one with no parameters) would need to initialize "m_array" with some dynamic memory:

    Code:
    DynamicStringArray:ynamicStringArray()
    {
         m_array = new char*[m_defaultIncrease];
         m_arraySize = m_defaultIncrease;
    }
    Keep in mind, this is an array of type pointer to char (char*). The actual elements have not been allocated any memory as of yet. For each element to act as it's own dynamically allocated string, you will have to initialize each element of the "m_array" with it's own memory using the new operator.

    Pointers can be some of the most confusing and error prone things in C++ programming (There are classes devoted specifically to helping you with them). So it's not a big deal if this is confusing, my advice is to follow that pointer tutorial until you feel confident enough to proceed, it covers pointers to pointers, and at the end covers how to use them to build a sorting algorithm (the bubble sort IIRC). It's one of the best tutorials on pointers I've seen.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    I'm a tad confused. How does this set the arrays elements to NULL?

    Code:
    DynamicStringArray:ynamicStringArray()
    
    {
    
         m_array = new char*[m_defaultIncrease];
    
         m_arraySize = m_defaultIncrease;
    
    }


    And is this code looking right now?

    Code:
    #include "dynamic_string_array.h"
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cctype>
    #include <sstream>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::stringstream;
    
    DynamicStringArray::DynamicStringArray() {
      myarray = new char*[defaultincrease];
      arraysize = defaultincrease;
    }
    
    DynamicStringArray::DynamicStringArray(int arraymax = 10) {
      arraysize = arraymax;
      int *myarray[arraymax] = {};
    }
    
    DynamicStringArray::~DynamicStringArray() {
      delete[] arraysize_;
    }
    
    char * DynamicStringArray::At(const unsigned int i) {
      if((i < 0) || (i > myarray) {
        throw "Invalid Location";
      }
      return string* myarray->at(i);
    }
    Code:
    #ifndef DYNAMIC_STRING_ARRAY_H
    #define DYNAMIC_STRING_ARRAY_H
    #include <string>
    #include <climits>
    
    class DynamicStringArray {
    private:
      char** myarray;
      static const int defaultincrease = 10;
      int arraysize;
      int IncreaseCapacity();
      string ToUpper();
    public:
      DynamicStringArray();
      char* At(unsigned int loc);
      char* GetFirst();
      char* GetLast();
      void AddFront(string *str);
      void AddBack(string *str);
      void DeleteFront();
      void DeleteBack();
      int* GetSize();
      int* GetCapacity();
      bool Empty();
      void Clear();
      string* Sort();
      string* ToString();
    }
    
    #endif /* DYNAMIC_STRING_ARRAY_H */

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>int *myarray[arraymax] = {};
    Why do you have this, seriously? What do you think it's doing?

    >>delete[] arraysize_;
    Why are you trying to delete an int?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Well, I figured this part was setting up the array.

    >>int *myarray[arraymax] = {};

    I figured this part was setting up the maximum amount of elements it could have.

    >>int *myarray[arraymax] = {};

    And I figured this was setting it to NULL.

    >>int *myarray[arraymax] = {};

    I'm guessing I was way off then? ^^; I admit with the other part, I only had the vaguest idea of what I was doing.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    int* myarray[arraymax] = {};
    Due to the part in red color, you are declaring a new local variable named array.
    But even if you leave that out, you get myarray[arraymax] = {}, which means you're trying to assign an initializer list {} to the pointer in myarray[arraymax]. Clearly does not work. A pointer is not an array.

    Forget the interface for now. Focus on learning how to handle dynamic 2D arrays. So first off: what's your plan? How do you make a 2D array? How do you initialize it? How do you use it? How do you free it? Start with those questions first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Quote Originally Posted by Elysia View Post
    So first off: what's your plan? How do you make a 2D array? How do you initialize it? How do you use it? How do you free it? Start with those questions first.
    In order:

    No real "plan" to be honest. I'm just following directions as I go.

    Not sure. I have a general idea how and I've researched it, but I haven't done it in practice, which is why I made this topic. The stuff I point down is what I read would declare an array of 10 elements and make them all NULL. Ultimately I don't know what I'm doing wrong.

    Same as a above.

    I only have a general idea on how to use them. We've just started learning this.
    Last edited by Erik Ingvoldsen; 03-25-2015 at 12:59 PM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It might help to visualize how to do it.
    If you start with a 1D array, you can allocate N elements of some type T. Say int, or whatever.
    How do you allocate a dynamic 1D array? You use new and specify an array type and a size. Operator new returns a pointer.
    Now, what if, instead of T being an int, it was an actual array? So you would have an array of arrays.
    So you allocate a 1D array of pointers, and then you allocate an array for each of those pointers.

    Very crude image:
    Need Help On an Assignment-array-png
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    I understand the concept, but I'm not sure what that has to do with what I'm currently doing.

    Code:
    DynamicStringArray::DynamicStringArray() {
      myarray = new char*[defaultincrease];
      arraysize = defaultincrease;
    }
    For example with this...this calls the array and sets it to 10 elements, but I'm not seeing how it would declare it all NULL. I've read before that this is how you set all the elements of an array to NULL

    Code:
    int *myarray[] = {};
    But you said it was wrong and I'm not sure how else to do it.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>myarray = new char*[defaultincrease];
    What does this do?

    Let's just forget about 2D arrays for the moment. Let's just create a 1D array of ints. Can you do that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Like this, right?

    Code:
    int array [3] = { 0, 4, 10 };

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, but that's a static array. What if it's a dynamic array, i.e. the size is not known at compile time?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    This?

    Code:
    int size;
    int array [size] = {};

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need some help with an assignment..please.
    By ronaldh12 in forum C Programming
    Replies: 3
    Last Post: 09-30-2011, 09:33 AM
  2. Help!! Assignment!
    By Joeshua Lee in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2011, 09:30 AM
  3. Help!! Assignment!
    By Joeshua Lee in forum C Programming
    Replies: 3
    Last Post: 08-09-2011, 09:30 AM
  4. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  5. Assignment Help...maybe..
    By TerribleatC in forum C Programming
    Replies: 5
    Last Post: 01-05-2007, 08:05 AM