just a little help needed

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2005
Posts: 31
Reputation: sahil_logic is an unknown quantity at this point 
Solved Threads: 0
sahil_logic sahil_logic is offline Offline
Light Poster

just a little help needed

 
0
  #1
Nov 22nd, 2005
#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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: just a little help needed

 
0
  #2
Nov 22nd, 2005
It is very difficult to read ur code...plz use code tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 31
Reputation: sahil_logic is an unknown quantity at this point 
Solved Threads: 0
sahil_logic sahil_logic is offline Offline
Light Poster

Re: just a little help needed

 
0
  #3
Nov 22nd, 2005
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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 31
Reputation: sahil_logic is an unknown quantity at this point 
Solved Threads: 0
sahil_logic sahil_logic is offline Offline
Light Poster

Re: just a little help needed

 
0
  #4
Nov 22nd, 2005
how to make it more readable . if it is tags how to do it?? new to forums
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 6
Reputation: mooni is an unknown quantity at this point 
Solved Threads: 0
mooni mooni is offline Offline
Newbie Poster

Re: just a little help needed

 
0
  #5
Nov 22nd, 2005
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";
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 31
Reputation: sahil_logic is an unknown quantity at this point 
Solved Threads: 0
sahil_logic sahil_logic is offline Offline
Light Poster

Re: just a little help needed

 
0
  #6
Nov 22nd, 2005
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 6
Reputation: mooni is an unknown quantity at this point 
Solved Threads: 0
mooni mooni is offline Offline
Newbie Poster

Re: just a little help needed

 
0
  #7
Nov 22nd, 2005
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}
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 31
Reputation: sahil_logic is an unknown quantity at this point 
Solved Threads: 0
sahil_logic sahil_logic is offline Offline
Light Poster

Re: just a little help needed

 
0
  #8
Nov 22nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 6
Reputation: mooni is an unknown quantity at this point 
Solved Threads: 0
mooni mooni is offline Offline
Newbie Poster

Re: just a little help needed

 
0
  #9
Nov 23rd, 2005
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]<<" ";
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 6
Reputation: mooni is an unknown quantity at this point 
Solved Threads: 0
mooni mooni is offline Offline
Newbie Poster

Re: just a little help needed

 
0
  #10
Nov 23rd, 2005
if u need any further help do tell me.....always there
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2040 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC