Thread: Need Help On an Assignment

  1. #46
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    @laserlight

    Well...str is the variable that's being put into contents. FrontStr takes the contents and adds it to the array. If I got rid of FrontStr and replaced it with str, what would go into contents?

    @jiggunjer

    I'm very new with delete and not sure what's wrong with it or what a one line code would be in this case. As for copy, I have never successfully used that before at all, so what's wrong with the syntax exactly?

    For size, I think I fixed it...

    Code:
    void IncreaseCapacity() {
      arraysize += 10;
      std::string* newarray = new std::string[arraysize];
      myarray[arraysize];
      myarray.copy(newarray);
      while (i < arraysize) {
        delete [] myarray[i];
        myarray[i] = NULL;
        ++i;
      }
      delete [] myarray;
      myarray = NULL;
      std::swap(myarray, newarray); 
    }
    Size declared, size implemented into new array, size also implemented into old array. Is that right?

  2. #47
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yeah now you can just do:
    Code:
    void IncreaseCapacity() 
    {
       // Make a new, bigger array:
       const int oldsize = arraysize;
       arraysize += 10;
       std::string* newarray = new std::string[arraysize];
    
       // Do the copy:
       for (int i = 0; i < oldsize; i++) {
          newarray[i] = myarray[i];
       }
    
       // Swap the old and new arrays:
       delete [] myarray;
       myarray = NULL;
       std::swap(myarray, newarray);
    }

  3. #48
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    So using this, AddFront and AddBack would be fine? Also, thanks for the notes, they really help me understand what's going on with the code. Thanks! =)

  4. #49
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    @Erik, I think you might want to look at the following line. Think of what the expression is evaluating into (it's missing the closing parenthesis, but I'm just thinking about the expression itself). I could be missing something, but this should not compile as is.

    Code:
    // where 'myarray' is an array of strings,
    // contents is the new string being added,
    // and arraysize represents the current number of elements of 'myarray'
    
    if ((myarray + contents) > arraysize {
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  5. #50
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Ahhh, right, so something like this?

    Code:
    int i = myarray;
    
    int j = contents;
    
    int k = arraysize;
    
    if (i + j > k) { }

  6. #51
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    myarray is a pointer, not an int.
    contents is a string, not an int.
    arraysize is the capacity, not the actual occupied size
    In short: your if statement makes no sense. In my header example I made a clear distinction between N and NNZ.
    Your if should look like
    Code:
    if(NNZ==N){increasesize()}
    BackAdd should add the string to the array while keeping the elements continuous. For example
    before: xxx... (3 strings in array of size 6)
    after: xxxx..(4 strings in array of size 6)
    currently your after is: xxx..x (non-continuous appending).

    P.S no need for a new keyword in BackAdd.

  7. #52
    Registered User
    Join Date
    Mar 2015
    Posts
    6
    Hi,I have a question in below code.
    in tower function in else, do act all of statement ?(I cant speak English well,sorry).
    Tanks
    Code:
    
    
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <iostream>
     
    void tower(int,char,char,char);
    int main()
    {
    int ndisk;
    system("cls");
    printf("\nEnter number of disks : ");
    scanf("%d",&ndisk);
    tower(ndisk,'A','B','C');
    getch();
    return 0;
    }//end of main()
     
    void tower(int topN, char src,char aux,char dest)
    {
    if(topN == 1)
    {
    printf("\n Disk 1 from %c to %c ",src,dest);
    }
    else
    {
    tower(topN-1,src,dest,aux); //src to aux
    printf("\n Disk %d from %c to %c ",topN,src,dest);
    tower(topN-1,aux,src,dest); //aux to dest
    }
    }//end of tower()

  8. #53
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please start a new topic in the C forum.
    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. #54
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Erik Ingvoldsen View Post
    Ahhh, right, so something like this?

    Code:
    int i = myarray;
    
    int j = contents;
    
    int k = arraysize;
    
    if (i + j > k) { }
    You will need to check the current size of the array (in elements), against the current capacity of the array.

    Referring back to jiggunjer's example, think of it like this, your array is a cup, that is designed to hold a certain amount of water. The "capacity" of the cup is the total number of ounces that it could contain currently. The "size" is the number of ounces that it actually holds (ie. You could have a 10 ounce glass with 4 ounces of water in it). So every time you add an ounce (a single string, of any length) to the cup, you check to see if the size is going to exceed the capacity.

    If the cup is about to overflow, you get a bigger cup, then you pour all the liquid from the old cup into the new one, and finally set the new cup where the old cup was (that is what IncreaseSize() is doing).
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  10. #55
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Oh! I goofed, for some reason I was thinking myarray would also give the size. ^^;

    Code:
    if(NNZ==N){increasesize()}


    What I don't get is why you have an equal sign for this. Isn't the point to increase the size if it's larger? Or at the very least have it set to >= for greater than or equal to? Or did I miss something?

    And for AddBack, would doing a double swap solve this problem or would it still be the same?

  11. #56
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Erik Ingvoldsen View Post
    What I don't get is why you have an equal sign for this. Isn't the point to increase the size if it's larger? Or at the very least have it set to >= for greater than or equal to? Or did I miss something?
    Assuming size is the size of the number of elements in the array and capacity is the array's capacity.
    What is the value of size and capacity when the array is full?
    If it makes it easier, assume that you're allocating an array of 10 elements, then filling the array up with 10 elements.
    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.

  12. #57
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    When the array is full, both would be 10, but when I need to increase it, size would be over 10, no?

  13. #58
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Depends on where you increase the size. Consider a function AddBack that is called when the array is full.
    The precondition is array size == capacity.
    Then the logic is either:

    Increase size
    Check if array is full

    OR

    Check if array is full
    Increase size

    In the first case, array size == capacity and in the second case array size > capacity.
    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.

  14. #59
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Alright well this is what I have for AddFront.

    Code:
    void AddFront(std::string str) {
      std::string FrontStr;
      std::string contents(str);
      currentsize = sizeof myarray;
      if(currentsize == arraysize) {
        IncreaseCapacity();
      }
      contents.copy(FrontStr, arraysize);
      std::swap(myarray[i], FrontStr);
    }
    currentsize is the private int for the currentarray size, but I'm not 100% sure if I did it properly.

  15. #60
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't understand why you make so many unnecessary copies...
    Using backwards substitution:

    std::swap(myarray[i], FrontStr);
    contents.copy(FrontStr, arraysize);
    Substitute FrontStr with contents.
    std::string contents(str);
    Substitute contents with str.
    That is, FrontStr == contents == str. You have three strings with exactly the contents for absolutely no reason.

    Therefore, since we have a std::string, all you have to do is:
    myarray[i] = str;

    Okay, that's that. But there is another issue to consider:

    >>currentsize = sizeof myarray;
    What does sizeof do? What is the type of myarray? Know that sizeof behaves differently with arrays and pointers.
    You will find that this won't do what you think it does.

    Indeed, consider:
    int a[10];
    sizeof(a) == ???
    a[0] = 0;
    sizeof(a) == ???
    a[1] = 0;
    sizeof(a) == ???
    // etc

    int* a = new int[10];
    sizeof(a) == ???
    a[0] = 0;
    sizeof(a) == ???
    a[1] = 0;
    sizeof(a) == ???
    // etc
    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.

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