# Thread: merging arrays using pointers help!!!

1. ## 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. can anyone help because i still cant get any better than the code i posted

3. 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. sorry about that boys but it is all right because i figured it out