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; vector works with any type*. It's a generic dynamic array. So switch your dangerous C arrays to vectors and it ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    vector works with any type*. It's a generic dynamic array.
    So switch your dangerous C arrays to vectors and it will work.
    *) Well, almost, but don't worry about that. It works for ints too.
    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.

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    6,439
    Quote Originally Posted by jeedi khan View Post
    That's EASY to say in Words but a bit Difficult to EXPRESS in COde...
    It is trivial to express in code. If you can't translate my description into code, you're not trying hard enough.

    Quote Originally Posted by jeedi khan View Post
    here newsize is not an int as it refers to the value being generated dynamically during run...So it will return that the array has not a constant value and doesn't compile successfuly.
    So why is your original code using operator new? operator new performs dynamic memory allocation, with a length determined at RUN TIME.


    Quote Originally Posted by Elysia View Post
    Come on, people. Stop being bastards and start helping.
    While it is true I told the OP off for not trying, I also gave complete advice. Deliberately.

    There is the problem jeedi khan would rather cry to be spoon-fed with code, than TRY to produce code from a description. What s/he hasn't realised is that the effort for him/her to produce the code from the description is less than the effort s/he has already expended trying to get somebody to do that task.
    rags_to_riches likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Quote Originally Posted by grumpy View Post
    While it is true I told the OP off for not trying, I also gave complete advice. Deliberately.
    Sorry, it wasn't aimed at you, grumpy. It was aimed at SirPrattlepod who contributed nothing but almost turning the thread into a flame war. Should have been more explicit.
    jeedi khan likes this.
    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.

  4. #19
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    There is the problem jeedi khan would rather cry to be spoon-fed with code, than TRY to produce code from a description.
    Because Adak pretty much did that over in the C forum for him.

  5. #20
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Elysia View Post
    Sorry, it wasn't aimed at you, grumpy. It was aimed at SirPrattlepod who contributed nothing but almost turning the thread into a flame war. Should have been more explicit.
    I did not intend to try and start a flamewar. My contribution was that in my experience some learning/teaching institutions actually do use C++ as their "language" and then go about teaching things the "C way". This wasn't aimed at the OP either... I'm sorry if it sounded that way. I did use his source code as an example of "context" after the OP asked, but that was only for convenience (the approach being taken is more "c like" than "c++ like"). So, in summary, I apologise to the OP and the contributors to this thread; my comments were an observation only and not directly related to the OP or this thread.

  6. #21
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by rags_to_riches View Post
    As far as i took the purpose of this foum is to give a direction to the existing work one done on a program ....if one is trying and he is not getting so the right direction to him be sought...Adak did that for me and thats the purpose but if you/someone else are not willing to do so ,so Kindly don't indulge in unnecessary details about others.This is the Problem i observed here with SirPrattlepod too..ONE SHOULD NOT beat about the bush if s/he is not willing to guide others..Infact i tries myself first and again and again but when unable to do so then post here...

  7. #22
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by grumpy View Post
    It is trivial to express in code. If you can't translate my description into code, you're not trying hard enough.



    So why is your original code using operator new? operator new performs dynamic memory allocation, with a length determined at RUN TIME.





    While it is true I told the OP off for not trying, I also gave complete advice. Deliberately.

    There is the problem jeedi khan would rather cry to be spoon-fed with code, than TRY to produce code from a description. What s/he hasn't realised is that the effort for him/her to produce the code from the description is less than the effort s/he has already expended trying to get somebody to do that task.
    I am asking for int arrays instead vectors/strings...not to be spoon-fed ..if description regarding int arrays is given i would proceed on..vectors can do that i know better but requires here is int arrays...

  8. #23
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Ok. Penance. I don't really know what it does though.

    Code:
    // crc code.cpp : Defines the entry point for the console application.
    //
    
    #include <iostream>
    //#include <conio.h>    // OS-Specific include. Removed.
    //#include <malloc.h>   // Not needed for new/delete (they are operators)
    
    //#include"stdafx.h"
    
    //#define size len;     // Note the semi-colon... it should not be here
                            // The macro isn't used anyway, so commented it out
    
    #include <stdio.h>
                            
    using namespace std;
    
    // Formatted using astyle -A4 -U -p -f filename.cpp
    
    int main(void)
    {
        int message[10], i, lim_fcs, n, tot_len;
        int pattern[50], msg_len, patt_len, len;
        
        cout << "Enter the number of bits for message length(length should be "
                "within 10 bits):" << endl;
        cin >> n;
        cout << "Enter the message to send:" << endl;
    
        /* Should add error checking here/above to ensure that n is <= 10
         * or better still have 10 as a #define or const, or check that n is
         * <= sizeof message / sizeof message[0]
         */
        
        for(i = 0; i < n; i++) {
            cin >> message[i];
        }
        
        cout << "The message to send  is:" << endl;
        for(i = 0; i < n; i++) {
            cout << message[i];
            cout << "\t";
        }
        cout << "\n";
        
        msg_len = n;
        cout << "Enter the limit for FCS:" << endl;
        cin >> lim_fcs;
        
        // The message below is wrong... it should be within 50
        cout << "Enter the number of bits for Pattern(limit to be "
                "within 10 bits)" << endl; // pattern bits taken here
        cin >> patt_len;
        
        /* Should add error checking here/above to ensure that patt_len is <= 50
         * or better still have 50 as a #define or const, or check that patt_len is
         * <= sizeof pattern / sizeof pattern[0]
         */
        
        cout << "Enter the pattern bits:" << endl;
        for(i = 0; i < patt_len; i++) {
            cin >> pattern[i];
        }
    
        cout << "The pattern is:\n";
        for(i = 0; i < patt_len; i++) {
            cout << pattern[i];
            cout << "\t";
        }
        cout << endl;   // Since you used '\n' above maybe replace this with '\n'
                        // or change the prior usage to cout << endl; Consistency
                        // is good
                        
        tot_len = msg_len + lim_fcs;
        len = tot_len - patt_len;
            
        int *a = new int[len]();    // Array of zeroes
    
        cout << "The array is:\n";
        for(int j = 0; j < len; j++) {
            cout <<  a[j];
            cout << "\t";
        }
        cout << endl;
    
        int newsize = len + patt_len;
        
        // NEED here is an array patternnew[newsize] having elements of pattern[]
        // followed by zeros of a[]....
    
        int *foo = new int[newsize];
        int  pos = 0;
        for (int j = 0; j < patt_len; j++, pos++) {
            foo[pos] = pattern[j];
        }
        for (int j = 0; j < len; j++, pos++) {
            foo[pos] = a[j];
        }
       
        cout << "foo is:\n";
        for(i = 0; i < newsize; i++) {
            cout << foo[i];
            cout << "\t";
        }
        cout << endl;
        
        delete [] a;
        delete [] foo;
        
        getchar();
        
        return 0;
    }
    [/quote]
    jeedi khan likes this.

  9. #24
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by SirPrattlepod View Post
    Ok. Penance. I don't really know what it does though.

    Code:
    // crc code.cpp : Defines the entry point for the console application.
    //
    
    #include <iostream>
    //#include <conio.h>    // OS-Specific include. Removed.
    //#include <malloc.h>   // Not needed for new/delete (they are operators)
    
    //#include"stdafx.h"
    
    //#define size len;     // Note the semi-colon... it should not be here
                            // The macro isn't used anyway, so commented it out
    
    #include <stdio.h>
                            
    using namespace std;
    
    // Formatted using astyle -A4 -U -p -f filename.cpp
    
    int main(void)
    {
        int message[10], i, lim_fcs, n, tot_len;
        int pattern[50], msg_len, patt_len, len;
        
        cout << "Enter the number of bits for message length(length should be "
                "within 10 bits):" << endl;
        cin >> n;
        cout << "Enter the message to send:" << endl;
    
        /* Should add error checking here/above to ensure that n is <= 10
         * or better still have 10 as a #define or const, or check that n is
         * <= sizeof message / sizeof message[0]
         */
        
        for(i = 0; i < n; i++) {
            cin >> message[i];
        }
        
        cout << "The message to send  is:" << endl;
        for(i = 0; i < n; i++) {
            cout << message[i];
            cout << "\t";
        }
        cout << "\n";
        
        msg_len = n;
        cout << "Enter the limit for FCS:" << endl;
        cin >> lim_fcs;
        
        // The message below is wrong... it should be within 50
        cout << "Enter the number of bits for Pattern(limit to be "
                "within 10 bits)" << endl; // pattern bits taken here
        cin >> patt_len;
        
        /* Should add error checking here/above to ensure that patt_len is <= 50
         * or better still have 50 as a #define or const, or check that patt_len is
         * <= sizeof pattern / sizeof pattern[0]
         */
        
        cout << "Enter the pattern bits:" << endl;
        for(i = 0; i < patt_len; i++) {
            cin >> pattern[i];
        }
    
        cout << "The pattern is:\n";
        for(i = 0; i < patt_len; i++) {
            cout << pattern[i];
            cout << "\t";
        }
        cout << endl;   // Since you used '\n' above maybe replace this with '\n'
                        // or change the prior usage to cout << endl; Consistency
                        // is good
                        
        tot_len = msg_len + lim_fcs;
        len = tot_len - patt_len;
            
        int *a = new int[len]();    // Array of zeroes
    
        cout << "The array is:\n";
        for(int j = 0; j < len; j++) {
            cout <<  a[j];
            cout << "\t";
        }
        cout << endl;
    
        int newsize = len + patt_len;
        
        // NEED here is an array patternnew[newsize] having elements of pattern[]
        // followed by zeros of a[]....
    
        int *foo = new int[newsize];
        int  pos = 0;
        for (int j = 0; j < patt_len; j++, pos++) {
            foo[pos] = pattern[j];
        }
        for (int j = 0; j < len; j++, pos++) {
            foo[pos] = a[j];
        }
       
        cout << "foo is:\n";
        for(i = 0; i < newsize; i++) {
            cout << foo[i];
            cout << "\t";
        }
        cout << endl;
        
        delete [] a;
        delete [] foo;
        
        getchar();
        
        return 0;
    }
    [/QUOTE]


    Thanks
    I got it now.Infact the contents from line 90 to 104 were indeed i required....

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Quote Originally Posted by SirPrattlepod View Post
    Ok. Penance. I don't really know what it does though
    This is called spoon-feeding, and it is not a good thing. You learn so much more by finding the answer yourself than getting it handed to you.
    Furthermore, this code is garbage, and if I ever saw something like that in my code, I'd throw it out without second thought.
    Big dislike.

    Vectors rule, and that is all I have to say on the matter. If the OP cannot be bothered to even experiment with them, then he/she has a sad future ahead. Not as a programmer.
    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. #26
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by Elysia View Post
    This is called spoon-feeding, and it is not a good thing. You learn so much more by finding the answer yourself than getting it handed to you.
    Furthermore, this code is garbage, and if I ever saw something like that in my code, I'd throw it out without second thought.
    Big dislike.

    Vectors rule, and that is all I have to say on the matter. If the OP cannot be bothered to even experiment with them, then he/she has a sad future ahead. Not as a programmer.

    Cool....
    I am working on Vectors too...Learning now insert(),erase(),push_back() bla bla...

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Then I am going to suggest you rewrite your program using vectors. It will be good learning experience
    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. #28
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Elysia View Post
    This is called spoon-feeding, and it is not a good thing. You learn so much more by finding the answer yourself than getting it handed to you.
    Furthermore, this code is garbage, and if I ever saw something like that in my code, I'd throw it out without second thought.
    Big dislike.

    Vectors rule, and that is all I have to say on the matter. If the OP cannot be bothered to even experiment with them, then he/she has a sad future ahead. Not as a programmer.
    Did I say it was good? Did I say it was a good way to do it (I already said it was CRAP and got accused of trying to start a flamewar). Furthermore, it's not my code I just added a couple of lines and some comments regarding the OPs code. So, we have come full circle and you reveal your true colours -- I was correct in the first place.

  14. #29
    Registered User
    Join Date
    Oct 2006
    Posts
    2,503
    Quote Originally Posted by SirPrattlepod View Post
    Furthermore, it's not my code I just added a couple of lines and some comments regarding the OPs code.
    but the point is that you gave the OP a full answer to the problem at hand. this forum has very clear rules about this.

    Quote Originally Posted by SirPrattlepod View Post
    So, we have come full circle and you reveal your true colours -- I was correct in the first place.
    the accuracy of this statement is highly dubious. Elysia can be pretty tough on people sometimes, but she does it for their benefit. she knows what she's talking about, and most importantly, she understands the forum rules. I suggest you learn them too.

    you're a very new user here, so my recommendation is that you watch and pay attention to how the regulars conduct themselves here, and try to follow that example.
    Code:
    namespace life
    {
        const bool change = true;
    }

  15. #30
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Elkvis View Post
    but the point is that you gave the OP a full answer to the problem at hand. this forum has very clear rules about this.
    I disagree strongly. The thread was going nowhere. I could have given a response in pseudocode I guess, or given subtle hints, or other crazy stuff, but at the end of the day writing it in code is equivalent. In my defence my "answer" is the same as if I'd given an English description, a mathematical description, a pseudocode description, or whatever.


    the accuracy of this statement is highly dubious. Elysia can be pretty tough on people sometimes, but she does it for their benefit. she knows what she's talking about, and most importantly, she understands the forum rules. I suggest you learn them too.
    It's not inaccurate. They (Elysia) attacked me by saying my previous answers were non-constructive. They implied that I was saying the OPs code was crap (which I never did). I then added, what... 8?, lines plus a few comments to the OPs source code and then Elysia insulted the OP by saying their source code was complete and utter rubbish? Something doesn't add up here. I don't care if Elysia has 1 post or a million posts I'll be honest every time, even if the honesty seems brutal. Live with it.

    you're a very new user here, so my recommendation is that you watch and pay attention to how the regulars conduct themselves here, and try to follow that example.
    To be honest I'm not overly impressed with "the regulars" and their conduct. Should I quiver in fear and follow like a lost lamb? Nah. Of course not. How new I am or how "old" they are is not relevant. If I think their example is wrong I'll say so and not just bow to their experience... that'd be a bit silly.

    Edit: I do realise your post was written with the best of intentions and I thank you for that. But, please, do not suggest to me how I should conduct myself.
    Last edited by SirPrattlepod; 08-21-2013 at 07:00 AM.

Page 2 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, 05:20 AM
  2. printing arrays with concatenation
    By derek23 in forum C Programming
    Replies: 1
    Last Post: 07-17-2005, 03:02 AM
  3. concatenation
    By rose2626 in forum C++ Programming
    Replies: 10
    Last Post: 04-25-2003, 01:27 PM
  4. concatenation
    By F*SH in forum C++ Programming
    Replies: 34
    Last Post: 11-13-2002, 05:47 PM
  5. Concatenation in C++
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2001, 12:05 PM

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