| | |
just a little help needed
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2005
Posts: 31
Reputation:
Solved Threads: 0
#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.
#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.
•
•
Join Date: Nov 2005
Posts: 31
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by 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
C++ Syntax (Toggle Plain Text)
;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"; }
•
•
Join Date: Oct 2005
Posts: 6
Reputation:
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]; // 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";
}
{ 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";
}
•
•
Join Date: Nov 2005
Posts: 31
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
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"; }
•
•
Join Date: Nov 2005
Posts: 31
Reputation:
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.
•
•
Join Date: Oct 2005
Posts: 6
Reputation:
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<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]<<" ";
}
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]<<" ";
}
![]() |
Similar Threads
- Harddrive failure and 100% CPU under Win2K, Help Needed! (Windows NT / 2000 / XP)
- XP PRO iis help needed (Windows NT / 2000 / XP)
- CWS. help needed (Viruses, Spyware and other Nasties)
- are all these needed (Windows NT / 2000 / XP)
- help much needed !! (OS X)
Other Threads in the C++ Forum
- Previous Thread: What exactly is C++?
- Next Thread: Need major help
Views: 2040 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





