Jan 23, 2016 at 10:08pm UTC
I'm implement function of 2D array filled with random content 1 to M. The issue is that when I run the program, I must to get these response. 1 loop is authorized
Result wanted: Is dictionary/map
Key: number
value: frequency of key from array a and b
a = [2,1,1,4]
b = [1,3,3,5,5]
response = {1:3,2:1,5:2,3:2,4:1}
I try this code by I do not get a good result
// abd_test.cpp : définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
class Random
{
public:
Random()
{
srand(static_cast<unsigned int>(time(NULL)));
}
unsigned int operator()(unsigned int max)
{
double tmp = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
return static_cast<unsigned int>(tmp * max);
}
};
int main(int argc, char** argv)
{
int random_numbers[10000];
int size = sizeof(random_numbers) / sizeof(int);
for (int i = 1; i<size; ++i)
random_numbers[i] = i;
Random r;
random_shuffle(random_numbers, random_numbers + size, r);
for (int i = 1; i < 10000; i++)
{
cout << random_numbers[i] << endl;
}
system("pause");
return 0;
}
Jan 24, 2016 at 12:08am UTC
I'm implement function of 2D array filled with random content
Why not use rand() to fill each element as in
1 2 3 4 5 6 7 8 9
#include <algorithm>
#include <cstdlib>
int main(int argc, char * argv[])
{
int random_numbers[10000];
std::generate_n(random_numbers, 10000, std::rand);
// std::random_shuffle(random_numbers, random_numbers + 10000);
}
Why are you doing that shuffle?
Last edited on Jan 24, 2016 at 12:13am UTC
Jan 24, 2016 at 12:38am UTC
Thank Kbw, My problem is these two vectors. these two given vectors that my program has to write with this frequency,
a = [2,1,1,4]
b = [1,3,3,5,5]
response = {1:3,2:1,5:2,3:2,4:1}