Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,197 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,335 people online right now. Registration is fast and FREE... Join Now!




Merge arrays UGENT

 
Reply to this topicStart new topic

Merge arrays UGENT, help writing the function for the last part

satjit_singh88
30 May, 2008 - 04:17 AM
Post #1

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 8

Would anyone have ana idea of how to write the last function, im having trouble with it

CODE
#include <iostream>

using namespace std;

void read(int list[], int &n);

void display(const int a[], int n);
// print the first n elements of a[]

void merge(int a[], int n1, int b[], int n2, int c[]);
// merge values in ascending order of arrays a and b to array c in ascending order
// a has n1 integers; b has n2 integers

const int MAXSIZE = 10;

int main()
{
    // don't modify anything in main()
    int list1[MAXSIZE];
    int list2[MAXSIZE];
    int list3[MAXSIZE * 2];
    int size1, size2;
    
    read(list1, size1);
    display(list1, size1);
    
    read(list2, size2);
    display(list2, size2);
    
    merge(list1, size1, list2, size2, list3);
    
    display(list3, size1 + size2);

    cout << endl;
    system("pause");
    return 0;
}

void read(int list[], int& n)
{
    cout << "Enter no more than " << MAXSIZE << " integers in ascending order (-1 to finish): " << endl;
    n = 0;
    for (int i = 0; i < MAXSIZE; i++)
    {
      cin >> list[i];
      if (list[i] == -1)
        break;
      else
        n++;
      if (n == MAXSIZE)
        cin.ignore(100, '\n');
    }
    
}

// print the first n elements of a[]
void display(const int a[], int n)
{
     for (int i = 0; i < n; i++)
       cout << a[i] << " " << flush;
     cout << endl;
}

// merge sorted values of arrays a and b to array c
void merge(int a[], int n1, int b[], int n2, int c[])


User is offlineProfile CardPM
+Quote Post

foohoo
RE: Merge Arrays UGENT
30 May, 2008 - 04:41 AM
Post #2

New D.I.C Head
*

Joined: 9 Aug, 2007
Posts: 30


My Contributions
Hi,

I think the easiest way to do this would be have 2 for-loops in the function and a count. The count starts at 0. In the first for-loop add each element of "a" to "c" using n1 as the for-loop variable i.e. i<n1 incrementing the count variable each iteration (using it as the "c" array index i.e. c[count] = a[i]). Do the same in the second for loop adding "b" to "c" using the count where it left off and using n2 as the for-loop variable.

Once done the "c" array should be complete. You can then look for a sorting algorithm (Bubble sort, insert sort etc) to sort it into ascending order. There are loads of instances of these algorithms on this forum and on the net!

thanks!
User is offlineProfile CardPM
+Quote Post

KYA
RE: Merge Arrays UGENT
30 May, 2008 - 05:36 AM
Post #3

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,049



Thanked: 124 times
Dream Kudos: 1200
My Contributions
Here is it all:

cpp

/*
* Merge and Sort
* KYA
*/

#include <iostream>

using namespace std;

void read(int list[], int &n);
void display(const int a[], int n);
// print the first n elements of a[]
void merge(int a[], int n1, int b[], int n2, int c[]);
// merge values in ascending order of arrays a and b to array c in ascending order
// a has n1 integers; b has n2 integers

const int MAXSIZE = 10;

int main()
{
// don't modify anything in main()
int list1[MAXSIZE];
int list2[MAXSIZE];
int list3[MAXSIZE * 2];
int size1, size2;

read(list1, size1);
display(list1, size1);

read(list2, size2);
display(list2, size2);

merge(list1, size1, list2, size2, list3);

display(list3, (size1 + size2));

cout << endl;
system("pause");
return 0;
}

void read(int list[], int& n)
{
cout << "Enter no more than " << MAXSIZE << " integers in ascending order (-1 to finish): " << endl;
n = 0;
for (int i = 0; i < MAXSIZE; i++)
{
cin >> list[i];
if (list[i] == -1)
break;
else
n++;
if (n == MAXSIZE)
cin.ignore(100, '\n');
}

}

// print the first n elements of a[]
void display(const int a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " " << flush;
cout << endl;
}

// merge sorted values of arrays a and b to array c
void merge(int a[], int n1, int b[], int n2, int c[])
{
//merge a and b into one array c
for (int i = 0; i < n1; i++){
c[i] = a[i];
}//end for

for (int i = n1, j = 0; i < (n1+n2); i++, j++){
c[i] = b[j];
}//end for

cout << "\n\nc[] pre sort:";
display(c, (n1+n2));
cout << "\n\n";
//sort
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(c[i]<c[j])
{
int temp=c[i]; //swap
c[i]=c[j];
c[j]=temp;
}//end if
}//end inner for
}//end outer for
}//end merge and sort function



This one is a bubble sort. Enjoy



This post has been edited by KYA: 30 May, 2008 - 05:59 AM
User is online!Profile CardPM
+Quote Post

satjit_singh88
RE: Merge Arrays UGENT
30 May, 2008 - 05:12 PM
Post #4

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 8

Thanks alot smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:13PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month