Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre lang="cs">// merge.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iomanip>
#include<iostream>
using namespace std;
class merge
{
public:
    void num();
private :
   int a[100], b[100],merc[100];
   int temp;
};
void merge:: num()
{
    int temp;
    int i=0 ,n,q, m=0;
    cout<<"enter the number of numbers :"<<endl;
    cin>>n;
    cout<<"plz enter  nums"<<endl;
      for(int i=0;i<n;i++)0
        cin>>a[i];
    for(int j=1 ; j<n ; j++)
            for(int i=0 ; i<n-j ; i++){
                   if(a[i]>a[i+1])
                   {
                       temp=a[i];
                       a[i]=a[i+1];
                       a[i+1]=temp;
                   }
}
    for(int i=0;i<n;i++)
        cout<<"num"<<i<<"="<<a[i]<<endl;
    cout<<"enter the number of numbers :"<<endl;
    cin>>q;
    cout<<"plz enter  nums"<<endl;
      for(int m=0;m<q;m++)
        cin>>b[m];
    for(int h=1 ; h<q ; h++)
            for(int m=0 ; m<q-h ; m++){
                   if(b[m]>b[m+1])
                   {
                       temp=b[m];
                       b[m]=b[m+1];
                       b[m+1]=temp;
                   }
}
            for(int m=0;m<q;m++)
                cout<<"num"<<m<<"="<<b[m]<<endl;
            int k=0;
            int result [100];
            while(k < n+q){
            if(a[i]<b[m]){
                result[k]=a[i];
                k++;
            }
            else
            {
                result [k]=b[m];
                k++;
            }
            }
            for(int i=0 ; i<q+n;i++){
                cout<<"yay"<<result[i]<<endl;}
}

int main()
{
    merge m;
    m.num();
    return 0;
}

Posted

Perhaps, in your while loop, you want to modify the input indexes, a bit? I have re-formatted it so it is a little clearer...
C#
while(k < n+q)
   {
   if(a[i]<b[m])
      {
      result[k]=a[i];
      k++;
      }
   else
      {
      result [k]=b[m];
      k++;
      }
   }
If "i" and "m" do not change, how is your loop supposed to work?
 
Share this answer
 
You badly need to learn naming. You're not only making life hard on yourself, as seen by the questions you ask here, but also reducing the chance of getting help from others.

a, b, c, h, i, k, n, m, q; Surely you can come up with better descriptions of your variables. There's no need to save bytes on code level today, and most editors have some form of IntelliSense to help you write code without the need of typing entire names if you find yourself too lazy.

Put a little effort into your coding.
 
Share this answer
 
Comments
Richard MacCutchan 20-Mar-11 11:28am    
Good comment. I see far too many examples like this; I just hope they are not working for my bank.
Niklas L 21-Mar-11 4:00am    
Thanks. Good programming is about being obvious.
Hi,

Seems you want to achieve something like:
C++
#include <vector>
#include<iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;

    std::vector<int> vals;
    int nVals = {0};
    do
    {
        cout << "Enter the number of values :" << endl;
        if (cin >> nVals && (nVals > 0))
        {
            cout <<"Enter " << nVals << " values:"<< endl;
            std::copy_n(std::istream_iterator<int>(cin), nVals, std::back_inserter(vals));
            std::sort(vals.end() - nVals, vals.end());
            cout << "Values: ";
            std::copy(vals.end() - nVals, vals.end(), std::ostream_iterator<int>(cout, " "));
            cout << endl;
        }
    } while (cin && (nVals > 0));

    std::sort(vals.begin(), vals.end());
    cout << "All sorted values: ";
    std::copy(vals.begin(), vals.end(), std::ostream_iterator<int>(cout, " "));
    cout << endl;

    return 0;
}


Use the Standard C++ Library containers and algorithms to avoid painful and error prone index handling, and miserable sort and merge attempts.
cheers,
AR
 
Share this answer
 
Comments
Emilio Garavaglia 20-Mar-11 17:00pm    
This solution is technically perfect, but considering the level of the OP, I doubt it can be any useful.
Not to mention that, if his goal is to learn about those algorithms, using "ready made ones" will not allow him to learn.
Alain Rist 20-Mar-11 18:43pm    
Considering the level of the OP the first step is: Hands up your keyboard, scratch your head and THINK :) That's my answer intent.
First of all: bring your code into a human readable form. That makes it easier to find errors.
I've done it for you:
void merge::num()
{
  int    temp;
  int    i, j, n, q, m, h;

  cout << "enter the number of numbers :" << endl;
  cin >> n;
  cout << "plz enter  nums" << endl;

  for(i=0;i<n;i++) cin >> a[i];

  for(j=1;j<n;j++)
    for(i=0;i<n-j;i++)
    {
      if(a[i]>a[i+1])
      {
        temp = a[i];
        a[i] = a[i+1];
        a[i+1] = temp;
      }
    }

  for(i=0;i<n;i++) cout << "num" << i << "=" << a[i] << endl;

  cout << "enter the number of numbers :" << endl;
  cin >> q;
  cout << "plz enter  nums" << endl;

  for(m=0;m<q;m++) cin >> b[m];

  for(h=1;h<q;h++)
    for(m=0;m<q-h;m++)
    {
      if(b[m]>b[m+1])
      {
        temp = b[m];
        b[m] = b[m+1];
        b[m+1] = temp;
      }
    }

  for(m=0;m<q;m++) cout << "num" << m << "=" << b[m] << endl;

  int    result[100];
  
  for(i=m=0;(i<n)&&(m<q);)  result[i+m-1] = a[i]<b[m] ? a[i++] : b[m++];
  for(;i<n;i++)             result[i+m] = a[i];
  for(;m<q;m++)             result[i+m] = b[m];

  for(i=0;i<q+n;i++)
  {
    cout << "yay " << result[i] << endl;
  }
}

Regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900