#include<iostream.h>
#include<conio.h>
int main()
{
void sortarray(int array[15],int size);
void mergearray(int ar1[15], int ar2[15], int ar3[30],int m ,int n);
int m, n ,ar1[15],ar2[15],ar3[30],array[15],size;
cout<<" Enter the size of the arrays "<<endl;
cin>>m>>n;
cout<<" Enter the elements of first array "<<endl;
for(int i=0;i<m;i++)
cin>>ar1;


cout<<" Enter the elements of second array "<<endl;
for(int j=0;j<n;j++)
cin>>ar2[j];


sortarray(ar1,m);
sortarray(ar2,n);
mergearray(ar1,ar2,ar3,m,n);
getch();
}//****************End of main


void sortarray(int array[15],int size)
{
int temp=0;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size+1;j++)
{
if(array>array[j])
{temp=array;
array=array[j];
array[j]=temp;
}
}
}
}
void mergearray(int ar1[15],int ar2[15], int ar3[30] , int m ,int n)
{  if(m>n)
{
for(int i=0;i<n;i++)
{
if(ar1>ar2)
ar3=ar2;
else ar3=ar1;
}
for(int k=n;k<m;k++)
ar3[n]=ar1[n];
}
else
{
for(int i=0;i<m;i++)
{
if(ar1>ar2)
ar3=ar2;
else ar3=ar1;
}
for(int k=m;k<n;k++)
ar3[m]=ar1[m];
}


for(int k=0;k<m+n;k++)
cout<<ar3[k]<<"\t";
}

not getting the correct output .
i am basically trying to make a prog to accept the elements of two arrays then merging the two arrays and getting it sorted .
then printing it.there are no compile errors and the second array gets sorted while after that wat i get is a bunch of arbitrary values. plz indiate the faults.

Recommended Answers

All 10 Replies

It is very difficult to read ur code...plz use code tags

sunnypalsingh - It is very difficult to read ur code...plz use code tags

#include<iostream.h>
#include<conio.h>
int main()
{
    void sortarray(int array[15],int size);
    void mergearray(int ar1[15], int ar2[15], int ar3[30],int m ,int n);
    int m, n ,ar1[15],ar2[15],ar3[30],array[15],size;
    cout<<" Enter the size of the arrays "<<endl;
    cin>>m>>n;
    cout<<" Enter the elements of first array "<<endl;
    for(int i=0;i<m;i++)
    cin>>ar1[i];

    cout<<" Enter the elements of second array "<<endl;
    for(int j=0;j<n;j++)
    cin>>ar2[j];

    sortarray(ar1,m);
    sortarray(ar2,n);
    mergearray(ar1,ar2,ar3,m,n);
    getch();
}//****************End of main

void sortarray(int array[15],int size)
{
     int temp=0;
     for(int i=0;i<size;i++)
     {
             for(int j=i+1[CODE];j<size+1;j++)
             {
                     if(array[i]>array[j])
                     {temp=array[i];
                      array[i]=array[j];
                      array[j]=temp;
                     }
             }
     }
}
void mergearray(int ar1[15],int ar2[15], int ar3[30] , int m ,int n)
{  if(m>n)
   {
     for(int i=0;i<n;i++)
     {
             if(ar1[i]>ar2[i])
             ar3[i]=ar2[i];
             else ar3[i]=ar1[i];
     }
     for(int k=n;k<m;k++)
     ar3[n]=ar1[n];
    }
    else
    {
      for(int i=0;i<m;i++)
      {
             if(ar1[i]>ar2[i])
             ar3[i]=ar2[i];
             else ar3[i]=ar1[i];
      }
      for(int k=m;k<n;k++)
      ar3[m]=ar1[m];
    }

     for(int k=0;k<m+n;k++)
     cout<<ar3[k]<<"\t";
}

how to make it more readable . if it is tags how to do it?? new to forums

void mergearray(int ar1[15],int ar2[15], int ar3[30] , int m ,int n)
{  if(m>n)
{
for(int i=0;i<n;i++)
{
if(ar1>ar2)
ar3=ar2;
else ar3=ar1;
}
for(int k=n;k<m;k++)
ar3[k]=ar1[k];              // it must be k
}
else
{
for(int i=0;i<m;i++)
{
if(ar1>ar2)
ar3=ar2;
else ar3=ar1;
}
for(int k=m;k<n;k++)
ar3[k]=ar1[k];            // it must be k
}


for(int k=0;k<m+n;k++)
cout<<ar3[k]<<"\t";
}
void mergearray(int ar1[15],int ar2[15], int ar3[30] , int m ,int n)
{  if(m>n)
   {
     for(int i=0;i<=n;i++)
     {
             if(ar1[i]>ar2[i])
             ar3[i]=ar2[i];
             else ar3[i]=ar1[i];
     }
     for(int k=n;k<=m;k++)
     ar3[k]=ar1[k];
    }
    else if(n>m)
    {
      for(int i=0;i<=m;i++)
      {
             if(ar1[i]>ar2[i])
             ar3[i]=ar2[i];
             else ar3[i]=ar1[i];
      }
      for(int k=m;k<=n;k++)
      ar3[k]=ar1[k];
    }
    if(m==n)
    {for(int i=0;i<=n;i++)//made all the very strict conditions < or > to <= or >=
     {
             if(ar1[i]>=ar2[i])
             ar3[i]=ar2[i];
             else ar3[i]=ar1[i];
     }
     for(int k=n;k<=m;k++)
     ar3[k]=ar1[k];
    }
            
    
     for(int k=0;k<m+n;k++)
     cout<<ar3[k]<<"\t";
}

but still not getting the correct output. now first array gets sorted but again arbitrary values for second array

My dear just tell me what u actually want to do.
what i've understood yet is that u want user to input two arrays then merge them and get the merged array sorted
like
a1 = {2,3,1,4}
a2 = {7,5,8,6}

what u want in output is
a3 = {1,2,3,4,5,6,7,8}
a1 = {2,3,1,4}
a2 = {7,5,8,6}

yes that is wat i want to do but i have realised that wat if in both the arrays (at the same location) same value is present . so i have included one extra check .m==n; .moreover the code is compiling without error but it is not giving the desired sorted array instead it sorts one of the array while i get some funny , big values or zeros for the second array.

i believe u'll love it................
here is the correct code....... but u r requested to pray for me....
May Allah bless all of us

#include<iostream.h>
#include<conio.h>
void sortarray(int* array,int size);
void mergearray(int* ar1, int* ar2, int* ar3,int m ,int n);
void main()
{


int m, n ,ar1[15],ar2[15],ar3[30];
cout<<" Enter the size of the arrays "<<endl;
cin>>m>>n;
cout<<" Enter the elements of first array "<<endl;
for(int i=0;i<m;i++)
cin>>ar1;


cout<<" Enter the elements of second array "<<endl;
for(int j=0;j<n;j++)
cin>>ar2[j];


sortarray(ar1,m);
sortarray(ar2,n);


cout << "sorted ar1:  ";
for(i = 0;i<m;i++)
cout << ar1 << " ";
cout << "\nsorted Array2: ";
for (i = 0; i<n;i++)
cout << ar2 << " ";
cout << endl;
mergearray(ar1,ar2,ar3,m,n);
getch();
}//****************End of main


void sortarray(int* array,int size)
{
int temp=0;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(array>array[j])
{temp=array;
array=array[j];
array[j]=temp;
}
}
}
}
void mergearray(int* ar1,int* ar2, int* ar3 , int m ,int n)
{
if(m>n)
{


for(int i=0;i<n;i++)
ar3=ar2;
for(int k=n;k<m+n;k++)
ar3[k]=ar1[k-n];
}
else if(n>m)
{
for(int i=0;i<m;i++)
ar3=ar2;
for(int k=m;k<n+m;k++)
ar3[k]=ar1[k-m];
}
if(m==n)
{
for(int i=0;i<n;i++)//made all the very strict conditions < or > to <= or >=
ar3=ar2;


for(int k=n;k<m+n;k++)
ar3[k]=ar1[k-n];
}
cout << "\nUnsorted Array3: ";
for(int i=0;i < m+n;i++)
cout << ar3 << " ";
sortarray(ar3,m+n);
cout << "\nsorted Array3: ";
for(int k=0;k<m+n;k++)
cout<<ar3[k]<<" ";
}

if u need any further help do tell me.....always there

  1. Don't use <iostream.h>. You should use this with the new standard:
    #include <iostream>
    using namespace std;
  2. Ditto with getch(). It's non-standard.
  3. main() returns int. And it should also return 0; while you're at it.
  4. Don't rely on variables declared in for loops ( for(int x = 0; ... ) to be visible throughout the rest of the block. That is not part of the standard. Just re-declare the variable in all for loops, or at the top of the function or something.

And work on your indentation. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.