Like Tree11Likes

Problem in concatenation of arrays

This is a discussion on Problem in concatenation of arrays within the C++ Programming forums, part of the General Programming Boards category; I think the discussion is getting non-academic more than fruitful..we should top here..stop it all please....

  1. #46
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    I think the discussion is getting non-academic more than fruitful..we should top here..stop it all please.

  2. #47
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by rags_to_riches View Post
    Tha'ts just closing eyes and ignoring facts..hadn't been you there to see my posts after every adak's sugggestion?..You are infact beating about the bush..Surprised to see mates like you whose job is to just make remember which one is doing what ?(by not helping others/suggesting others ) .

  3. #48
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Matticus View Post
    Care to qualify this statement?
    Yes. See my comment in the src code I updated for the OP; up near the top referencing astyle.

  4. #49
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    This line?

    // Formatted using astyle -A4 -U -p -f filename.cpp
    It doesn't equate to the statement "indentation is nothing." If anything, it shows the opposite.

    Also, in my own personal opinion, someone learning the language should strive for simple readability (e.g. indentation) themselves manually, instead of relying on external tools for this purpose. It's just good practice (in both senses of the word), IMO.

  5. #50
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    My solution is not "wrong".
    Your solution is not wrong in the sense that the program does what it was intended to do. But that doesn't say a lot. You could replace every loop in the code with ifs and gotos, and the code would still not be wrong in that sense; it would work. It would be easy to justify why such a solution is not satisfactory. And it would certainly not be something to teach newcomers.

    In fact, my contributions give more insight into what is happening than using vectors can ever hope to do.
    You could solve this problem using only assembly and it would probably provide more "insight into what is happening" than your solution does. But that wouldn't get the OP any closer to a proper C++ program. Here are 2 things that would help in that regard:
    Using std::vector.
    Writing the code (rather than having it handed on a silver platter)
    Elysia likes this.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  6. #51
    Registered User
    Join Date
    Apr 2013
    Posts
    1,341
    The original poster is new to programming, so I wouldn't expect the code to be very good, just something that works. After he has a working version, he can try to clean it up.

    Getting back to the original problem, where's the actual CRC code? You have the user enter a message, then enter a pattern, which you then effectively append to the message. Implementing CRC is usually done similar to long hand division, except that xor is used instead of subtract, and what is appended to the message is the remainder.

    So if the CRC "polynomial" has "n+1" bits, producing a "n" bit remainder after division, and a message length of "m" bits, you append "n" zeroes to the message, then divide that message by the CRC "polynomial", then store the remainder into those "n" zeroes that you appended to the message.

    remainder = (message x 2^n)%(crc_polynomial)

    encoded _message = (message x 2^n) - remainder

    Where - is exclusive or, but in this case it's just "n" appended zeroes being xor'ed to so you can just store the remainder.

    update - It's not clear to me if the append part of this program was to really do the equivalent of reallocating an array, as opposed to using a large array (like 256 bytes), and then adjusting a variable that represents the length of the message without involving any array reallocation. A second array would be needed for the CRC generator polynomial.
    Last edited by rcgldr; 08-21-2013 at 09:17 PM.
    SirPrattlepod likes this.

  7. #52
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by rcgldr View Post

    Getting back to the original problem, where's the actual CRC code? You have the user enter a message, then enter a pattern, which you then effectively append to the message. Implementing CRC is usually done similar to long hand division, except that xor is used instead of subtract, and what is appended to the message is the remainder.

    So if the CRC "polynomial" has "n+1" bits, producing a "n" bit remainder after division, and a message length of "m" bits, you append "n" zeroes to the message, then divide that message by the CRC "polynomial", then store the remainder into those "n" zeroes that you appended to the message.

    remainder = (message x 2^n)%(crc_polynomial)

    encoded _message = (message x 2^n) - remainder

    Where - is exclusive or, but in this case it's just "n" appended zeroes being xor'ed to so you can just store the remainder.

    update - It's not clear to me if the append part of this program was to really do the equivalent of reallocating an array, as opposed to using a large array (like 256 bytes), and then adjusting a variable that represents the length of the message without involving any array reallocation. A second array would be needed for the CRC generator polynomial.
    The Program met its purpose and YES it was JUST the initials to do so.that's why posted that anyhow

  8. #53
    Registered User
    Join Date
    Apr 2013
    Posts
    1,341
    If the long term goal is to generate CRC, normally an array large enough to hold the largest message and the redundancy data (remainder) would be used, but since this is a classroom program, maybe the goal of expanding arrays was wanted. Here is an example using realloc, which is a "C" method, but if you going to use an array instead of a vector, then malloc and realloc would be alternatives to using new. Note that realloc preserves the current data when it reallocates memory, but if it's an expansion, the additional memory is uninitialized. This example could be considered to be a C program (versus C++):

    Code:
    #include <malloc.h>
    #include <stdio.h>
    int main()
    {
    int * pArray;                           /* pointer to array */
    int * pTemp;                            /* temp ptr */
    int i;
        /* allocate 20 integers */
        pArray = malloc(20*sizeof(int));
        if(pArray == NULL)
            return(0);
        for(i = 0; i < 20; i++)             /* put data into the array */
            pArray[i] = i;
        /* reallocate to 30 integers */
        pTemp = realloc(pArray, 30*sizeof(int));
        if(pTemp == NULL){                  /* if realloc failed */
            /* this could be a goto the free below, to avoid redundant code */
            free(pArray);
            return(0);
        }   
        pArray = pTemp;                     /* update pArray */
        for(i = 20; i < 30; i++)            /* put more data into the array */
            pArray[i] = i;
        for(i = 0; i < 30; i++){            /* display the array */
            printf("%4d ", pArray[i]);
            if(9 == (i%10))
                printf("\n");
        }
        free(pArray);                       /* free the memory */
        return(0);
    }
    Last edited by rcgldr; 08-22-2013 at 01:52 AM.

  9. #54
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Seriously, after people in the thread complain in full force that manual reallocation is bad, you come and do the same? And to make matters worse, you use C and you hand out a solution?
    We are trying to teach the OP modern C++ here. If the OP was interested in C, he/she would have posted in the C section. Stop posting C code in the C++ forum, please.
    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.

  10. #55
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Elysia View Post
    Seriously, after people in the thread complain in full force that manual reallocation is bad, you come and do the same? And to make matters worse, you use C and you hand out a solution?
    We are trying to teach the OP modern C++ here. If the OP was interested in C, he/she would have posted in the C section. Stop posting C code in the C++ forum, please.
    It's valid C++.

    Where, exactly, have you taught (or tried to teach) the OP anything at all? *confused*

  11. #56
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Quote Originally Posted by SirPrattlepod View Post
    It's valid C++.
    Valid C++ code does not make good C++ code!
    And no, this is not valid C++ code (it will not compile).

    Where, exactly, have you taught (or tried to teach) the OP anything at all? *confused*
    vectors.
    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
    Jun 2005
    Posts
    6,656
    Quote Originally Posted by SirPrattlepod View Post
    It's valid C++.
    Most C code can be compiled as C++, but a lot of C techniques - including ones you used - are considered very poor practice in C++. By using such code in a C++ forum, frankly, you made a fool of yourself. The OP can be forgiven for ignorance on that front, but you were answering the question.

    Quote Originally Posted by SirPrattlepod View Post
    Where, exactly, have you taught (or tried to teach) the OP anything at all? *confused*
    Elysia did fine in terms of supplying suggestions - in mostly natural language - about what was needed. The OP would need to think a little, but will learn significantly more of use by responding to Elysia's hints than by simply copy/pasting the code you supplied.

    Anyone who has spent time helping beginners knows that people learn more if they THINK through a problem, and learn almost nothing of use if someone just gives them the code they ask for.

    The fact that the OP doesn't want to think through the problem does not excuse you just supplying the code as you did. The fact you provided very poor code just makes your mistake even worse.

    And, by persisting in arguing otherwise, you are showing yourself up as an arrogant prat.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  13. #58
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by grumpy View Post
    Most C code can be compiled as C++, but a lot of C techniques - including ones you used - are considered very poor practice in C++. By using such code in a C++ forum, frankly, you made a fool of yourself. The OP can be forgiven for ignorance on that front, but you were answering the question.
    LOL. I simply used the OP's code.

    Edit: Besides, if you bothered to read the thread you'd realise that we're no longer talking about my crappy code.
    Last edited by SirPrattlepod; 08-22-2013 at 06:53 AM.

  14. #59
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by grumpy View Post
    The fact that the OP doesn't want to think through the problem does not excuse you just supplying the code as you did.
    Well to be fair, OP did post a version using std::vector a page or so back.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  15. #60
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Quote Originally Posted by SirPrattlepod View Post
    LOL. I simply used the OP's code.
    You are making a fool of yourself again.
    The OP is a newbie. You are not. You are supposed to show how it is properly done instead of copy and pasting some code that you know is not good code. You are supposed to steer newbies to modern C++, not some mix-mash old C.

    @jeedi khan: I would (and others, no doubt) still want to look at your vector code, but without that proper indentation, it's just too horrible to read. Do you think you can fix the indentation?
    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.

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R problem !! String concatenation :(
    By karanmitra in forum C Programming
    Replies: 9
    Last Post: 08-18-2005, 06:20 AM
  2. printing arrays with concatenation
    By derek23 in forum C Programming
    Replies: 1
    Last Post: 07-17-2005, 04:02 AM
  3. concatenation
    By rose2626 in forum C++ Programming
    Replies: 10
    Last Post: 04-25-2003, 02:27 PM
  4. concatenation
    By F*SH in forum C++ Programming
    Replies: 34
    Last Post: 11-13-2002, 06:47 PM
  5. Concatenation in C++
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2001, 01:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21