944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2214
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 22nd, 2005
0

just a little help needed

Expand Post »
#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;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";
}

// 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sahil_logic is offline Offline
31 posts
since Nov 2005
Nov 22nd, 2005
0

Re: just a little help needed

It is very difficult to read ur code...plz use code tags
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Nov 22nd, 2005
0

Re: just a little help needed

Quote originally posted by sunnypalsingh ...
It is very difficult to read ur code...plz use code tags
[CODE]
#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
C++ Syntax (Toggle Plain Text)
  1. ;j<size+1;j++)
  2. {
  3. if(array[i]>array[j])
  4. {temp=array[i];
  5. array[i]=array[j];
  6. array[j]=temp;
  7. }
  8. }
  9. }
  10. }
  11. void mergearray(int ar1[15],int ar2[15], int ar3[30] , int m ,int n)
  12. { if(m>n)
  13. {
  14. for(int i=0;i<n;i++)
  15. {
  16. if(ar1[i]>ar2[i])
  17. ar3[i]=ar2[i];
  18. else ar3[i]=ar1[i];
  19. }
  20. for(int k=n;k<m;k++)
  21. ar3[n]=ar1[n];
  22. }
  23. else
  24. {
  25. for(int i=0;i<m;i++)
  26. {
  27. if(ar1[i]>ar2[i])
  28. ar3[i]=ar2[i];
  29. else ar3[i]=ar1[i];
  30. }
  31. for(int k=m;k<n;k++)
  32. ar3[m]=ar1[m];
  33. }
  34.  
  35. for(int k=0;k<m+n;k++)
  36. cout<<ar3[k]<<"\t";
  37. }
Reputation Points: 10
Solved Threads: 0
Light Poster
sahil_logic is offline Offline
31 posts
since Nov 2005
Nov 22nd, 2005
0

Re: just a little help needed

how to make it more readable . if it is tags how to do it?? new to forums
Reputation Points: 10
Solved Threads: 0
Light Poster
sahil_logic is offline Offline
31 posts
since Nov 2005
Nov 22nd, 2005
0

Re: just a little help needed

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]; // it must be k
}
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[k]=ar1[k]; // it must be k
}

for(int k=0;k<m+n;k++)
cout<<ar3[k]<<"\t";
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mooni is offline Offline
6 posts
since Oct 2005
Nov 22nd, 2005
0

Re: just a little help needed

C++ Syntax (Toggle Plain Text)
  1. void mergearray(int ar1[15],int ar2[15], int ar3[30] , int m ,int n)
  2. { if(m>n)
  3. {
  4. for(int i=0;i<=n;i++)
  5. {
  6. if(ar1[i]>ar2[i])
  7. ar3[i]=ar2[i];
  8. else ar3[i]=ar1[i];
  9. }
  10. for(int k=n;k<=m;k++)
  11. ar3[k]=ar1[k];
  12. }
  13. else if(n>m)
  14. {
  15. for(int i=0;i<=m;i++)
  16. {
  17. if(ar1[i]>ar2[i])
  18. ar3[i]=ar2[i];
  19. else ar3[i]=ar1[i];
  20. }
  21. for(int k=m;k<=n;k++)
  22. ar3[k]=ar1[k];
  23. }
  24. if(m==n)
  25. {for(int i=0;i<=n;i++)//made all the very strict conditions < or > to <= or >=
  26. {
  27. if(ar1[i]>=ar2[i])
  28. ar3[i]=ar2[i];
  29. else ar3[i]=ar1[i];
  30. }
  31. for(int k=n;k<=m;k++)
  32. ar3[k]=ar1[k];
  33. }
  34.  
  35.  
  36. for(int k=0;k<m+n;k++)
  37. cout<<ar3[k]<<"\t";
  38. }
but still not getting the correct output. now first array gets sorted but again arbitrary values for second array
Reputation Points: 10
Solved Threads: 0
Light Poster
sahil_logic is offline Offline
31 posts
since Nov 2005
Nov 22nd, 2005
0

Re: just a little help needed

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}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mooni is offline Offline
6 posts
since Oct 2005
Nov 22nd, 2005
0

Re: just a little help needed

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sahil_logic is offline Offline
31 posts
since Nov 2005
Nov 23rd, 2005
0

Re: just a little help needed

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[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);

cout << "sorted ar1: ";
for(i = 0;i<m;i++)
cout << ar1[i] << " ";
cout << "\nsorted Array2: ";
for (i = 0; i<n;i++)
cout << ar2[i] << " ";
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[i]>array[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;i<n;i++)
ar3[i]=ar2[i];
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[i]=ar2[i];
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[i]=ar2[i];

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[i] << " ";
sortarray(ar3,m+n);
cout << "\nsorted Array3: ";
for(int k=0;k<m+n;k++)
cout<<ar3[k]<<" ";
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mooni is offline Offline
6 posts
since Oct 2005
Nov 23rd, 2005
0

Re: just a little help needed

if u need any further help do tell me.....always there
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mooni is offline Offline
6 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: What exactly is C++?
Next Thread in C++ Forum Timeline: Need major help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC