954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

just a little help needed

#include
#include
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 "<>m>>n;
cout<<" Enter the elements of first array "<>ar1[i];

cout<<" Enter the elements of second array "<>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;iarray[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;iar2[i])
ar3[i]=ar2[i];
else ar3[i]=ar1[i];
}
for(int k=n;kar2[i])
ar3[i]=ar2[i];
else ar3[i]=ar1[i];
}
for(int k=m;k

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

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

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
It is very difficult to read ur code...plz use code tags

[CODE]
#include
#include
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 "<>m>>n;
cout<<" Enter the elements of first array "<>ar1[i];

cout<<" Enter the elements of second array "<>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;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";
}

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

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

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

void mergearray(int ar1[15],int ar2[15], int ar3[30] , int m ,int n)
{ if(m>n)
{
for(int i=0;iar2[i])
ar3[i]=ar2[i];
else ar3[i]=ar1[i];
}
for(int k=n;kar2[i])
ar3[i]=ar2[i];
else ar3[i]=ar1[i];
}
for(int k=m;k

mooni
Newbie Poster
6 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 
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

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

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}

mooni
Newbie Poster
6 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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.

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

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
#include
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 "<>m>>n;
cout<<" Enter the elements of first array "<>ar1[i];

cout<<" Enter the elements of second array "<>ar2[j];

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

cout << "sorted ar1: ";
for(i = 0;iarray[j])
{temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
}
void mergearray(int* ar1,int* ar2, int* ar3 , int m ,int n)
{
if(m>n)
{

for(int i=0;im)
{
for(int i=0;i to <= or >=
ar3[i]=ar2[i];

for(int k=n;k

mooni
Newbie Poster
6 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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

mooni
Newbie Poster
6 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 
  • Don't use . You should use this with the new standard:

    #include <iostream>
    using namespace std;
  • Ditto with getch(). It's non-standard.
  • main() returnsint. And it should also return 0; while you're at it.
  • 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. :)

dwks
Posting Whiz in Training
269 posts since Nov 2005
Reputation Points: 185
Solved Threads: 28
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You