0

I have looked and looked and am still lost on how to copy or get elements from an array and put them into new arrays ( divide and conquer is the goal).

I have an array that generates 100 random numbers. I need to split the random numbers into 4 smaller arrays obviously containing 25 elements and not have any duplicates. I have read about using pointers, but honestly I don't understand why even use a pointer. Why do I care about another variables address?

I don't know how to do this. Here is my code so far:

#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;


int main()
{
  // Seed the random number generator

  srand(time(NULL));

  //create an array to store our random numbers in

  int Orignumbers[100]    = {};
  // Arrays for the divide and conquer method
  int NumbersA   [25]     = {};
  int NumbersB   [25]     = {};
  int NumbersC   [25]     = {};
  int NumbersD   [25]     = {};


  //Generate the random numbers
  for(int i =0; i < 100; i++)
  {
    int SomeRandomNumber = rand() % 100 + 1;

  // Throw random number into the array

  Orignumbers[i] = SomeRandomNumber;

  }

//  for(int i = 0; i < ) started the for loop for the other arrays, this is where I am stuck!!

  // Print out the random numbers
  for(int i = 0; i < 100; i++)
  {
    cout << Orignumbers[i] << " , ";
  }

}
Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
JFive575
  • 19
  • 1
  • 1
  • 6
  • 2
    What if, the initial 100 numbers are not distinct in the `Orignumbers` array? Then you cannot have any array of size 25 that has 25 distinct value, by pigeonhole principle. You would obviously have a duplicate in at least on of the 4 arrays. – Redwanul Haque Sourave Nov 07 '17 at 07:33
  • 4
    Use `std::vector` (or `std::array` for fixed size), not raw arrays. Preferably use the `` facilities instead of old `srand` etc. When you want random numbers without duplicates in a contiguous sequence, consider shuffling. – Cheers and hth. - Alf Nov 07 '17 at 07:33
  • possible duplicate : https://stackoverflow.com/questions/20409931/how-to-copy-values-from-an-array-into-a-new-one – Alden Ken Nov 07 '17 at 07:35
  • http://en.cppreference.com/w/cpp/algorithm/copy – Jesper Juhl Nov 07 '17 at 07:37

4 Answers4

1

"divide and conquer" is rather easy; when copying into NumbersA and so forth, you just have to access your Originnumbers with a proper offset, i.e. 0, 25, 50, and 75:

for(int i = 0; i < 25; i++) {
    NumbersA[i] = Orignumbers[i];
    NumbersB[i] = Orignumbers[i+25];
    NumbersC[i] = Orignumbers[i+50];
    NumbersD[i] = Orignumbers[i+75];
}

The thing about "no duplicates" is a little bit more tricky. Generating a random sequence of unique numbers is usually solved through "shuffling". Standard library provides functions for that:

#include <random>
#include <algorithm>
#include <iterator>
#include <vector>

int main()
{
    std::random_device rd;
    std::mt19937 g(rd());

    int Orignumbers[100];

    //Generate the random numbers without duplicates
    for(int i =0; i < 100; i++) {
        Orignumbers[i] = i+1;
    }
    std::shuffle(Orignumbers, Orignumbers+100, g);

    // Arrays for the divide and conquer method
    int NumbersA   [25]     = {};
    int NumbersB   [25]     = {};
    int NumbersC   [25]     = {};
    int NumbersD   [25]     = {};


    for(int i = 0; i < 25; i++) {
        NumbersA[i] = Orignumbers[i];
        NumbersB[i] = Orignumbers[i+25];
        NumbersC[i] = Orignumbers[i+50];
        NumbersD[i] = Orignumbers[i+75];
    }

    // Print out the random numbers
    for(int i = 0; i < 100; i++)
    {
        cout << Orignumbers[i] << " , ";
    }

}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

As you tagged your question with C++ then forget about old-fashion arrays, let's do it C++ style. You want to split your array into 4 arrays and they should not have duplicate numbers, so you can't have a number 5 times in your original array, because then surely one of your 4 arrays will have a duplicate one, So here is the way I propose to do it :

#include <set>
#include <ctime>
#include <vector>

int main() {
    std::multiset<int> allNums;
    std::srand(unsigned(std::time(0)));

    for (int i = 0; i < 100; ++i) {
        int SomeRandomNumber = std::rand() % 100 + 1;
        if (allNums.count(SomeRandomNumber) < 4) {
            allNums.insert(SomeRandomNumber);
        }
        else {
            --i;
        }
    }
    std::vector<int> vOne, vTwo, vThree, vFour;
    for (auto iter = allNums.begin(); iter != allNums.end(); ++iter) {
        vOne.push_back(*iter);
        ++iter;
        vTwo.push_back(*iter);
        ++iter;
        vThree.push_back(*iter);
        ++iter;
        vFour.push_back(*iter);
    }
    system("pause");
    return 0;
}

EDIT : As you mentioned in the comments, you just want to find a number in an array, so how about this :

for (int i = 0; i < 100; ++i) {
    if (origArray[i] == magicNumber) {
        cout << "magicNumber founded in index " << i << "of origArray";
    }
}
faruk13
  • 1,276
  • 1
  • 16
  • 23
HMD
  • 2,202
  • 6
  • 24
  • 37
  • Consider shuffling. – Cheers and hth. - Alf Nov 07 '17 at 08:09
  • @Cheers and hth. - Alf Yes, shuffling is the best way to create distinct numbers, but if I get the question right, 25 sized arrays should not have duplicate elements, So the original one can have duplicates (up to 4 for each). – HMD Nov 07 '17 at 08:28
  • I guess I should have asked this differently. I do not understand c++ at all. I struggle with this language and programming in general. This is for an assignment for an algorithms class. I don't need anything that is super way over the top, just something that is easy to implement. I guess the whole duplicate thing it doesn't really matter I was just under the assumption that 25 * 4 being 100 with 100 random numbers that there wouldn't be duplicates in the new arrays. The assignment is too search through an array one by one until we find a "magic number". – JFive575 Nov 07 '17 at 08:58
  • Then I think you just want to use the first part of @Stephan Lechner answer. But try to understand what you are working with, after all you want your grades to be acceptable at the end of the curriculum. – HMD Nov 07 '17 at 09:03
  • @JFive575 And for search each element of an array to find specific number you don't need to split it, Have a look at the edit on my answer. – HMD Nov 07 '17 at 09:06
  • I'm still lost! I understand the concepts. I read what you guys are saying but actually doing, well that's a different story . I tired doing the for loop with the Numbers A-D section, and didn't work properly. I don't know how else to ask the question. I'm trying to find one magic number out of 100 in this example. we have the original array of 100. She wants us to divide that original 100 into other arrays, search them and look for the "magic number". She also mentioned hyper threading but that's way over my head. – JFive575 Nov 07 '17 at 09:36
  • Dear @JFive575 I don't know how can I explain it simpler, You can divide your original array into for sub arrays using Stephan first code and then search each of them with the way in **EDIT** in my answer. I'm not sure what you mean by "magic number" but if it's some given number to search for, this is the way to go. – HMD Nov 07 '17 at 09:41
0

Problem:

The program can't be guaranteed to have no duplicate value as the rand() function can generate any random sequence and that may include the decimal value of 99 for 99 times though probability is very low but chances are.
Example:

for(loop=0; loop<9; loop++) 
printf("%d", Rand()%10);

If looped for 10 times, it may result some values like:

  Output: 6,1,1,1,2,9,1,3,6,9
  Compiled Successfully:

Hence, no certainity that values won't repeat

Possibly Solution:

There could be a solution where you can place the values in OriginalArray and compare the rand() generate values against the OriginalArray values. For first iteration of loop, you can directly assign value to OriginalArray then from 2nd iteration of loop you've to compare rand() value against OriginalArray but insertion time consumption may be higher than O(NN) as rand() function may repeat values.

Possibly Solution:

 #include <iostream>
 #include <time.h>
 #include <stdlib.h>
 using namespace std;
 int main()
 {
   int Orignumbers[100] ;

  int NumbersA [25] ,
    NumbersB [25] , 
    NumbersC [25] , 
    NumbersD [25] ;
    srand(time(NULL));

   for(int i =0; i < 100; i++){
    Orignumbers[i] = rand() % 100+1; 
   for(int loop=0; loop<i; loop++) {
       if(Orignumber[loop] == Orignumber[i] ) {
           i--;
           break;
        }
     }
  }

    //Placing in four different arrays thats maybe needed.
    for(int i = 0; i <25; i++ ) {
    NumbersA[i] = Orignumbers[i];
    NumbersB[i] = Orignumbers[i+25];
    NumbersC[i] = Orignumbers[i+50];
    NumbersD[i] = Orignumbers[i+75];
    } 

    for(int i = 0; i < 99; i++)
    cout << Orignumbers[i] << " , ";
    }
Zahid Khan
  • 2,130
  • 2
  • 18
  • 31
0

On some situations, even on C++, the use of arrays might be preferable than vectors, for example, when dealing with multidimensional arrays (2D, 3D, etc) that needs to be continuous and ordered on the memory. (e.g. later access by other applications or faster exporting to file using formats such as HDF5.)

Like Jesper pointed out, you may use Copy and I would add MemCopy to copy the content of an array or memory block into another.

Don't underestimate the importance of pointers, they may solve your problem without the need doing any copy. A bit like Stephan solution but without the need of the index variable "i", just having the pointers initialized at different places on the array. For a very large number of elements, such strategy will save some relevant processing time.

Fábio Lobão
  • 171
  • 1
  • 11