I have to write two arrays of 10 random numbers betwwen 1 and 25 then sort those two arrays in increasing order then make a third array that has all the numbers of the first two arrays in sorted order but no duplicate numbers in the third array.

Here are my function prototypes:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void rand_gen(int a[], int size);
void sorting(int a[], int size);
int linear_search(int a[], int size, int key);
void print_out(int a[], int size);
void merge(int a[], int b[]);

Here is my main function:

#include "lab10_1.h"

int main()
{
    const int max=20;
    int a[max], b[max];
    srand(time(0));
    rand_gen(a, 10);
    cout<<"\nArray 1 is the following: \n";
    print_out(a, 10);
    sorting(a, 10);
    cout<<"\nArray 1 in increasing order: \n";
    print_out(a, 10);
    rand_gen(b, 10);
    cout<<"\nArray 2 is the following: \n";
    print_out(b, 10);
    sorting(b, 10);
    cout<<"\nArray 2 in increasing order: \n";
    print_out(b, 10);
    return 0;
}

And here are my functions:

#include "lab10_1.h"

void rand_gen(int a[], int size)
{
    int i;
    size=10;
    for (i=0; i<size; i++)
        a[i]=1+rand()%25;
}
void sorting(int a[], int size)
{
    int i, pass, hold;
    for (pass=0; pass<size-1; pass++)
    {
        for (i=0; i<size-1; i++)
        {
            if (a[i]>a[i+1])
            {
                hold=a[i];
                a[i]=a[i+1];
                a[i+1]=hold;
            }
        }
    }
}
void print_out(int a[], int size)
{
    int counter=0;
    for (int i=0; i<size; i++)
    {
        counter++;
        cout<<a[i]<<" ";
    }
    cout<<endl;
}
int linear_search(int a[], int size, int key)
{
    int i, position=-1;
    for (i=0; i<size; i++)
    {
        if (a[i]==key)
            return i;
        else
            return position;
    }
}
void merge(int a[], int b[])
{
// I dont know
}

So far I can create the random arrays, print them in increasing order, but i cannot make the third array and delete the duplicate numbers.

So are there any suggestions? I'm a little short on time I have finals on Weds and a 14 page paper due Tues and this code is due Tues by 8am.

Any help is appreciated...

What have you tried so far?

Well I have tried numerous things but to no avail I think I need to manipulate my linear search functin to find the duplicates but other than that I'm not sure what I need to do to actually compile the third array... I'm at a total loss

This is going to sound like I'm being an a**, but I don't mean to be... sometimes it helps to just put stuff down on paper and visualize it.

Write 2 columns of ascending (sorted) numbers on the paper (you can use 10 values, but less will suffice for illustration). Have an arrow point to the top of the "a" (left) list, and another arrow point to the top of the "b" (right) list. You will single step through the values in the "a" list, comparing them to the values pointed to by the pointer in the "b" list. Set aside room for a result list (which is now empty):

Compare:
- If the pointer for the "a" list is past the end of the "a" array, write all the remaining values from the "b" list to the result list and exit.
or
- If the pointer for the "b" list is past the end of the "b" array, write all the remaining values from the "a" list to the result list and exit.

- If the "current" value in the "a" list is greater than the "current" value in the "b" list, write the "b" list number in the result list and move the pointer up in the "b" list. Go back to "Compare:"
- If the "current" value in the "a" list is less than the "current" value in the "b" list, write the "a" list number in the result list and move the pointer up in the "a" list. Go back to "Compare:"
- If the "current" value in the "a" list is equal to the "current" value in the "b" list, write the value from the "a" list to the result list and move both pointers up. Go to "Compare:"

Once you have this down on paper, translate it to code and you're done.

Take a look at sort() from <algorithm>

Ok here is what I tried for my merge function

void merge(int a[], int b[], int c[])
{
    int inxa=0;
    int inxb=0;
    int inxc=0;
    while (inxa<a[10] && inxb<b[10])
    {
        if (a[inxa]<b[inxb])
        {
            c[inxc]=a[inxa];
            inxa++;
        }
        else
        {
            c[inxc]=b[inxb];
            inxb++;
        }
        inxc++;
    }
    while (inxa<a[10])
    {
        c[inxc]=a[inxa];
        inxa++;
 inxc++;
    }
    while (inxb<b[10])
    {
        c[inxc]=b[inxb];
        inxb++;
        inxc++;
    }
}

And this is one of my random results:

Array 1 is the following:
11 7 7 19 1 1 7 11 13 8

Array 1 in increasing order:
1 1 7 7 7 8 11 11 13 19

Array 2 is the following:
21 5 7 9 24 23 6 11 3 16

Array 2 in increasing order:
3 5 6 7 9 11 16 21 23 24

Array 3 is the following:
1 0 0 0 -448573208 11177 -451693662 11177 -445779608 11177 5247480 0 -2126029856 32767 -451693662 11177 2 0 -450226970 11177

Another kind of result I'm getting is this:

Array 1 is the following:
9 7 11 6 20 13 4 24 3 15

Array 1 in increasing order:
3 4 6 7 9 11 13 15 20 24

Array 2 is the following:
14 3 2 19 19 10 3 20 3 13

Array 2 in increasing order:
2 3 3 3 10 13 14 19 19 20

Array 3 is the following:
2 3 3 3 10 13 14 19 19 20 454291376 32767 4197952 0 4196659 0 1278468456 11091 65535 1

A few adjustments to merge() might get you over the hump.

WARNING: following snippet not tested. Use at own risk and understand what comments are saying before using.

while(inxa < 10 && inxb < 10)//while not used either all of a or all of b.  You don't want a[10] and b[10] because they are elements in the arrays, and are not necessarily indicative of the size of the arrays
{
   if(a[inxa] <= b[inxb])//arbitrarily indicate a[x] < b[x] if a[x] equals b[x]
   {
     if(inxc > 0)//if something in c already
     {
        if(a[inxa] != c[inxc])//don't add duplicates to c
        {
          ++inxc;  //go to next available index of c
          c[inxc] = a[inxa]; //assign this value to c
        }
     }
     else //if nothing in c yet just add a[inxa] to c
       c[0] = a[inxa];
        
     ++inxa;//go to next value in a
   }
   else //if b[x] < a[x]
   {
     //as above except for b instead of a
   }
}

or if you prefer

int smallest;
while(inxa < 10 && inxb < 10)
{
  //startng at first elements in a and b, determine smallest of current values
  //then go to next element in appropriate array 
  if(a[inxa] <= b[inxb])
     smallest = a[inxa++];
  else
     smallest = b[inxb++];
  
  //add smallest to c if c is empty or if current element in c is not equal to smallest
  if(inxc == 0)
    c[0] = smallest;
  else
    if(c[inxc] != smallest)
      c[++inxc] = smallest;
}

Thank you all so much for your help! I got my correct code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.