Другие языки программирования и технологии

Нужно перемешать массив на C++. Есть массив, его нужно случайным образом перемешать. Нужен именно КОД, а не алгоритм

// diejfi_fenfe_2011-11-26
#include <iostream>
#include <ctime>
using namespace std;
#define N 10

void main()
{
srand((unsigned) time(NULL));

int number[N] = {0,1,2,3,4,5,6,7,8,9};
int index[N];
int mixed[N];

for (int n = 0; n < N; n++) cout << ' ' << number[n];

for (int n = 0; n < N; n++)
{
index[n] = rand() % N;

for (int m = 0; m < n; m++)
{
if (index[m] == index[n])
{
n--;
break;
}
}
}

cout << endl;

for (int n = 0; n < N; n++)
{
mixed[n] = number[index[n]];
cout << ' ' << mixed[n];
}

fflush(stdin);
cin.get();
}
Азиз Аралбаев
Азиз Аралбаев
55 928
Лучший ответ
Интересно, а как Вы себе представляете, что такое "КОД" на С++?
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

void swap (char *a, int e1, int e2)
{
char temp = a[ e1 ];
a[ e1 ] = a[ e2 ];
a[ e2 ] = temp;
}

int main (void)
{
char a[ ] = {"Good, bad, I'm the guy with the gun !!!"};
int i, temp, size = strlen(a);

srand( time( NULL ) );
cout << a << endl;

for (i = 0; i < size; i++)
swap(a, rand() % size, rand() % size);

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

Похожие вопросы