Thread: merging arrays using pointers help!!!

  1. #1
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Question merging arrays using pointers help!!!

    here is the original code it works but it must use pointers so it is not considered correct help?
    int array(int a[], int na){
    for(int ia=0, ia<na;ia++){
    cout<<"Enter a number: ";
    cin>>a[ia];}
    return o;}
    int bubblesort(int a[], int na){
    for (int pass=0; pass<na; pass++){
    for(int ia=0;ia<na-1;ia++){
    if(a[ia]>a[ia+1]){
    int j=a[ia];
    a[ia]=a[ia+1];
    a[ia+1]=j;}}}
    return 0;}
    int merger(int a[],int b[],int c[],int na, intnb, int nc){
    int ia,ib,ic;
    ia=ib=ic=0;
    while(ia<&&ib<nb){
    if(a[ia]<b[ib])
    c[ic++]=a[ia++];
    else if(b[ib]<a[ia])
    c[ic++]=b[ib++];
    else{ c[ic++]=a[ia++];
    ib++;}}
    while(ia<na)
    c[ic++]=a[ia++];
    while(ib<nb)
    c[ic++]=b[ib++];
    return 0;}
    int main(){
    const int na=8;
    const int nb=12;
    const int nc=20;
    int a[na];
    int b[nb];
    int c[nc];
    array(a,na);
    cout<<"Here is array a: \n";
    bubblesort(,na);
    for (nt ia=0; ia<na;ia++){
    cout<<a[ia]<<" ";}
    array(b,nb);
    cout<<"Herer is array b: ";
    bubblesort(b,nb);
    for(int ib=0;ib<nb;ib++){
    cout<<b[ib]<< " ";}
    merger(a,b,c,na,nb,nc);
    cout<<"Herer is the final array: \n";
    for (int ic=0;ic<nc;ic++){
    if(c[ic]!=c[ic+1]){
    cout<<c[ic]<<" ";}}
    cout<<endl;
    return 0;}
    and here is what i tried and i am totally lost

    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    void array(int a[], int na){
    for(int ia=0; ia<na;ia++){
    cout<<"Enter a number: ";
    cin>>a[ia];}
    // return 0;
    }
    void bubblesort(int a[],int na){
    for(int pass=0; pass<na;pass++){
    for(int ia=0;ia<na-1;ia++){
    if(a[ia]>a[ia+1]){
    int j=a[ia];
    a[ia]=a[ia+1];
    a[ia+1]=j;}}}

    //return 0;
    }
    void merger(int *a[],int *b[],int *c[],int na,int nb,int nc){
    int ia,ib,ic;
    ia= ib= ic=0;
    while(ia<na&&ib<nb){
    if(*a[ia]<*b[ib])
    *c[ic++]=*a[ia++];
    else if(*b[ib]<*a[ia])
    *c[ic++]=*b[ib++];
    else{
    *c[ic++]=*a[ia++];
    ib++;}}
    while(ia<na)
    *c[ic++]=*a[ia++];
    while(ib<nb)
    *c[ic++]=*b[ib++];

    // return 0;
    }


    int main()
    {
    const int na=8;
    const int nb=12;
    const int nc=20;
    int a[na];
    int b[nb];
    int c[nc];
    cout<<"Enter numbers for array a: \n";
    array(a,na);
    cout<<"Here is array a: \n";
    bubblesort(a,na);
    for(int ia=0; ia<na;ia++){
    cout<<a[ia]<<" ";}
    cout<<"\nEnter numbers for array b: \n";
    array(b,nb);
    cout<<"Here is array b: \n";
    bubblesort(b,nb);
    for(int ib=0; ib<nb;ib++){
    cout<<b[ib]<<" ";}
    int *aptr=a[na];
    int *bptr=b[nb];
    merger(*aptr,*bptr,&c,na,nb,nc);
    cout<<"\nHere is the final merged array: \n";
    for( int ic=0;ic<nc;ic++){
    if(*c[ic]!=*c[ic+1]){
    cout<<*c[ic]<<" ";}}
    cout<<endl;
    return 0;
    }

  2. #2
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Cool

    can anyone help because i still cant get any better than the code i posted

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    I think the reason you're not getting any replies is because nobody can read your code. Try putting it in [ c o d e ] tags (but without the spaces of course) and use proper indentation so that people can read it.

    I assume you're trying to take two sorted arrays and create a third sorted array containing the contents of the first two? Look up "mergesort" on google. That ought to help.

  4. #4
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Lightbulb

    sorry about that boys but it is all right because i figured it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to 2D arrays
    By bertazoid in forum C Programming
    Replies: 16
    Last Post: 02-26-2009, 04:46 PM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. Using pointers instead of subscripts for arrays
    By Sir Andus in forum C Programming
    Replies: 7
    Last Post: 11-09-2006, 10:59 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM