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; Hi all!!! I am writing a program for CRC in C++ ...I want an int arrays conctaentaion i.e. i have ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    158

    Post Problem in concatenation of arrays

    Hi all!!!
    I am writing a program for CRC in C++ ...I want an int arrays conctaentaion i.e. i have created an array of zeros and now wanna add it to the last of an array pattern[]..how to achieve this ?The new array should be of the size newsize .
    Following is the code:

    Code:
    // crc code.cpp : Defines the entry point for the console application.
    //
    
    
    #include"stdafx.h"
    #include<iostream>
    #include<conio.h>
    #include<malloc.h>
    using namespace std;
    # define size len;
    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;
        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;
        cout<<"Enter the number of bits for Pattern(limit to be within 10 bits)"<<endl;// pattern bits taken here
        cin>>patt_len;
        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;
    
    
         tot_len=msg_len+lim_fcs;
         len=tot_len-patt_len;
         int *a;                                       //array for zeros here
         a=new int[len];
         for(int j=0;j<len;j++)
            {
                a[j]=0;
            }
    
         cout<<"The array is:\n";
          for(int j=0;j<len;j++)
            {
              cout<<  a[j];
              cout<<"\t";
            }
         int newsize =len+patt_len;
    //NEED here is an array patternnew[newsize] having elements of pattern[] followed by zeros of a[]....
        
        getch();
        return 0;
    }
    Last edited by jeedi khan; 08-19-2013 at 02:14 AM.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,160
    Since this is C++, if you want to be able to concatenate arrays, use strings or vectors instead.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by rcgldr View Post
    Since this is C++, if you want to be able to concatenate arrays, use strings or vectors instead.
    Would You Please elaborate using strings this WORK ?
    I am in dire NEED

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,649
    Yes it will Try it!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by std10093 View Post
    Yes it will Try it!
    Here are bits !!!

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    5,827
    Quote Originally Posted by jeedi khan View Post
    Would You Please elaborate using strings this WORK ?
    I am in dire NEED
    It's posts like this that make me hanker for a "Dislike" button. "Dire NEED" on your part is irrelevant. Effort on your part is relevant .... except that you have exhibited no effort.

    All you need is to allocate an array that is large enough to hold all elements needed. Once you have that, copy from the first array then from the second array.

    If you want to do it with std::string's, look up std::string's append method.
    Salem and rags_to_riches like this.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by grumpy View Post
    It's posts like this that make me hanker for a "Dislike" button. "Dire NEED" on your part is irrelevant. Effort on your part is relevant .... except that you have exhibited no effort.

    All you need is to allocate an array that is large enough to hold all elements needed. Once you have that, copy from the first array then from the second array.

    If you want to do it with std::string's, look up std::string's append method.
    I want to know about the Integer arrays instead ...How to get the int arrays combine this WAY.
    GUIDANCE will BE Appreciated..THANKS IN ADVANCE!!!

  8. #8
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    934
    Quote Originally Posted by jeedi khan View Post
    I want to know about the Integer arrays instead ...How to get the int arrays combine this WAY.
    GUIDANCE will BE Appreciated..THANKS IN ADVANCE!!!
    All the guidance you should need is in grumpys post.

    Quote Originally Posted by grumpy
    All you need is to allocate an array that is large enough to hold all elements needed. Once you have that, copy from the first array then from the second array.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  9. #9
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by Neo1 View Post
    All the guidance you should need is in grumpys post.
    That's EASY to say in Words but a bit Difficult to EXPRESS in COde...I will need to declare an array patternnew of size as
    Code:
    int patternnew[newsize]
    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.

  10. #10
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Another problem is that some "institutes of education" say they're teaching C++ when they're really teaching (bad) C.

  11. #11
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by SirPrattlepod View Post
    Another problem is that some "institutes of education" say they're teaching C++ when they're really teaching (bad) C.
    With Reference to Context?

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by jeedi khan View Post
    With Reference to Context?
    Context? Your source code.
    ZuK likes this.

  13. #13
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by SirPrattlepod View Post
    Context? Your source code.
    Feeling PITY for YOU now REALLY

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Come on, people. Stop being bastards and start helping.
    To create a dynamic array of variable size for some N determined at runtime, you can use std::vector.
    In fact, it's so flexible, it can do all you need to do.
    Take a look at: std::vector - cppreference.com
    If you still have problems, let us know.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by Elysia View Post
    Come on, people. Stop being bastards and start helping.
    To create a dynamic array of variable size for some N determined at runtime, you can use std::vector.
    In fact, it's so flexible, it can do all you need to do.
    Take a look at: std::vector - cppreference.com
    If you still have problems, let us know.
    what about int arrays instead vectors/strings?

Page 1 of 5 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